Skip to content

Commit 5bb5579

Browse files
committed
Fix lib_path discovery when using -p with namespaces
1 parent 004cc0f commit 5bb5579

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

mypy/build.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ def build(sources: List[BuildSource],
168168
for source in sources:
169169
if source.path:
170170
# Include directory of the program file in the module search path.
171-
dir = remove_cwd_prefix_from_path(dirname(source.path))
171+
dir = remove_cwd_prefix_from_path(
172+
dirname(source.path), namespaces_allowed=options.namespace_packages
173+
)
172174
if dir not in lib_path:
173175
lib_path.insert(0, dir)
174176

@@ -701,21 +703,23 @@ def stats_summary(self) -> Mapping[str, object]:
701703
return self.stats
702704

703705

704-
def remove_cwd_prefix_from_path(p: str) -> str:
706+
def remove_cwd_prefix_from_path(p: str, namespaces_allowed: bool) -> str:
705707
"""Remove current working directory prefix from p, if present.
706708
707709
Also crawl up until a directory without __init__.py is found.
708710
709711
If the result would be empty, return '.' instead.
710712
"""
713+
def is_pkg(p):
714+
return (os.path.isfile(os.path.join(p, '__init__.py'))
715+
or os.path.isfile(os.path.join(p, '__init__.pyi')))
716+
711717
cur = os.getcwd()
712718
# Add separator to the end of the path, unless one is already present.
713719
if basename(cur) != '':
714720
cur += os.sep
715721
# Compute root path.
716-
while (p and
717-
(os.path.isfile(os.path.join(p, '__init__.py')) or
718-
os.path.isfile(os.path.join(p, '__init__.pyi')))):
722+
while (p and (namespaces_allowed or is_pkg(p))):
719723
dir, base = os.path.split(p)
720724
if not base:
721725
break

0 commit comments

Comments
 (0)