Skip to content

Commit

Permalink
find_sources: deal more robustly against filenames with periods
Browse files Browse the repository at this point in the history
Note that we deal with the possibility of parent_module containing a
period in _crawl_up_helper

Brought up in python#9833
  • Loading branch information
hauntsaninja committed Dec 23, 2020
1 parent dc25178 commit c3623fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/find_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ def crawl_up(self, path: str) -> Tuple[str, str]:
if module_name == "__init__":
return parent_module, base_dir

# Note that module_name might not actually be a valid identifier, but that's okay
# Note that module_name might not actually be a valid identifier, but that's okay (once
# we've removed periods from module_name)
# Ignoring this possibility sidesteps some search path confusion
module_name = module_name.replace(".", "-")

module = module_join(parent_module, module_name)
return module, base_dir

Expand Down
6 changes: 6 additions & 0 deletions mypy/test/test_find_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ def test_crawl_namespace_multi_dir(self) -> None:
assert crawl(finder, "/a/pkg/a.py") == ("pkg.a", "/a")
assert crawl(finder, "/b/pkg/b.py") == ("pkg.b", "/b")

def test_crawl_invalid_module(self) -> None:
options = Options()

finder = SourceFinder(FakeFSCache({"/dot.dot.py"}), options)
assert crawl(finder, "/dot.dot.py") == ("dot-dot", "/")

def test_find_sources_no_namespace(self) -> None:
options = Options()
options.namespace_packages = False
Expand Down

0 comments on commit c3623fa

Please sign in to comment.