summaryrefslogtreecommitdiff
path: root/pylint/checkers/variables.py
diff options
context:
space:
mode:
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>2023-04-06 19:38:05 +0200
committerGitHub <noreply@github.com>2023-04-06 19:38:05 +0200
commit84d495968cebf368c69932f538ba8d7d2f0bdbd9 (patch)
tree88166a3be7dd5f0c554f7ceb8d935fc6a6dab78b /pylint/checkers/variables.py
parentde0147e92586be56cdee9711dd3d3a7a7e1c9ef2 (diff)
downloadpylint-git-84d495968cebf368c69932f538ba8d7d2f0bdbd9.tar.gz
Fix check unused arguments false positive bug (#8542) (#8545)
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. Update `unused-argument` test to include a check for the case of `__init__` and `__new__` being defined in a class but `__new__` does not use all of the argument. This is fine because `__new__` must have the same argument of `__init__`. Update with a second check in case of `__init__` being not defined in a class. Then the unused arguments check must be done on `__new__`. Fixes https://github.com/pylint-dev/pylint/issues/3670 (cherry picked from commit 156da64d0fb4c06e15c5b619b91ce550d594a770) Co-authored-by: Théo Battrel <theo.util@protonmail.ch>
Diffstat (limited to 'pylint/checkers/variables.py')
-rw-r--r--pylint/checkers/variables.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py
index f55b71264..79d9ded08 100644
--- a/pylint/checkers/variables.py
+++ b/pylint/checkers/variables.py
@@ -2602,6 +2602,16 @@ class VariablesChecker(BaseChecker):
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(