Skip to content

[Backport maintenance/2.17.x] Fix check unused arguments false positive bug #8545

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
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
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/3670.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix `unused-argument` false positive when `__new__` does not use all the arguments of `__init__`.

Closes #3670
10 changes: 10 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,16 @@ def _check_is_unused(
argnames = node.argnames()
# Care about functions with unknown argument (builtins)
if name in argnames:
if node.name == "__new__":
is_init_def = False
# Look for the `__init__` method in all the methods of the same class.
for n in node.parent.get_children():
is_init_def = hasattr(n, "name") and (n.name == "__init__")
if is_init_def:
break
# Ignore unused arguments check for `__new__` if `__init__` is defined.
if is_init_def:
return
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
else:
if stmt.parent and isinstance(
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/u/unused/unused_argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,24 @@ class Descendant(Ancestor):
def set_thing(self, thing, *, other=None):
"""Subclass does not raise unused-argument"""
self.thing = thing


# Test that Class with both `__init__` and `__new__` don't check
# on `__new__` for unused arguments

# pylint: disable=invalid-name

class TestClassWithInitAndNew:
def __init__(self, argA, argB):
self.argA = argA
self.argB = argB

def __new__(cls, argA, argB):
return object.__new__(cls)

# Test that `__new__` method is checked for unused arguments
# when `__init__` is not in the Class

class TestClassWithOnlyNew:
def __new__(cls, argA, argB): # [unused-argument, unused-argument]
return object.__new__(cls)
2 changes: 2 additions & 0 deletions tests/functional/u/unused/unused_argument.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ unused-argument:73:0:None:None:AAAA.selected:Unused argument 'args':INFERENCE
unused-argument:73:0:None:None:AAAA.selected:Unused argument 'kwargs':INFERENCE
unused-argument:92:23:92:26:BBBB.__init__:Unused argument 'arg':INFERENCE
unused-argument:103:34:103:39:Ancestor.set_thing:Unused argument 'other':INFERENCE
unused-argument:129:21:129:25:TestClassWithOnlyNew.__new__:Unused argument 'argA':INFERENCE
unused-argument:129:27:129:31:TestClassWithOnlyNew.__new__:Unused argument 'argB':INFERENCE