10
10
from collections .abc import Iterator
11
11
from contextlib import contextmanager
12
12
13
- import pkg_resources
14
-
15
13
import astroid
16
14
from astroid import manager , test_utils
17
15
from astroid .const import IS_JYTHON
18
16
from astroid .exceptions import AstroidBuildingError , AstroidImportError
17
+ from astroid .interpreter ._import import util
19
18
from astroid .modutils import is_standard_module
20
19
from astroid .nodes import Const
21
20
from astroid .nodes .scoped_nodes import ClassDef
@@ -111,6 +110,16 @@ def test_ast_from_namespace_pkgutil(self) -> None:
111
110
def test_ast_from_namespace_pkg_resources (self ) -> None :
112
111
self ._test_ast_from_old_namespace_package_protocol ("pkg_resources" )
113
112
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
+
114
123
def test_implicit_namespace_package (self ) -> None :
115
124
data_dir = os .path .dirname (resources .find ("data/namespace_pep_420" ))
116
125
contribute = os .path .join (data_dir , "contribute_to_namespace" )
@@ -131,7 +140,6 @@ def test_implicit_namespace_package(self) -> None:
131
140
def test_namespace_package_pth_support (self ) -> None :
132
141
pth = "foogle_fax-0.12.5-py2.7-nspkg.pth"
133
142
site .addpackage (resources .RESOURCE_PATH , pth , [])
134
- pkg_resources ._namespace_packages ["foogle" ] = []
135
143
136
144
try :
137
145
module = self .manager .ast_from_module_name ("foogle.fax" )
@@ -141,18 +149,14 @@ def test_namespace_package_pth_support(self) -> None:
141
149
with self .assertRaises (AstroidImportError ):
142
150
self .manager .ast_from_module_name ("foogle.moogle" )
143
151
finally :
144
- del pkg_resources ._namespace_packages ["foogle" ]
145
152
sys .modules .pop ("foogle" )
146
153
147
154
def test_nested_namespace_import (self ) -> None :
148
155
pth = "foogle_fax-0.12.5-py2.7-nspkg.pth"
149
156
site .addpackage (resources .RESOURCE_PATH , pth , [])
150
- pkg_resources ._namespace_packages ["foogle" ] = ["foogle.crank" ]
151
- pkg_resources ._namespace_packages ["foogle.crank" ] = []
152
157
try :
153
158
self .manager .ast_from_module_name ("foogle.crank" )
154
159
finally :
155
- del pkg_resources ._namespace_packages ["foogle" ]
156
160
sys .modules .pop ("foogle" )
157
161
158
162
def test_namespace_and_file_mismatch (self ) -> None :
@@ -161,12 +165,10 @@ def test_namespace_and_file_mismatch(self) -> None:
161
165
self .assertEqual (ast .name , "unittest" )
162
166
pth = "foogle_fax-0.12.5-py2.7-nspkg.pth"
163
167
site .addpackage (resources .RESOURCE_PATH , pth , [])
164
- pkg_resources ._namespace_packages ["foogle" ] = []
165
168
try :
166
169
with self .assertRaises (AstroidImportError ):
167
170
self .manager .ast_from_module_name ("unittest.foogle.fax" )
168
171
finally :
169
- del pkg_resources ._namespace_packages ["foogle" ]
170
172
sys .modules .pop ("foogle" )
171
173
172
174
def _test_ast_from_zip (self , archive : str ) -> None :
@@ -323,6 +325,7 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
323
325
lrus = (
324
326
astroid .nodes .node_classes .LookupMixIn .lookup ,
325
327
astroid .modutils ._cache_normalize_path_ ,
328
+ util .is_namespace ,
326
329
astroid .interpreter .objectmodel .ObjectModel .attributes ,
327
330
)
328
331
@@ -332,6 +335,7 @@ def test_clear_cache_clears_other_lru_caches(self) -> None:
332
335
# Generate some hits and misses
333
336
ClassDef ().lookup ("garbage" )
334
337
is_standard_module ("unittest" , std_path = ["garbage_path" ])
338
+ util .is_namespace ("unittest" )
335
339
astroid .interpreter .objectmodel .ObjectModel ().attributes ()
336
340
337
341
# Did the hits or misses actually happen?
0 commit comments