Skip to content

Commit 3c27c47

Browse files
Fix no-name-in-module when variable is same as module name (#8169) (#8226)
Co-authored-by: Pierre Sassoulas <[email protected]> (cherry picked from commit f690448) Co-authored-by: Dani Alcala <[email protected]>
1 parent f48ec66 commit 3c27c47

File tree

7 files changed

+23
-1
lines changed

7 files changed

+23
-1
lines changed

doc/whatsnew/fragments/8148.bugfix

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix ``no-name-in-module`` false positive raised when a package defines a variable with the
2+
same name as one of its submodules.
3+
4+
Closes #8148

pylint/checkers/variables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,7 @@ def _check_module_attrs(
29332933
break
29342934
try:
29352935
module = next(module.getattr(name)[0].infer())
2936-
if module is astroid.Uninferable:
2936+
if not isinstance(module, nodes.Module):
29372937
return None
29382938
except astroid.NotFoundError:
29392939
if module.name in self._ignored_modules:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
base = [
2+
'Exchange',
3+
'Precise',
4+
'exchanges',
5+
'decimal_to_precision',
6+
]

tests/regrtest_data/pkg_mod_imports/base/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class SomeError(Exception):
2+
pass
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from pkg_mod_imports.base.errors import SomeError

tests/test_self.py

+9
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,15 @@ def test_output_no_header(self) -> None:
12931293
args, expected_output=expected, unexpected_output=not_expected
12941294
)
12951295

1296+
def test_no_name_in_module(self) -> None:
1297+
"""Test that a package with both a variable name `base` and a module `base`
1298+
does not emit a no-name-in-module msg."""
1299+
module = join(HERE, "regrtest_data", "test_no_name_in_module.py")
1300+
unexpected = "No name 'errors' in module 'list' (no-name-in-module)"
1301+
self._test_output(
1302+
[module, "-E"], expected_output="", unexpected_output=unexpected
1303+
)
1304+
12961305

12971306
class TestCallbackOptions:
12981307
"""Test for all callback options we support."""

0 commit comments

Comments
 (0)