Skip to content
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

Fix a false negative for duplicate-argument-name #9670

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/user_guide/checkers/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Basic checker Messages
methods and is instantiated.
:star-needs-assignment-target (E0114): *Can use starred expression only in assignment target*
Emitted when a star expression is not used in an assignment target.
:duplicate-argument-name (E0108): *Duplicate argument name %s in function definition*
:duplicate-argument-name (E0108): *Duplicate argument name %r in function definition*
Duplicate argument names in function definitions are syntax errors.
:return-in-init (E0101): *Explicit return in __init__*
Used when the special class method __init__ has an explicit return value.
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/9669.false_negative
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a false negative for ``duplicate-argument-name`` by including ``positional-only``, ``*args`` and ``**kwargs`` arguments in the check.

Closes #9669
6 changes: 3 additions & 3 deletions pylint/checkers/base/basic_error_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class BasicErrorChecker(_BasicChecker):
"pre-decrement operator -- and ++, which doesn't exist in Python.",
),
"E0108": (
"Duplicate argument name %s in function definition",
"Duplicate argument name %r in function definition",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an incidental change to add quotes to the name of the missing argument in the message output.

"duplicate-argument-name",
"Duplicate argument names in function definitions are syntax errors.",
),
Expand Down Expand Up @@ -285,8 +285,8 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
self.add_message("return-in-init", node=node)
# Check for duplicate names by clustering args with same name for detailed report
arg_clusters = {}
arguments: Iterator[Any] = filter(None, [node.args.args, node.args.kwonlyargs])
for arg in itertools.chain.from_iterable(arguments):
arguments: Iterator[Any] = node.args.arguments
for arg in arguments:
mbyrnepr2 marked this conversation as resolved.
Show resolved Hide resolved
if arg.name in arg_clusters:
self.add_message(
"duplicate-argument-name",
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/a/async_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ too-many-arguments:26:0:26:26:complex_function:Too many arguments (10/5):UNDEFIN
too-many-branches:26:0:26:26:complex_function:Too many branches (13/12):UNDEFINED
too-many-return-statements:26:0:26:26:complex_function:Too many return statements (10/6):UNDEFINED
dangerous-default-value:59:0:59:14:func:Dangerous default value [] as argument:UNDEFINED
duplicate-argument-name:59:18:59:19:func:Duplicate argument name a in function definition:HIGH
duplicate-argument-name:59:18:59:19:func:Duplicate argument name 'a' in function definition:HIGH
disallowed-name:64:0:64:13:foo:"Disallowed name ""foo""":HIGH
empty-docstring:64:0:64:13:foo:Empty function docstring:HIGH
17 changes: 13 additions & 4 deletions tests/functional/d/duplicate/duplicate_argument_name.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
"""Check for duplicate function arguments."""

# pylint: disable=missing-docstring, line-too-long


def foo1(_, _): # [duplicate-argument-name]
"""Function with duplicate argument name."""
...

def foo2(_abc, *, _abc): # [duplicate-argument-name]
"""Function with duplicate argument name."""
...

def foo3(_, _=3): # [duplicate-argument-name]
"""Function with duplicate argument name."""
...

def foo4(_, *, _): # [duplicate-argument-name]
"""Function with duplicate argument name."""
...

def foo5(_, *_, _=3): # [duplicate-argument-name, duplicate-argument-name]
...

# +1: [duplicate-argument-name, duplicate-argument-name, duplicate-argument-name, duplicate-argument-name]
def foo6(_, /, _, *_, _="_", **_):
Pierre-Sassoulas marked this conversation as resolved.
Show resolved Hide resolved
...
14 changes: 10 additions & 4 deletions tests/functional/d/duplicate/duplicate_argument_name.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
duplicate-argument-name:4:12:4:13:foo1:Duplicate argument name _ in function definition:HIGH
duplicate-argument-name:7:18:7:22:foo2:Duplicate argument name _abc in function definition:HIGH
duplicate-argument-name:10:12:10:13:foo3:Duplicate argument name _ in function definition:HIGH
duplicate-argument-name:13:15:13:16:foo4:Duplicate argument name _ in function definition:HIGH
duplicate-argument-name:6:12:6:13:foo1:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:9:18:9:22:foo2:Duplicate argument name '_abc' in function definition:HIGH
duplicate-argument-name:12:12:12:13:foo3:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:15:15:15:16:foo4:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:18:13:18:14:foo5:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:18:16:18:17:foo5:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:22:15:22:16:foo6:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:22:19:22:20:foo6:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:22:22:22:23:foo6:Duplicate argument name '_' in function definition:HIGH
duplicate-argument-name:22:31:22:32:foo6:Duplicate argument name '_' in function definition:HIGH
5 changes: 0 additions & 5 deletions tests/functional/d/duplicate/duplicate_argument_name_py3.py

This file was deleted.

This file was deleted.