Skip to content

Commit

Permalink
Fix invalid-name regression for class attributes in subclasses (#9772
Browse files Browse the repository at this point in the history
…) (#9775)

(cherry picked from commit b9a42e8)

Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
  • Loading branch information
github-actions[bot] and jacobtylerwalls authored Jul 7, 2024
1 parent ae730ac commit aea868c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9765.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a regression that raised ``invalid-name`` on class attributes merely
overriding invalid names from an ancestor.

Closes #9765
4 changes: 3 additions & 1 deletion pylint/checkers/base/name_checker/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ def visit_assignname( # pylint: disable=too-many-branches
self._check_name("variable", node.name, node)

# Check names defined in class scopes
elif isinstance(frame, nodes.ClassDef):
elif isinstance(frame, nodes.ClassDef) and not any(
frame.local_attr_ancestors(node.name)
):
if utils.is_enum_member(node) or utils.is_assign_name_annotated_with(
node, "Final"
):
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/i/invalid/invalid_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,10 @@ def test_disable_mixed(
"""Invalid-name will still be raised for other arguments."""
self.foo_bar = fooBar
self.foo_bar2 = fooBar2

def tearDown(self): ... # pylint: disable=invalid-name


class FooBarSubclass(FooBar):
tearDown = FooBar.tearDown
tearDownNotInAncestor = None # [invalid-name]
2 changes: 2 additions & 0 deletions tests/functional/i/invalid/invalid_name.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MAIN]
class-attribute-naming-style=snake_case
1 change: 1 addition & 0 deletions tests/functional/i/invalid/invalid_name.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ invalid-name:66:0:66:68:a_very_very_very_long_function_name_WithCamelCase_to_mak
invalid-name:74:23:74:29:FooBar.__init__:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
invalid-name:80:8:80:14:FooBar.func1:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
invalid-name:100:8:100:15:FooBar.test_disable_mixed:"Argument name ""fooBar2"" doesn't conform to snake_case naming style":HIGH
invalid-name:111:4:111:25:FooBarSubclass:"Class attribute name ""tearDownNotInAncestor"" doesn't conform to snake_case naming style":HIGH

0 comments on commit aea868c

Please sign in to comment.