From d8fae0124b60abb40d833e78fb32fe355143889b Mon Sep 17 00:00:00 2001 From: Claudiu Popa Date: Thu, 26 Sep 2019 08:29:08 +0200 Subject: [PATCH] ``self-assigning-variable`` skips class level assignments. Close #2930 --- ChangeLog | 4 ++++ pylint/checkers/base.py | 8 ++++++++ tests/functional/s/self_assigning_variable.py | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/ChangeLog b/ChangeLog index 13966b9c3d..0b3686ac95 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 9f4b8d053d..bea58016d7 100644 --- a/pylint/checkers/base.py +++ b/pylint/checkers/base.py @@ -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): @@ -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 diff --git a/tests/functional/s/self_assigning_variable.py b/tests/functional/s/self_assigning_variable.py index 2c5148f63e..6df2647e6b 100644 --- a/tests/functional/s/self_assigning_variable.py +++ b/tests/functional/s/self_assigning_variable.py @@ -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