-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Labels
Description
- Are you reporting a bug, or opening a feature request? Bug
Only reproducible on Windows b/c of different path separator. On a windows machine, issue:
> mkdir foo bar
> touch foo\__init__.py foo\spam.py bar\__init__.py bar\eggs.py
> stubgen foo\spam.py bar\eggs.py
Result: stubgen
never finishes, because caught in the endless while loop: out\foo/
doesn't start with out\bar/
, then doesn't start with out/
, then with /
repeatedly.
Lines 245 to 249 in 4e021d9
while True: | |
path = os.path.dirname(path) | |
if (cur + '/').startswith(path + '/'): | |
cur = path | |
break |
Since mypy
supports only Python 3.5 and onwards anyway - may I suggest using pathlib
instead of string manipulation? With pathlib
, the task of finding the common parent directory is a walk in the park and works on all OSes. For example, the common_dir_prefix
method could look like this:
from pathlib import Path
def common_dir_prefix(paths: List[str]) -> str:
if not paths:
return '.'
cur = Path(paths[0])
for parent in cur.parents:
# if the parent is parent of all paths, we're good to go
if all(parent in Path(path).parents for path in paths[1:]):
return str(parent)
return '.'
In addition, if an extensive path manipulation is done in mypy
(didn't check that), I would also plead switching to pathlib
exclusively.