Skip to content

Commit 2d6077e

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

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

mypy/build.py

+7-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,21 @@ 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+
is_pkg = lambda p: (os.path.isfile(os.path.join(p, '__init__.py'))
714+
or os.path.isfile(os.path.join(p, '__init__.pyi')))
711715
cur = os.getcwd()
712716
# Add separator to the end of the path, unless one is already present.
713717
if basename(cur) != '':
714718
cur += os.sep
715719
# 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')))):
720+
while (p and (namespaces_allowed or is_pkg(p))):
719721
dir, base = os.path.split(p)
720722
if not base:
721723
break

0 commit comments

Comments
 (0)