File tree Expand file tree Collapse file tree 3 files changed +24
-2
lines changed
astroid/interpreter/_import Expand file tree Collapse file tree 3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,11 @@ What's New in astroid 2.12.5?
1212=============================
1313Release date: TBA
1414
15+
16+ * Prevent first-party imports from being resolved to `site-packages`.
17+
18+ Refs PyCQA/pylint#7365
19+
1520* Fix ``astroid.interpreter._import.util.is_namespace()`` incorrectly
1621 returning ``True`` for frozen stdlib modules on PyPy.
1722
Original file line number Diff line number Diff line change 1515
1616@lru_cache (maxsize = 4096 )
1717def is_namespace (modname : str ) -> bool :
18+ from astroid .modutils import ( # pylint: disable=import-outside-toplevel
19+ EXT_LIB_DIRS ,
20+ STD_LIB_DIRS ,
21+ )
22+
23+ STD_AND_EXT_LIB_DIRS = STD_LIB_DIRS .union (EXT_LIB_DIRS )
24+
1825 if modname in sys .builtin_module_names :
1926 return False
2027
@@ -72,8 +79,15 @@ def is_namespace(modname: str) -> bool:
7279 last_submodule_search_locations .append (str (assumed_location ))
7380 continue
7481
75- # Update last_submodule_search_locations
82+ # Update last_submodule_search_locations for next iteration
7683 if found_spec and found_spec .submodule_search_locations :
84+ # But immediately return False if we can detect we are in stdlib
85+ # or external lib (e.g site-packages)
86+ if any (
87+ any (location .startswith (lib_dir ) for lib_dir in STD_AND_EXT_LIB_DIRS )
88+ for location in found_spec .submodule_search_locations
89+ ):
90+ return False
7791 last_submodule_search_locations = found_spec .submodule_search_locations
7892
7993 return (
Original file line number Diff line number Diff line change 1717from astroid .const import IS_JYTHON , IS_PYPY
1818from astroid .exceptions import AstroidBuildingError , AstroidImportError
1919from astroid .interpreter ._import import util
20- from astroid .modutils import is_standard_module
20+ from astroid .modutils import EXT_LIB_DIRS , is_standard_module
2121from astroid .nodes import Const
2222from astroid .nodes .scoped_nodes import ClassDef
2323
@@ -130,6 +130,9 @@ def test_submodule_homonym_with_non_module(self) -> None:
130130 def test_module_is_not_namespace (self ) -> None :
131131 self .assertFalse (util .is_namespace ("tests.testdata.python3.data.all" ))
132132 self .assertFalse (util .is_namespace ("__main__" ))
133+ self .assertFalse (
134+ util .is_namespace (list (EXT_LIB_DIRS )[0 ].rsplit ("/" , maxsplit = 1 )[- 1 ]),
135+ )
133136 self .assertFalse (util .is_namespace ("importlib._bootstrap" ))
134137
135138 def test_module_unexpectedly_missing_spec (self ) -> None :
You can’t perform that action at this time.
0 commit comments