From 69d10cf61b3eb18981b4aa677b7b66c609793090 Mon Sep 17 00:00:00 2001 From: jakkdl Date: Mon, 28 Oct 2024 16:02:53 +0100 Subject: [PATCH 1/2] b024 no longer treats assigned class vars as abstract --- .github/workflows/ci.yml | 2 +- bugbear.py | 2 +- tests/b024.py | 18 +++++++++++++----- tests/test_bugbear.py | 2 ++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c88b3c..8131c10 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,7 +9,7 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13-dev"] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"] os: [ubuntu-latest] diff --git a/bugbear.py b/bugbear.py index 2519a24..23768d2 100644 --- a/bugbear.py +++ b/bugbear.py @@ -996,7 +996,7 @@ def is_str_or_ellipsis(node): 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)): + if isinstance(stmt, ast.AnnAssign) and stmt.value is None: has_abstract_method = True continue diff --git a/tests/b024.py b/tests/b024.py index f975887..058de7a 100644 --- a/tests/b024.py +++ b/tests/b024.py @@ -10,7 +10,7 @@ """ Should emit: -B024 - on lines 17, 34, 52, 58, 69, 74, 84, 89 +B024 - on lines 17, 52, 58, 69, 74, 123, 129 """ @@ -31,7 +31,7 @@ def method(self): foo() -class Base_4(ABC): +class Base_4(ABC): # safe @notabc.abstractmethod def method(self): foo() @@ -112,16 +112,24 @@ def method(self): foo() -class abc_set_class_variable_1(ABC): # safe +# safe, see https://github.com/PyCQA/flake8-bugbear/issues/293 +class abc_annasign_empty_class_variable_1(ABC): foo: int + def method(self): + foo() -class abc_set_class_variable_2(ABC): # safe +# *not* safe, see https://github.com/PyCQA/flake8-bugbear/issues/471 +class abc_assign_class_variable(ABC): foo = 2 + def method(self): + foo() -class abc_set_class_variable_3(ABC): # safe +class abc_annassign_class_variable(ABC): # *not* safe, see #471 foo: int = 2 + def method(self): + foo() # this doesn't actually declare a class variable, it's just an expression diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py index bdc2552..1f28130 100644 --- a/tests/test_bugbear.py +++ b/tests/test_bugbear.py @@ -461,6 +461,8 @@ 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(123, 0, vars=("abc_assign_class_variable",)), + B024(129, 0, vars=("abc_annassign_class_variable",)), ) self.assertEqual(errors, expected) From 5d0010810c4b7078b898e8fd268d90a978276c20 Mon Sep 17 00:00:00 2001 From: jakkdl Date: Mon, 28 Oct 2024 16:06:42 +0100 Subject: [PATCH 2/2] add changelog entry --- README.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.rst b/README.rst index 54bdbc2..09dbb07 100644 --- a/README.rst +++ b/README.rst @@ -357,6 +357,11 @@ MIT Change Log ---------- + +FUTURE +~~~~~~ +* B024: No longer treats assigned class variables as abstract (#471) + 24.8.19 ~~~~~~~