Skip to content

Commit 82f767a

Browse files
authored
Disallow invalid identifiers from getting implicit bazel __init__.py (#11268)
This helps unblock #9636. With namespace packages, the code to crawl upward would not stop anywhere and would run into temp directories with invalid module names. Normally it would stop when it found a dir without an __init__.py, but it was placing an implicit fake bazel __init__ there. Bazeled environments can't really have invalid module name directories, so this should help fix. Added tests for namespace-packages and no-namespace-packages
1 parent b75d935 commit 82f767a

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

mypy/fscache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def init_under_package_root(self, path: str) -> bool:
107107
dirname, basename = os.path.split(path)
108108
if basename != '__init__.py':
109109
return False
110+
if not os.path.basename(dirname).isidentifier():
111+
# Can't put an __init__.py in a place that's not an identifier
112+
return False
110113
try:
111114
st = self.stat(dirname)
112115
except OSError:

test-data/unit/cmdline.test

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,13 @@ emarg/foo.py:1: error: Name "fail" is not defined
946946
emarg/hatch/villip/mankangulisk.py:1: error: Name "fail" is not defined
947947

948948
[case testPackageRootEmpty]
949-
# cmd: mypy --package-root= a/b/c.py main.py
949+
# cmd: mypy --no-namespace-packages --package-root= a/b/c.py main.py
950+
[file a/b/c.py]
951+
[file main.py]
952+
import a.b.c
953+
954+
[case testPackageRootEmptyNamespacePackage]
955+
# cmd: mypy --namespace-packages --package-root= a/b/c.py main.py
950956
[file a/b/c.py]
951957
[file main.py]
952958
import a.b.c

0 commit comments

Comments
 (0)