From c3623faac692e16a9c39bcc82334795b416fcb55 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Wed, 23 Dec 2020 18:37:28 -0500 Subject: [PATCH] find_sources: deal more robustly against filenames with periods Note that we deal with the possibility of parent_module containing a period in _crawl_up_helper Brought up in #9833 --- mypy/find_sources.py | 5 ++++- mypy/test/test_find_sources.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/mypy/find_sources.py b/mypy/find_sources.py index 47d686cddcbc..e31bd093997d 100644 --- a/mypy/find_sources.py +++ b/mypy/find_sources.py @@ -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 diff --git a/mypy/test/test_find_sources.py b/mypy/test/test_find_sources.py index 5cedec338bbc..10330978b132 100644 --- a/mypy/test/test_find_sources.py +++ b/mypy/test/test_find_sources.py @@ -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