diff --git a/doc/whatsnew/fragments/10652.false_positive b/doc/whatsnew/fragments/10652.false_positive new file mode 100644 index 0000000000..a5a7319a26 --- /dev/null +++ b/doc/whatsnew/fragments/10652.false_positive @@ -0,0 +1,3 @@ +Fix false positive for ``invalid-name`` on a partially uninferable module-level constant. + +Closes #10652 diff --git a/pylint/checkers/base/name_checker/checker.py b/pylint/checkers/base/name_checker/checker.py index c2f2655282..3cb1c336da 100644 --- a/pylint/checkers/base/name_checker/checker.py +++ b/pylint/checkers/base/name_checker/checker.py @@ -520,9 +520,14 @@ def visit_assignname( # pylint: disable=too-many-branches,too-many-statements self._check_name("const", node.name, node) else: node_type = "variable" + iattrs = tuple(node.frame().igetattr(node.name)) if ( - (iattrs := tuple(node.frame().igetattr(node.name))) - and util.Uninferable not in iattrs + util.Uninferable in iattrs + and self._name_regexps["const"].match(node.name) is not None + ): + return + if ( + util.Uninferable not in iattrs and len(iattrs) > 1 and all( astroid.are_exclusive(*combo) diff --git a/tests/functional/i/invalid/invalid_name/invalid_name_module_level.py b/tests/functional/i/invalid/invalid_name/invalid_name_module_level.py index 9012cea417..9eae16b7c1 100644 --- a/tests/functional/i/invalid/invalid_name/invalid_name_module_level.py +++ b/tests/functional/i/invalid/invalid_name/invalid_name_module_level.py @@ -1,5 +1,5 @@ """Tests for invalid name for names declared at module level""" -# pylint: disable=missing-class-docstring, too-few-public-methods, missing-function-docstring +# pylint: disable=missing-class-docstring, too-few-public-methods, missing-function-docstring, wrong-import-position import collections @@ -42,3 +42,12 @@ def A(): # [invalid-name] other_const = [2] else: other_const = [3] + + +from importlib.metadata import PackageNotFoundError +from importlib.metadata import version + +try: + VERSION = version("ty") # uninferable +except PackageNotFoundError: + VERSION = "0.0.0"