Skip to content

Improve node info for invalid-name #5094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Release date: TBA

Closes #5111

* Improve node information for ``invalid-name`` on function argument.


What's New in Pylint 2.11.1?
============================
Expand Down
8 changes: 4 additions & 4 deletions pylint/checkers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
# Check argument names
args = node.args.args
if args is not None:
self._recursive_check_names(args, node)
self._recursive_check_names(args)

visit_asyncfunctiondef = visit_functiondef

Expand Down Expand Up @@ -2008,13 +2008,13 @@ def visit_assignname(self, node: nodes.AssignName) -> None:
else:
self._check_name("class_attribute", node.name, node)

def _recursive_check_names(self, args, node):
def _recursive_check_names(self, args):
"""check names in a possibly recursive list <arg>"""
for arg in args:
if isinstance(arg, nodes.AssignName):
self._check_name("argument", arg.name, node)
self._check_name("argument", arg.name, arg)
else:
self._recursive_check_names(arg.elts, node)
self._recursive_check_names(arg.elts)

def _find_name_group(self, node_type):
return self._name_group.get(node_type, node_type)
Expand Down
33 changes: 31 additions & 2 deletions tests/functional/i/invalid/invalid_name.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" Tests for invalid-name checker. """
# pylint: disable=unused-import, wrong-import-position, import-outside-toplevel, missing-class-docstring
# pylint: disable=unused-import, wrong-import-position, import-outside-toplevel, missing-class-docstring,missing-function-docstring
# pylint: disable=too-few-public-methods


Expand Down Expand Up @@ -72,5 +72,34 @@ def a_very_very_very_long_function_name_WithCamelCase_to_make_it_sad(): # Shoul


class FooBar:
def __init__(self, fooBar) -> None: # [invalid-name]
def __init__(self, fooBar) -> None: # [invalid-name]
self.foo_bar = fooBar
self.foo_bar2 = None

def func1(
self,
fooBar, # [invalid-name]
):
self.foo_bar = fooBar

# Test disable invalid-name
def test_disable1(self, fooBar): # pylint: disable=invalid-name
self.foo_bar = fooBar

def test_disable2(
self,
fooBar, # pylint: disable=invalid-name
):
self.foo_bar = fooBar

def test_disable3(self, fooBar): # pylint: disable=invalid-name
self.foo_bar = fooBar

def test_disable_mixed(
self,
fooBar, # pylint: disable=invalid-name
fooBar2, # [invalid-name]
):
"""Invalid-name will still be raised for other arguments."""
self.foo_bar = fooBar
self.foo_bar2 = fooBar2
4 changes: 3 additions & 1 deletion tests/functional/i/invalid/invalid_name.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ invalid-name:16:4::"Constant name ""time"" doesn't conform to UPPER_CASE naming
invalid-name:32:0:a:"Function name ""a"" doesn't conform to snake_case naming style":HIGH
invalid-name:46:4::"Constant name ""Foocapfor"" doesn't conform to UPPER_CASE naming style":HIGH
invalid-name:63:0:a_very_very_very_long_function_name_WithCamelCase_to_make_it_sad:"Function name ""a_very_very_very_long_function_name_WithCamelCase_to_make_it_sad"" doesn't conform to snake_case naming style":HIGH
invalid-name:75:4:FooBar.__init__:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
invalid-name:75:23:FooBar.__init__:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
invalid-name:81:8:FooBar.func1:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
invalid-name:101:8:FooBar.test_disable_mixed:"Argument name ""fooBar2"" doesn't conform to snake_case naming style":HIGH
2 changes: 1 addition & 1 deletion tests/functional/n/name/name_styles.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
invalid-name:11:0::"Constant name ""bad_const_name"" doesn't conform to UPPER_CASE naming style"
invalid-name:14:0:BADFUNCTION_name:"Function name ""BADFUNCTION_name"" doesn't conform to snake_case naming style"
invalid-name:16:4:BADFUNCTION_name:"Variable name ""BAD_LOCAL_VAR"" doesn't conform to snake_case naming style"
invalid-name:20:0:func_bad_argname:"Argument name ""NOT_GOOD"" doesn't conform to snake_case naming style"
invalid-name:20:21:func_bad_argname:"Argument name ""NOT_GOOD"" doesn't conform to snake_case naming style"
invalid-name:30:0:bad_class_name:"Class name ""bad_class_name"" doesn't conform to PascalCase naming style"
invalid-name:41:8:CorrectClassName.__init__:"Attribute name ""_Bad_AtTR_name"" doesn't conform to snake_case naming style"
invalid-name:42:8:CorrectClassName.__init__:"Attribute name ""Bad_PUBLIC_name"" doesn't conform to snake_case naming style"
Expand Down