Skip to content

Commit

Permalink
self-assigning-variable skips class level assignments.
Browse files Browse the repository at this point in the history
Close #2930
  • Loading branch information
PCManticore committed Sep 26, 2019
1 parent 4d0fbec commit d8fae01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Expand Up @@ -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?
===========================
Expand Down
8 changes: 8 additions & 0 deletions pylint/checkers/base.py
Expand Up @@ -1495,6 +1495,10 @@ def visit_with(self, node):

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):
Expand All @@ -1518,6 +1522,10 @@ def _check_self_assigning_variable(self, node):
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
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/s/self_assigning_variable.py
Expand Up @@ -18,3 +18,10 @@ class Class:
FIRST = Class()
FIRST = FIRST # [self-assigning-variable]
FIRST, SECOND = FIRST, CLS.FIRST # [self-assigning-variable]


FOO = 1


class RedefinedModuleLevel:
FOO = FOO

0 comments on commit d8fae01

Please sign in to comment.