Skip to content

Commit b9a42e8

Browse files
Fix invalid-name regression for class attributes in subclasses (#9772)
1 parent 52955ba commit b9a42e8

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a regression that raised ``invalid-name`` on class attributes merely
2+
overriding invalid names from an ancestor.
3+
4+
Closes #9765

pylint/checkers/base/name_checker/checker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,9 @@ def visit_assignname( # pylint: disable=too-many-branches
491491
self._check_name("variable", node.name, node)
492492

493493
# Check names defined in class scopes
494-
elif isinstance(frame, nodes.ClassDef):
494+
elif isinstance(frame, nodes.ClassDef) and not any(
495+
frame.local_attr_ancestors(node.name)
496+
):
495497
if utils.is_enum_member(node) or utils.is_assign_name_annotated_with(
496498
node, "Final"
497499
):

tests/functional/i/invalid/invalid_name.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,10 @@ def test_disable_mixed(
102102
"""Invalid-name will still be raised for other arguments."""
103103
self.foo_bar = fooBar
104104
self.foo_bar2 = fooBar2
105+
106+
def tearDown(self): ... # pylint: disable=invalid-name
107+
108+
109+
class FooBarSubclass(FooBar):
110+
tearDown = FooBar.tearDown
111+
tearDownNotInAncestor = None # [invalid-name]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[MAIN]
2+
class-attribute-naming-style=snake_case

tests/functional/i/invalid/invalid_name.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ invalid-name:66:0:66:68:a_very_very_very_long_function_name_WithCamelCase_to_mak
66
invalid-name:74:23:74:29:FooBar.__init__:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
77
invalid-name:80:8:80:14:FooBar.func1:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
88
invalid-name:100:8:100:15:FooBar.test_disable_mixed:"Argument name ""fooBar2"" doesn't conform to snake_case naming style":HIGH
9+
invalid-name:111:4:111:25:FooBarSubclass:"Class attribute name ""tearDownNotInAncestor"" doesn't conform to snake_case naming style":HIGH

0 commit comments

Comments
 (0)