Skip to content
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

Improve is_namespace() check for modules where __spec__ is None #1802

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ Release date: TBA

Refs PyCQA/pylint#5151

* Improve detection of namespace packages for the modules with ``__spec__`` set to None.

Closes PyCQA/pylint#7488.


What's New in astroid 2.12.11?
==============================
Expand Down
5 changes: 4 additions & 1 deletion astroid/interpreter/_import/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ def is_namespace(modname: str) -> bool:
# Check first fragment of modname, e.g. "astroid", not "astroid.interpreter"
# because of cffi's behavior
# See: https://github.com/PyCQA/astroid/issues/1776
mod = sys.modules[processed_components[0]]
return (
sys.modules[processed_components[0]].__spec__ is None
mod.__spec__ is None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we even need to check mod.__spec__ is None, but I don't fully understand all different styles of namespace packages. The test does pass even if we remove this.

and getattr(mod, "__file__", None) is None
and hasattr(mod, "__path__")
and not IS_PYPY
)
except KeyError:
Expand Down
9 changes: 9 additions & 0 deletions tests/unittest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ def test_module_unexpectedly_missing_spec(self) -> None:
finally:
astroid_module.__spec__ = original_spec

def test_module_unexpectedly_spec_is_none(self) -> None:
astroid_module = sys.modules["astroid"]
original_spec = astroid_module.__spec__
astroid_module.__spec__ = None
try:
self.assertFalse(util.is_namespace("astroid"))
finally:
astroid_module.__spec__ = original_spec

def test_implicit_namespace_package(self) -> None:
data_dir = os.path.dirname(resources.find("data/namespace_pep_420"))
contribute = os.path.join(data_dir, "contribute_to_namespace")
Expand Down