diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-11-07 18:12:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-07 18:12:08 +0200 |
commit | 6827dfe84e793a4fc9a07afc3a463ded6e22b230 (patch) | |
tree | 06965ca42cdf02f3abc5c103094d853a767cd462 | |
parent | 0a1ebd488fcdf7bf306615aba29e401ce49e3e10 (diff) | |
download | pylint-git-6827dfe84e793a4fc9a07afc3a463ded6e22b230.tar.gz |
Make ``self-cls-assignment`` check tuple assignment (#5268)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | doc/whatsnew/2.12.rst | 2 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 28 | ||||
-rw-r--r-- | tests/functional/s/self/self_cls_assignment.py | 2 | ||||
-rw-r--r-- | tests/functional/s/self/self_cls_assignment.txt | 9 |
5 files changed, 23 insertions, 20 deletions
@@ -127,6 +127,8 @@ Release date: TBA Closes #4426 +* ``self-cls-assignment`` now also considers tuple assignment + * Fix ``missing-function-docstring`` not being able to check ``__init__`` and other magic methods even if the ``no-docstring-rgx`` setting was set to do so diff --git a/doc/whatsnew/2.12.rst b/doc/whatsnew/2.12.rst index f3b6bc86c..3be3f3c7b 100644 --- a/doc/whatsnew/2.12.rst +++ b/doc/whatsnew/2.12.rst @@ -112,6 +112,8 @@ Other Changes Fixes part of #3688 +* ``self-cls-assignment`` now also considers tuple assignment + * ``undefined-variable`` now correctly triggers for assignment expressions in if ... else statements This includes a basic form of control flow inference for if ... else statements using constant boolean values diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 3ae5957c7..cf11e213c 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -61,7 +61,7 @@ import os import re import sys from functools import lru_cache -from typing import DefaultDict, List, Tuple +from typing import DefaultDict, List, Optional, Set, Tuple import astroid from astroid import nodes @@ -2022,18 +2022,19 @@ class VariablesChecker(BaseChecker): return self._store_type_annotation_node(node.type_annotation) - def _check_self_cls_assign(self, node): + def _check_self_cls_assign(self, node: nodes.Assign) -> None: """Check that self/cls don't get assigned""" - assign_names = { - target.name - for target in node.targets - if isinstance(target, nodes.AssignName) - } + assign_names: Set[Optional[str]] = set() + for target in node.targets: + if isinstance(target, nodes.AssignName): + assign_names.add(target.name) + elif isinstance(target, nodes.Tuple): + assign_names.update( + elt.name for elt in target.elts if isinstance(elt, nodes.AssignName) + ) scope = node.scope() nonlocals_with_same_name = any( - child - for child in scope.body - if isinstance(child, nodes.Nonlocal) and assign_names & set(child.names) + child for child in scope.body if isinstance(child, nodes.Nonlocal) ) if nonlocals_with_same_name: scope = node.scope().parent.scope() @@ -2048,12 +2049,7 @@ class VariablesChecker(BaseChecker): if not argument_names: return self_cls_name = argument_names[0] - target_assign_names = ( - target.name - for target in node.targets - if isinstance(target, nodes.AssignName) - ) - if self_cls_name in target_assign_names: + if self_cls_name in assign_names: self.add_message("self-cls-assignment", node=node, args=(self_cls_name,)) def _check_unpacking(self, inferred, node, targets): diff --git a/tests/functional/s/self/self_cls_assignment.py b/tests/functional/s/self/self_cls_assignment.py index 4e63bb422..e69dafd59 100644 --- a/tests/functional/s/self/self_cls_assignment.py +++ b/tests/functional/s/self/self_cls_assignment.py @@ -14,6 +14,8 @@ class Foo(object): def self_foofoo(self, lala): """Instance method, should warn for self""" self = lala # [self-cls-assignment] + self, var = lala, 1 # [self-cls-assignment] + print(var) @classmethod def cls_foo(cls): diff --git a/tests/functional/s/self/self_cls_assignment.txt b/tests/functional/s/self/self_cls_assignment.txt index eeb2ca230..cedd256e0 100644 --- a/tests/functional/s/self/self_cls_assignment.txt +++ b/tests/functional/s/self/self_cls_assignment.txt @@ -1,4 +1,5 @@ -self-cls-assignment:11:8:Foo.self_foo:Invalid assignment to bar_ in method -self-cls-assignment:16:8:Foo.self_foofoo:Invalid assignment to self in method -self-cls-assignment:21:8:Foo.cls_foo:Invalid assignment to cls in method -self-cls-assignment:44:12:TestNonLocal.function._set_param:Invalid assignment to self in method +self-cls-assignment:11:8:Foo.self_foo:Invalid assignment to bar_ in method:HIGH +self-cls-assignment:16:8:Foo.self_foofoo:Invalid assignment to self in method:HIGH +self-cls-assignment:17:8:Foo.self_foofoo:Invalid assignment to self in method:HIGH +self-cls-assignment:23:8:Foo.cls_foo:Invalid assignment to cls in method:HIGH +self-cls-assignment:46:12:TestNonLocal.function._set_param:Invalid assignment to self in method:HIGH |