Skip to content

Commit 17ddcf4

Browse files
authored
Improve node info for invalid-name (#5094)
1 parent f193806 commit 17ddcf4

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

ChangeLog

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Release date: TBA
6262

6363
Closes #5111
6464

65+
* Improve node information for ``invalid-name`` on function argument.
66+
6567

6668
What's New in Pylint 2.11.1?
6769
============================

pylint/checkers/base.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
19571957
# Check argument names
19581958
args = node.args.args
19591959
if args is not None:
1960-
self._recursive_check_names(args, node)
1960+
self._recursive_check_names(args)
19611961

19621962
visit_asyncfunctiondef = visit_functiondef
19631963

@@ -2008,13 +2008,13 @@ def visit_assignname(self, node: nodes.AssignName) -> None:
20082008
else:
20092009
self._check_name("class_attribute", node.name, node)
20102010

2011-
def _recursive_check_names(self, args, node):
2011+
def _recursive_check_names(self, args):
20122012
"""check names in a possibly recursive list <arg>"""
20132013
for arg in args:
20142014
if isinstance(arg, nodes.AssignName):
2015-
self._check_name("argument", arg.name, node)
2015+
self._check_name("argument", arg.name, arg)
20162016
else:
2017-
self._recursive_check_names(arg.elts, node)
2017+
self._recursive_check_names(arg.elts)
20182018

20192019
def _find_name_group(self, node_type):
20202020
return self._name_group.get(node_type, node_type)

tests/functional/i/invalid/invalid_name.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
""" Tests for invalid-name checker. """
2-
# pylint: disable=unused-import, wrong-import-position, import-outside-toplevel, missing-class-docstring
2+
# pylint: disable=unused-import, wrong-import-position, import-outside-toplevel, missing-class-docstring,missing-function-docstring
33
# pylint: disable=too-few-public-methods
44

55

@@ -72,5 +72,34 @@ def a_very_very_very_long_function_name_WithCamelCase_to_make_it_sad(): # Shoul
7272

7373

7474
class FooBar:
75-
def __init__(self, fooBar) -> None: # [invalid-name]
75+
def __init__(self, fooBar) -> None: # [invalid-name]
7676
self.foo_bar = fooBar
77+
self.foo_bar2 = None
78+
79+
def func1(
80+
self,
81+
fooBar, # [invalid-name]
82+
):
83+
self.foo_bar = fooBar
84+
85+
# Test disable invalid-name
86+
def test_disable1(self, fooBar): # pylint: disable=invalid-name
87+
self.foo_bar = fooBar
88+
89+
def test_disable2(
90+
self,
91+
fooBar, # pylint: disable=invalid-name
92+
):
93+
self.foo_bar = fooBar
94+
95+
def test_disable3(self, fooBar): # pylint: disable=invalid-name
96+
self.foo_bar = fooBar
97+
98+
def test_disable_mixed(
99+
self,
100+
fooBar, # pylint: disable=invalid-name
101+
fooBar2, # [invalid-name]
102+
):
103+
"""Invalid-name will still be raised for other arguments."""
104+
self.foo_bar = fooBar
105+
self.foo_bar2 = fooBar2

tests/functional/i/invalid/invalid_name.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ invalid-name:16:4::"Constant name ""time"" doesn't conform to UPPER_CASE naming
33
invalid-name:32:0:a:"Function name ""a"" doesn't conform to snake_case naming style":HIGH
44
invalid-name:46:4::"Constant name ""Foocapfor"" doesn't conform to UPPER_CASE naming style":HIGH
55
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
6-
invalid-name:75:4:FooBar.__init__:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
6+
invalid-name:75:23:FooBar.__init__:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
7+
invalid-name:81:8:FooBar.func1:"Argument name ""fooBar"" doesn't conform to snake_case naming style":HIGH
8+
invalid-name:101:8:FooBar.test_disable_mixed:"Argument name ""fooBar2"" doesn't conform to snake_case naming style":HIGH

tests/functional/n/name/name_styles.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
invalid-name:11:0::"Constant name ""bad_const_name"" doesn't conform to UPPER_CASE naming style"
22
invalid-name:14:0:BADFUNCTION_name:"Function name ""BADFUNCTION_name"" doesn't conform to snake_case naming style"
33
invalid-name:16:4:BADFUNCTION_name:"Variable name ""BAD_LOCAL_VAR"" doesn't conform to snake_case naming style"
4-
invalid-name:20:0:func_bad_argname:"Argument name ""NOT_GOOD"" doesn't conform to snake_case naming style"
4+
invalid-name:20:21:func_bad_argname:"Argument name ""NOT_GOOD"" doesn't conform to snake_case naming style"
55
invalid-name:30:0:bad_class_name:"Class name ""bad_class_name"" doesn't conform to PascalCase naming style"
66
invalid-name:41:8:CorrectClassName.__init__:"Attribute name ""_Bad_AtTR_name"" doesn't conform to snake_case naming style"
77
invalid-name:42:8:CorrectClassName.__init__:"Attribute name ""Bad_PUBLIC_name"" doesn't conform to snake_case naming style"

0 commit comments

Comments
 (0)