diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-09-26 08:29:08 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-09-26 08:29:08 +0200 |
commit | d8fae0124b60abb40d833e78fb32fe355143889b (patch) | |
tree | 6b247bba560ec2a7045aa5ab5f6991cbe82e6fac | |
parent | 4d0fbec72d5c55591a9c35fa7a1bfdaa03be0e61 (diff) | |
download | pylint-git-d8fae0124b60abb40d833e78fb32fe355143889b.tar.gz |
``self-assigning-variable`` skips class level assignments.
Close #2930
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | pylint/checkers/base.py | 8 | ||||
-rw-r--r-- | tests/functional/s/self_assigning_variable.py | 7 |
3 files changed, 19 insertions, 0 deletions
@@ -10,6 +10,10 @@ Release date: TBA * ``ignored-modules`` can skip submodules. Close #3135 +* ``self-assigning-variable`` skips class level assignments. + + Close #2930 + What's New in Pylint 2.4.1? =========================== diff --git a/pylint/checkers/base.py b/pylint/checkers/base.py index 9f4b8d053..bea58016d 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -1495,6 +1495,10 @@ class BasicChecker(_BasicChecker): def _check_self_assigning_variable(self, node): # Detect assigning to the same variable. + + scope = node.scope() + scope_locals = scope.locals + rhs_names = [] targets = node.targets if isinstance(targets[0], astroid.Tuple): @@ -1518,6 +1522,10 @@ class BasicChecker(_BasicChecker): continue if not isinstance(target, astroid.AssignName): continue + if isinstance(scope, astroid.ClassDef) and target.name in scope_locals: + # Check that the scope is different than a class level, which is usually + # a pattern to expose module level attributes as class level ones. + continue if target.name == lhs_name.name: self.add_message( "self-assigning-variable", args=(target.name,), node=target diff --git a/tests/functional/s/self_assigning_variable.py b/tests/functional/s/self_assigning_variable.py index 2c5148f63..6df2647e6 100644 --- a/tests/functional/s/self_assigning_variable.py +++ b/tests/functional/s/self_assigning_variable.py @@ -18,3 +18,10 @@ CLS.FIRST = FIRST FIRST = Class() FIRST = FIRST # [self-assigning-variable] FIRST, SECOND = FIRST, CLS.FIRST # [self-assigning-variable] + + +FOO = 1 + + +class RedefinedModuleLevel: + FOO = FOO |