Skip to content

Commit cea0499

Browse files
committed
Fix #293, B024 now skips classes with class attribute declarations
1 parent 65c141e commit cea0499

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

bugbear.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def check_for_b023(self, loop_node):
612612
if reassigned_in_loop.issuperset(err.vars):
613613
self.errors.append(err)
614614

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

@@ -661,6 +661,12 @@ def is_str_or_ellipsis(node):
661661
has_abstract_method = False
662662

663663
for stmt in node.body:
664+
# https://github.com/PyCQA/flake8-bugbear/issues/293
665+
# Ignore abc's that declares a class attribute that must be set
666+
if isinstance(stmt, (ast.AnnAssign, ast.Assign)):
667+
has_abstract_method = True
668+
continue
669+
664670
# only check function defs
665671
if not isinstance(stmt, (ast.FunctionDef, ast.AsyncFunctionDef)):
666672
continue

tests/b024.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,20 @@ def method(self):
110110
class keyword_abc_2(metaclass=abc.ABC): # safe
111111
def method(self):
112112
foo()
113+
114+
115+
class abc_set_class_variable_1(ABC): # safe
116+
foo: int
117+
118+
119+
class abc_set_class_variable_2(ABC): # safe
120+
foo = 2
121+
122+
123+
class abc_set_class_variable_3(ABC): # safe
124+
foo: int = 2
125+
126+
127+
# this doesn't actually declare a class variable, it's just an expression
128+
class abc_set_class_variable_4(ABC): # error
129+
foo

tests/test_bugbear.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ def test_b024(self):
364364
B024(58, 0, vars=("MetaBase_1",)),
365365
B024(69, 0, vars=("abc_Base_1",)),
366366
B024(74, 0, vars=("abc_Base_2",)),
367+
B024(128, 0, vars=("abc_set_class_variable_4",)),
367368
)
368369
self.assertEqual(errors, expected)
369370

0 commit comments

Comments
 (0)