Skip to content

Commit a4996b4

Browse files
committed
Check if the ModuleSpec origin is None to figure out if it is a namespace package
ModuleSpec.origin was changed in python/cpython#5481 to be None for namespace packages.
1 parent be874a9 commit a4996b4

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

astroid/interpreter/_import/spec.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,12 @@ class PathSpecFinder(Finder):
160160
def find_module(self, modname, module_parts, processed, submodule_path):
161161
spec = importlib.machinery.PathFinder.find_spec(modname, path=submodule_path)
162162
if spec:
163-
location = spec.origin if spec.origin != 'namespace' else None
164-
module_type = ModuleType.PY_NAMESPACE if spec.origin == 'namespace' else None
163+
# origin can be either a string on older Python versions
164+
# or None in case it is a namespace package:
165+
# https://github.com/python/cpython/pull/5481
166+
is_namespace_pkg = spec.origin in ('namespace', None)
167+
location = spec.origin if not is_namespace_pkg else None
168+
module_type = ModuleType.PY_NAMESPACE if is_namespace_pkg else None
165169
spec = ModuleSpec(name=spec.name, location=location,
166170
origin=spec.origin, module_type=module_type,
167171
submodule_search_locations=list(spec.submodule_search_locations

0 commit comments

Comments
 (0)