1010from collections .abc import Iterator
1111from contextlib import contextmanager
1212
13- import pkg_resources
14-
1513import astroid
1614from astroid import manager , test_utils
1715from astroid .const import IS_JYTHON
1816from astroid .exceptions import AstroidBuildingError , AstroidImportError
17+ from astroid .interpreter ._import import util
1918from astroid .modutils import is_standard_module
2019from astroid .nodes import Const
2120from astroid .nodes .scoped_nodes import ClassDef
@@ -111,6 +110,16 @@ def test_ast_from_namespace_pkgutil(self) -> None:
111110 def test_ast_from_namespace_pkg_resources (self ) -> None :
112111 self ._test_ast_from_old_namespace_package_protocol ("pkg_resources" )
113112
113+ def test_identify_old_namespace_package_protocol (self ) -> None :
114+ # Like the above cases, this package follows the old namespace package protocol
115+ # astroid currently assumes such packages are in sys.modules, so import it
116+ # pylint: disable-next=import-outside-toplevel
117+ import tests .testdata .python3 .data .path_pkg_resources_1 .package .foo as _ # noqa
118+
119+ self .assertTrue (
120+ util .is_namespace ("tests.testdata.python3.data.path_pkg_resources_1" )
121+ )
122+
114123 def test_implicit_namespace_package (self ) -> None :
115124 data_dir = os .path .dirname (resources .find ("data/namespace_pep_420" ))
116125 contribute = os .path .join (data_dir , "contribute_to_namespace" )
@@ -131,7 +140,6 @@ def test_implicit_namespace_package(self) -> None:
131140 def test_namespace_package_pth_support (self ) -> None :
132141 pth = "foogle_fax-0.12.5-py2.7-nspkg.pth"
133142 site .addpackage (resources .RESOURCE_PATH , pth , [])
134- pkg_resources ._namespace_packages ["foogle" ] = []
135143
136144 try :
137145 module = self .manager .ast_from_module_name ("foogle.fax" )
@@ -141,18 +149,14 @@ def test_namespace_package_pth_support(self) -> None:
141149 with self .assertRaises (AstroidImportError ):
142150 self .manager .ast_from_module_name ("foogle.moogle" )
143151 finally :
144- del pkg_resources ._namespace_packages ["foogle" ]
145152 sys .modules .pop ("foogle" )
146153
147154 def test_nested_namespace_import (self ) -> None :
148155 pth = "foogle_fax-0.12.5-py2.7-nspkg.pth"
149156 site .addpackage (resources .RESOURCE_PATH , pth , [])
150- pkg_resources ._namespace_packages ["foogle" ] = ["foogle.crank" ]
151- pkg_resources ._namespace_packages ["foogle.crank" ] = []
152157 try :
153158 self .manager .ast_from_module_name ("foogle.crank" )
154159 finally :
155- del pkg_resources ._namespace_packages ["foogle" ]
156160 sys .modules .pop ("foogle" )
157161
158162 def test_namespace_and_file_mismatch (self ) -> None :
@@ -161,12 +165,10 @@ def test_namespace_and_file_mismatch(self) -> None:
161165 self .assertEqual (ast .name , "unittest" )
162166 pth = "foogle_fax-0.12.5-py2.7-nspkg.pth"
163167 site .addpackage (resources .RESOURCE_PATH , pth , [])
164- pkg_resources ._namespace_packages ["foogle" ] = []
165168 try :
166169 with self .assertRaises (AstroidImportError ):
167170 self .manager .ast_from_module_name ("unittest.foogle.fax" )
168171 finally :
169- del pkg_resources ._namespace_packages ["foogle" ]
170172 sys .modules .pop ("foogle" )
171173
172174 def _test_ast_from_zip (self , archive : str ) -> None :
@@ -323,6 +325,7 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
323325 lrus = (
324326 astroid .nodes .node_classes .LookupMixIn .lookup ,
325327 astroid .modutils ._cache_normalize_path_ ,
328+ util .is_namespace ,
326329 astroid .interpreter .objectmodel .ObjectModel .attributes ,
327330 )
328331
@@ -332,6 +335,7 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
332335 # Generate some hits and misses
333336 ClassDef ().lookup ("garbage" )
334337 is_standard_module ("unittest" , std_path = ["garbage_path" ])
338+ util .is_namespace ("unittest" )
335339 astroid .interpreter .objectmodel .ObjectModel ().attributes ()
336340
337341 # Did the hits or misses actually happen?
0 commit comments