Skip to content

Commit 586fc5a

Browse files
author
Guido van Rossum
committed
Tentative solution for namespace packages
1 parent b7926f2 commit 586fc5a

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

mypy/modulefinder.py

+29-21
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,35 @@ def _find_module(self, id: str) -> Optional[str]:
170170
# Now just look for 'baz.pyi', 'baz/__init__.py', etc., inside those directories.
171171
seplast = os.sep + components[-1] # so e.g. '/baz'
172172
sepinit = os.sep + '__init__'
173-
for base_dir, verify in candidate_base_dirs:
174-
base_path = base_dir + seplast # so e.g. '/usr/lib/python3.4/foo/bar/baz'
175-
# Prefer package over module, i.e. baz/__init__.py* over baz.py*.
176-
for extension in PYTHON_EXTENSIONS:
177-
path = base_path + sepinit + extension
178-
path_stubs = base_path + '-stubs' + sepinit + extension
179-
if fscache.isfile_case(path):
180-
if verify and not verify_module(fscache, id, path):
181-
continue
182-
return path
183-
elif fscache.isfile_case(path_stubs):
184-
if verify and not verify_module(fscache, id, path_stubs):
185-
continue
186-
return path_stubs
187-
# No package, look for module.
188-
for extension in PYTHON_EXTENSIONS:
189-
path = base_path + extension
190-
if fscache.isfile_case(path):
191-
if verify and not verify_module(fscache, id, path):
192-
continue
193-
return path
173+
verify_flags = [True]
174+
if self.options is not None and self.options.namespace_packages:
175+
verify_flags.append(False)
176+
# If --namespace-packages, we do the whole thing twice:
177+
# - once with classic rules (verify if requested)
178+
# - once looking for namespace packages (never verify)
179+
for verify_flag in verify_flags:
180+
for base_dir, verify in candidate_base_dirs:
181+
verify = verify and verify_flag
182+
base_path = base_dir + seplast # so e.g. '/usr/lib/python3.4/foo/bar/baz'
183+
# Prefer package over module, i.e. baz/__init__.py* over baz.py*.
184+
for extension in PYTHON_EXTENSIONS:
185+
path = base_path + sepinit + extension
186+
path_stubs = base_path + '-stubs' + sepinit + extension
187+
if fscache.isfile_case(path):
188+
if verify and not verify_module(fscache, id, path):
189+
continue
190+
return path
191+
elif fscache.isfile_case(path_stubs):
192+
if verify and not verify_module(fscache, id, path_stubs):
193+
continue
194+
return path_stubs
195+
# No package, look for module.
196+
for extension in PYTHON_EXTENSIONS:
197+
path = base_path + extension
198+
if fscache.isfile_case(path):
199+
if verify and not verify_module(fscache, id, path):
200+
continue
201+
return path
194202
return None
195203

196204
def find_modules_recursive(self, module: str) -> List[BuildSource]:

0 commit comments

Comments
 (0)