Skip to content

Commit

Permalink
Fix #293, B024 now skips classes with class attribute declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Oct 4, 2022
1 parent 65c141e commit cea0499
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def check_for_b023(self, loop_node):
if reassigned_in_loop.issuperset(err.vars):
self.errors.append(err)

def check_for_b024_and_b027(self, node: ast.ClassDef):
def check_for_b024_and_b027(self, node: ast.ClassDef): # noqa: C901
"""Check for inheritance from abstract classes in abc and lack of
any methods decorated with abstract*"""

Expand Down Expand Up @@ -661,6 +661,12 @@ def is_str_or_ellipsis(node):
has_abstract_method = False

for stmt in node.body:
# https://github.com/PyCQA/flake8-bugbear/issues/293
# Ignore abc's that declares a class attribute that must be set
if isinstance(stmt, (ast.AnnAssign, ast.Assign)):
has_abstract_method = True
continue

# only check function defs
if not isinstance(stmt, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
Expand Down
17 changes: 17 additions & 0 deletions tests/b024.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,20 @@ def method(self):
class keyword_abc_2(metaclass=abc.ABC): # safe
def method(self):
foo()


class abc_set_class_variable_1(ABC): # safe
foo: int


class abc_set_class_variable_2(ABC): # safe
foo = 2


class abc_set_class_variable_3(ABC): # safe
foo: int = 2


# this doesn't actually declare a class variable, it's just an expression
class abc_set_class_variable_4(ABC): # error
foo
1 change: 1 addition & 0 deletions tests/test_bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def test_b024(self):
B024(58, 0, vars=("MetaBase_1",)),
B024(69, 0, vars=("abc_Base_1",)),
B024(74, 0, vars=("abc_Base_2",)),
B024(128, 0, vars=("abc_set_class_variable_4",)),
)
self.assertEqual(errors, expected)

Expand Down

0 comments on commit cea0499

Please sign in to comment.