diff options
| author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-08-27 16:51:33 -0400 |
|---|---|---|
| committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-08-29 09:13:38 +0200 |
| commit | ef55fd3cd477ebe3c693b4b9eb15f52b56449b55 (patch) | |
| tree | f6eefa9e487843d7defbeb1cd7dc7bc163f808c0 | |
| parent | 7fba17d69b033a5aace1d7b1aed7887a8ef2c4b4 (diff) | |
| download | astroid-git-ef55fd3cd477ebe3c693b4b9eb15f52b56449b55.tar.gz | |
Fix namespace package detection for frozen stdlib modules on PyPy (#1757)
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/interpreter/_import/util.py | 4 | ||||
| -rw-r--r-- | tests/unittest_manager.py | 13 |
3 files changed, 19 insertions, 2 deletions
@@ -12,6 +12,10 @@ What's New in astroid 2.12.5? ============================= Release date: TBA +* Fix ``astroid.interpreter._import.util.is_namespace()`` incorrectly + returning ``True`` for frozen stdlib modules on PyPy. + + Closes #1755 What's New in astroid 2.12.4? diff --git a/astroid/interpreter/_import/util.py b/astroid/interpreter/_import/util.py index f082e9c4..3bdf0470 100644 --- a/astroid/interpreter/_import/util.py +++ b/astroid/interpreter/_import/util.py @@ -10,6 +10,8 @@ from functools import lru_cache from importlib._bootstrap_external import _NamespacePath from importlib.util import _find_spec_from_path # type: ignore[attr-defined] +from astroid.const import IS_PYPY + @lru_cache(maxsize=4096) def is_namespace(modname: str) -> bool: @@ -38,7 +40,7 @@ def is_namespace(modname: str) -> bool: return False try: # .pth files will be on sys.modules - return sys.modules[modname].__spec__ is None + return sys.modules[modname].__spec__ is None and not IS_PYPY except KeyError: return False except AttributeError: diff --git a/tests/unittest_manager.py b/tests/unittest_manager.py index 9cf02aff..79f21db1 100644 --- a/tests/unittest_manager.py +++ b/tests/unittest_manager.py @@ -10,9 +10,11 @@ import unittest from collections.abc import Iterator from contextlib import contextmanager +import pytest + import astroid from astroid import manager, test_utils -from astroid.const import IS_JYTHON +from astroid.const import IS_JYTHON, IS_PYPY from astroid.exceptions import AstroidBuildingError, AstroidImportError from astroid.interpreter._import import util from astroid.modutils import is_standard_module @@ -128,6 +130,7 @@ class AstroidManagerTest( def test_module_is_not_namespace(self) -> None: self.assertFalse(util.is_namespace("tests.testdata.python3.data.all")) self.assertFalse(util.is_namespace("__main__")) + self.assertFalse(util.is_namespace("importlib._bootstrap")) def test_module_unexpectedly_missing_spec(self) -> None: astroid_module = sys.modules["astroid"] @@ -155,6 +158,10 @@ class AstroidManagerTest( for _ in range(2): sys.path.pop(0) + @pytest.mark.skipif( + IS_PYPY, + reason="PyPy provides no way to tell apart frozen stdlib from old-style namespace packages", + ) def test_namespace_package_pth_support(self) -> None: pth = "foogle_fax-0.12.5-py2.7-nspkg.pth" site.addpackage(resources.RESOURCE_PATH, pth, []) @@ -169,6 +176,10 @@ class AstroidManagerTest( finally: sys.modules.pop("foogle") + @pytest.mark.skipif( + IS_PYPY, + reason="PyPy provides no way to tell apart frozen stdlib from old-style namespace packages", + ) def test_nested_namespace_import(self) -> None: pth = "foogle_fax-0.12.5-py2.7-nspkg.pth" site.addpackage(resources.RESOURCE_PATH, pth, []) |
