summaryrefslogtreecommitdiff
path: root/pylint
diff options
context:
space:
mode:
authorDaniƫl van Noord <13665637+DanielNoord@users.noreply.github.com>2021-11-07 18:12:08 +0200
committerGitHub <noreply@github.com>2021-11-07 18:12:08 +0200
commit6827dfe84e793a4fc9a07afc3a463ded6e22b230 (patch)
tree06965ca42cdf02f3abc5c103094d853a767cd462 /pylint
parent0a1ebd488fcdf7bf306615aba29e401ce49e3e10 (diff)
downloadpylint-git-6827dfe84e793a4fc9a07afc3a463ded6e22b230.tar.gz
Make ``self-cls-assignment`` check tuple assignment (#5268)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'pylint')
-rw-r--r--pylint/checkers/variables.py28
1 files changed, 12 insertions, 16 deletions
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):