Skip to content

Commit a0542c1

Browse files
authored
modulefinder: make -p handle namespace packages correctly (#9616)
Fixes part of #5759 The other part is passing files arguments, which we make steps towards in #9614 Co-authored-by: hauntsaninja <>
1 parent 161ee24 commit a0542c1

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

mypy/modulefinder.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,13 @@ def find_modules_recursive(self, module: str) -> List[BuildSource]:
387387
if mod not in hits:
388388
hits.add(mod)
389389
result += self.find_modules_recursive(module + '.' + mod)
390-
elif os.path.isdir(module_path) and module in self.ns_packages:
391-
# Even more subtler: handle recursive decent into PEP 420
390+
elif os.path.isdir(module_path):
391+
# Even subtler: handle recursive decent into PEP 420
392392
# namespace packages that are explicitly listed on the command
393393
# line with -p/--packages.
394394
for item in sorted(self.fscache.listdir(module_path)):
395-
if os.path.isdir(os.path.join(module_path, item)):
396-
result += self.find_modules_recursive(module + '.' + item)
395+
item, _ = os.path.splitext(item)
396+
result += self.find_modules_recursive(module + '.' + item)
397397
return result
398398

399399

test-data/unit/cmdline.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,17 @@ def bar(a: int, b: int) -> str:
809809
[out]
810810
src/anamespace/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")
811811

812+
[case testNestedPEP420Packages]
813+
# cmd: mypy -p bottles --namespace-packages
814+
[file bottles/jars/secret/glitter.py]
815+
x = 0 # type: str
816+
[file bottles/jars/sprinkle.py]
817+
from bottles.jars.secret.glitter import x
818+
x + 1
819+
[out]
820+
bottles/jars/secret/glitter.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
821+
bottles/jars/sprinkle.py:2: error: Unsupported operand types for + ("str" and "int")
822+
812823
[case testFollowImportStubs1]
813824
# cmd: mypy main.py
814825
[file mypy.ini]

0 commit comments

Comments
 (0)