Skip to content

Commit

Permalink
Fix no-name-in-module when variable is same as module name (#8169) (#…
Browse files Browse the repository at this point in the history
…8226)

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
(cherry picked from commit f690448)

Co-authored-by: Dani Alcala <112832187+clavedeluna@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and clavedeluna authored Feb 7, 2023
1 parent f48ec66 commit 3c27c47
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/8148.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix ``no-name-in-module`` false positive raised when a package defines a variable with the
same name as one of its submodules.

Closes #8148
2 changes: 1 addition & 1 deletion pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,7 @@ def _check_module_attrs(
break
try:
module = next(module.getattr(name)[0].infer())
if module is astroid.Uninferable:
if not isinstance(module, nodes.Module):
return None
except astroid.NotFoundError:
if module.name in self._ignored_modules:
Expand Down
6 changes: 6 additions & 0 deletions tests/regrtest_data/pkg_mod_imports/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
base = [
'Exchange',
'Precise',
'exchanges',
'decimal_to_precision',
]
Empty file.
2 changes: 2 additions & 0 deletions tests/regrtest_data/pkg_mod_imports/base/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class SomeError(Exception):
pass
1 change: 1 addition & 0 deletions tests/regrtest_data/test_no_name_in_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pkg_mod_imports.base.errors import SomeError
9 changes: 9 additions & 0 deletions tests/test_self.py
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,15 @@ def test_output_no_header(self) -> None:
args, expected_output=expected, unexpected_output=not_expected
)

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


class TestCallbackOptions:
"""Test for all callback options we support."""
Expand Down

0 comments on commit 3c27c47

Please sign in to comment.