Skip to content

Adjust is_namespace() to check ModuleSpec.loader #2410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Release date: TBA

Refs pylint-dev/pylint#9442

* Make ``astroid.interpreter._import.util.is_namespace`` only consider modules
with ``submodule_search_locations`` a namespace. This fixes a problems that ``six.moves``
brain was not effective if ``six.moves`` was already imported.

Closes #1107


What's New in astroid 3.1.1?
============================
Expand Down
8 changes: 8 additions & 0 deletions astroid/interpreter/_import/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import sys
from functools import lru_cache
from importlib._bootstrap_external import _NamespacePath

if sys.version_info >= (3, 11):
from importlib.machinery import NamespaceLoader
else:
from importlib._bootstrap_external import _NamespaceLoader as NamespaceLoader
from importlib.util import _find_spec_from_path # type: ignore[attr-defined]

from astroid.const import IS_PYPY
Expand Down Expand Up @@ -101,4 +106,7 @@ def is_namespace(modname: str) -> bool:
found_spec is not None
and found_spec.submodule_search_locations is not None
and found_spec.origin is None
and (
found_spec.loader is None or isinstance(found_spec.loader, NamespaceLoader)
)
)
14 changes: 14 additions & 0 deletions tests/brain/test_six.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def test_attribute_access(self) -> None:
six.moves.urllib_parse #@
six.moves.urllib_error #@
six.moves.urllib.request #@
from six.moves import StringIO
StringIO #@
"""
)
assert isinstance(ast_nodes, list)
Expand Down Expand Up @@ -64,6 +66,18 @@ def test_attribute_access(self) -> None:
self.assertIsInstance(urlretrieve, nodes.FunctionDef)
self.assertEqual(urlretrieve.qname(), "urllib.request.urlretrieve")

StringIO = next(ast_nodes[4].infer())
self.assertIsInstance(StringIO, nodes.ClassDef)
self.assertEqual(StringIO.qname(), "_io.StringIO")
self.assertTrue(StringIO.callable())

def test_attribute_access_with_six_moves_imported(self) -> None:
astroid.MANAGER.clear_cache()
astroid.MANAGER._mod_file_cache.clear()
import six.moves # type: ignore[import] # pylint: disable=import-outside-toplevel,unused-import,redefined-outer-name

self.test_attribute_access()

def test_from_imports(self) -> None:
ast_node = builder.extract_node(
"""
Expand Down
Loading