diff --git a/mypy/fscache.py b/mypy/fscache.py index 0f60dc7644999..c77b941c551ca 100644 --- a/mypy/fscache.py +++ b/mypy/fscache.py @@ -107,6 +107,9 @@ def init_under_package_root(self, path: str) -> bool: dirname, basename = os.path.split(path) if basename != '__init__.py': return False + if not os.path.basename(dirname).isidentifier(): + # Can't put an __init__.py in a place that's not an identifier + return False try: st = self.stat(dirname) except OSError: diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 92ef7e0690ed0..3dbcfdd826fc7 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -946,7 +946,13 @@ emarg/foo.py:1: error: Name "fail" is not defined emarg/hatch/villip/mankangulisk.py:1: error: Name "fail" is not defined [case testPackageRootEmpty] -# cmd: mypy --package-root= a/b/c.py main.py +# cmd: mypy --no-namespace-packages --package-root= a/b/c.py main.py +[file a/b/c.py] +[file main.py] +import a.b.c + +[case testPackageRootEmptyNamespacePackage] +# cmd: mypy --namespace-packages --package-root= a/b/c.py main.py [file a/b/c.py] [file main.py] import a.b.c