Skip to content

Commit

Permalink
Fix PyCQA#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 5991c6a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions bugbear.py
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
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
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 5991c6a

Please sign in to comment.