Skip to content

Commit 657b386

Browse files
[Backport maintenance/4.0.x] [invalid-name] Fix FP for exclusive assignment of objects (#10754)
[invalid-name] Fix FP for exclusive assignment of objects (#10746) (cherry picked from commit d308554) Co-authored-by: Jacob Walls <jacobtylerwalls@gmail.com>
1 parent 03f8a92 commit 657b386

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

custom_dict.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ asynccontextmanager
2424
attr
2525
attrib
2626
attrname
27+
attrs
2728
backport
2829
BaseChecker
2930
basename
@@ -144,6 +145,7 @@ gv
144145
hashable
145146
hmac
146147
html
148+
iattrs
147149
idgeneratormixin
148150
ifexpr
149151
igetattr
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix a false positive for ``invalid-name`` on an UPPER_CASED name inside an
2+
``if`` branch that assigns an object.
3+
4+
Closes #10745

pylint/checkers/base/name_checker/checker.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,12 @@ def visit_assignname( # pylint: disable=too-many-branches,too-many-statements
518518
and self._name_regexps["const"].match(node.name) is not None
519519
):
520520
return
521-
if (
522-
util.Uninferable not in iattrs
523-
and len(iattrs) > 1
524-
and all(
525-
astroid.are_exclusive(*combo)
526-
for combo in itertools.combinations(iattrs, 2)
527-
)
521+
# Do the exclusive assignment analysis on attrs, not iattrs.
522+
# iattrs locations could be anywhere (inference result).
523+
attrs = tuple(node.frame().getattr(node.name))
524+
if len(attrs) > 1 and all(
525+
astroid.are_exclusive(*combo)
526+
for combo in itertools.combinations(attrs, 2)
528527
):
529528
node_type = "const"
530529
if not self._meets_exception_for_non_consts(

tests/functional/i/invalid/invalid_name/invalid_name_module_level.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ def A(): # [invalid-name]
4444
other_const = [3]
4545

4646

47+
if CONST:
48+
ANOTHER_CONST = A()
49+
else:
50+
ANOTHER_CONST = 5
51+
52+
4753
from importlib.metadata import PackageNotFoundError
4854
from importlib.metadata import version
4955

0 commit comments

Comments
 (0)