File tree Expand file tree Collapse file tree 2 files changed +20
-2
lines changed
astroid/interpreter/_import Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -34,8 +34,17 @@ def is_namespace(modname: str) -> bool:
34
34
working_modname , path = last_submodule_search_locations
35
35
)
36
36
except ValueError :
37
- # Assume it's a .pth file, unless it's __main__
38
- return modname != "__main__"
37
+ if modname == "__main__" :
38
+ return False
39
+ try :
40
+ # .pth files will be on sys.modules
41
+ return sys .modules [modname ].__spec__ is None
42
+ except KeyError :
43
+ return False
44
+ except AttributeError :
45
+ # Workaround for "py" module
46
+ # https://github.com/pytest-dev/apipkg/issues/13
47
+ return False
39
48
except KeyError :
40
49
# Intermediate steps might raise KeyErrors
41
50
# https://github.com/python/cpython/issues/93334
Original file line number Diff line number Diff line change @@ -129,6 +129,15 @@ def test_module_is_not_namespace(self) -> None:
129
129
self .assertFalse (util .is_namespace ("tests.testdata.python3.data.all" ))
130
130
self .assertFalse (util .is_namespace ("__main__" ))
131
131
132
+ def test_module_unexpectedly_missing_spec (self ) -> None :
133
+ astroid_module = sys .modules ["astroid" ]
134
+ original_spec = astroid_module .__spec__
135
+ del astroid_module .__spec__
136
+ try :
137
+ self .assertFalse (util .is_namespace ("astroid" ))
138
+ finally :
139
+ astroid_module .__spec__ = original_spec
140
+
132
141
def test_implicit_namespace_package (self ) -> None :
133
142
data_dir = os .path .dirname (resources .find ("data/namespace_pep_420" ))
134
143
contribute = os .path .join (data_dir , "contribute_to_namespace" )
You can’t perform that action at this time.
0 commit comments