Skip to content

Commit ea64ec0

Browse files
committed
Fix lib_path discovery when using -p with namespaces
1 parent c84a63c commit ea64ec0

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

mypy/build.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,9 @@ def build(sources: List[BuildSource],
174174
for source in sources:
175175
if source.path:
176176
# Include directory of the program file in the module search path.
177-
dir = remove_cwd_prefix_from_path(dirname(source.path))
177+
dir = remove_cwd_prefix_from_path(
178+
dirname(source.path), namespaces_allowed=options.namespace_packages
179+
)
178180
if dir not in lib_path:
179181
lib_path.insert(0, dir)
180182

@@ -711,21 +713,23 @@ def stats_summary(self) -> Mapping[str, object]:
711713
return self.stats
712714

713715

714-
def remove_cwd_prefix_from_path(p: str) -> str:
716+
def remove_cwd_prefix_from_path(p: str, namespaces_allowed: bool) -> str:
715717
"""Remove current working directory prefix from p, if present.
716718
717719
Also crawl up until a directory without __init__.py is found.
718720
719721
If the result would be empty, return '.' instead.
720722
"""
723+
def is_pkg(p: str) -> bool:
724+
return (os.path.isfile(os.path.join(p, '__init__.py'))
725+
or os.path.isfile(os.path.join(p, '__init__.pyi')))
726+
721727
cur = os.getcwd()
722728
# Add separator to the end of the path, unless one is already present.
723729
if basename(cur) != '':
724730
cur += os.sep
725731
# Compute root path.
726-
while (p and
727-
(os.path.isfile(os.path.join(p, '__init__.py')) or
728-
os.path.isfile(os.path.join(p, '__init__.pyi')))):
732+
while (p and (namespaces_allowed or is_pkg(p))):
729733
dir, base = os.path.split(p)
730734
if not base:
731735
break

0 commit comments

Comments
 (0)