From 65c141e0425b991cb39caa19e55d09eeb655776c Mon Sep 17 00:00:00 2001
From: jakkdl
Date: Thu, 29 Sep 2022 11:48:05 +0200
Subject: [PATCH] fix B024 & B027 on python < 3.8
---
README.rst | 14 ++++++--------
bugbear.py | 16 ++++++++--------
tests/test_bugbear.py | 2 +-
3 files changed, 15 insertions(+), 17 deletions(-)
diff --git a/README.rst b/README.rst
index 2691856..5671345 100644
--- a/README.rst
+++ b/README.rst
@@ -296,23 +296,21 @@ MIT
Change Log
----------
+Future
+~~~~~~~~~
+
+* Add B027: Empty method in abstract base class with no abstract decorator
-<<<<<<< HEAD
22.9.23
~~~~~~~~~~
-* add B026: find argument unpacking after keyword argument (#287)
+* Add B026: find argument unpacking after keyword argument (#287)
* Move to setup.cfg like flake8 (#288)
22.9.11
~~~~~~~~~~
-* add B025: find duplicate except clauses (#284)
-=======
-Future
-~~~~~~~~~
-* Add B025: Empty method in abstract base class with no abstract decorator.
->>>>>>> 0937a2d (Adding B025: empty method in abstract base class with no abstract decorator)
+* Add B025: find duplicate except clauses (#284)
22.8.23
~~~~~~~~~~
diff --git a/bugbear.py b/bugbear.py
index c5ec2e4..b891b3f 100644
--- a/bugbear.py
+++ b/bugbear.py
@@ -635,17 +635,17 @@ def is_abstract_decorator(expr):
)
def empty_body(body) -> bool:
+ def is_str_or_ellipsis(node):
+ # ast.Ellipsis and ast.Str used in python<3.8
+ return isinstance(node, (ast.Ellipsis, ast.Str)) or (
+ isinstance(node, ast.Constant)
+ and (node.value is Ellipsis or isinstance(node.value, str))
+ )
+
# Function body consist solely of `pass`, `...`, and/or (doc)string literals
return all(
isinstance(stmt, ast.Pass)
- or (
- isinstance(stmt, ast.Expr)
- and isinstance(stmt.value, ast.Constant)
- and (
- stmt.value.value is Ellipsis
- or isinstance(stmt.value.value, str)
- )
- )
+ or (isinstance(stmt, ast.Expr) and is_str_or_ellipsis(stmt.value))
for stmt in body
)
diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py
index e15c926..15edd32 100644
--- a/tests/test_bugbear.py
+++ b/tests/test_bugbear.py
@@ -407,7 +407,7 @@ def test_b027(self):
B027(16, 4, vars=("empty_2",)),
B027(19, 4, vars=("empty_3",)),
B027(23, 4, vars=("empty_4",)),
- B027(31, 4, vars=("empty_5",)),
+ B027(31 if sys.version_info >= (3, 8) else 30, 4, vars=("empty_5",)),
)
self.assertEqual(errors, expected)