Skip to content

Commit caf95d5

Browse files
[Backport maintenance/4.0.x] Fix FP for invalid-name for partially uninferable module-level name (#10687)
Fix FP for `invalid-name` for partially uninferable module-level name (#10678) (cherry picked from commit 7ecfd9f) Co-authored-by: Jacob Walls <[email protected]>
1 parent 4d52769 commit caf95d5

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false positive for ``invalid-name`` on a partially uninferable module-level constant.
2+
3+
Closes #10652

pylint/checkers/base/name_checker/checker.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,14 @@ def visit_assignname( # pylint: disable=too-many-branches,too-many-statements
520520
self._check_name("const", node.name, node)
521521
else:
522522
node_type = "variable"
523+
iattrs = tuple(node.frame().igetattr(node.name))
523524
if (
524-
(iattrs := tuple(node.frame().igetattr(node.name)))
525-
and util.Uninferable not in iattrs
525+
util.Uninferable in iattrs
526+
and self._name_regexps["const"].match(node.name) is not None
527+
):
528+
return
529+
if (
530+
util.Uninferable not in iattrs
526531
and len(iattrs) > 1
527532
and all(
528533
astroid.are_exclusive(*combo)

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Tests for invalid name for names declared at module level"""
2-
# pylint: disable=missing-class-docstring, too-few-public-methods, missing-function-docstring
2+
# pylint: disable=missing-class-docstring, too-few-public-methods, missing-function-docstring, wrong-import-position
33

44
import collections
55

@@ -42,3 +42,12 @@ def A(): # [invalid-name]
4242
other_const = [2]
4343
else:
4444
other_const = [3]
45+
46+
47+
from importlib.metadata import PackageNotFoundError
48+
from importlib.metadata import version
49+
50+
try:
51+
VERSION = version("ty") # uninferable
52+
except PackageNotFoundError:
53+
VERSION = "0.0.0"

0 commit comments

Comments
 (0)