Skip to content

Commit 0c6384f

Browse files
committed
Fix check unused arguments false positive bug
Problem: the special method `__new__` must match the arguments of the `__init__` method even if `__new__` method does not use them. This generate `unused-argument` for the `__new__` method. Fix: the unused arguments check should not be done on the `__new__` method if the `__init__` method is defined in the same class. Fixes pylint-dev#3670
1 parent 82cee37 commit 0c6384f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pylint/checkers/variables.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,16 @@ def _check_is_unused(
25932593
argnames = node.argnames()
25942594
# Care about functions with unknown argument (builtins)
25952595
if name in argnames:
2596+
if node.name == "__new__":
2597+
is_init_def = False
2598+
# Look for the `__init__` method in all the methods of the same class.
2599+
for n in node.parent.get_children():
2600+
is_init_def = (hasattr(n, "name") and (n.name == "__init__"))
2601+
if is_init_def:
2602+
break
2603+
# Ignore unused arguments check for `__new__` if `__init__` is defined.
2604+
if is_init_def:
2605+
return
25962606
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)
25972607
else:
25982608
if stmt.parent and isinstance(

0 commit comments

Comments
 (0)