Skip to content

Commit

Permalink
modulefinder: make -p handle namespace packages correctly (#9616)
Browse files Browse the repository at this point in the history
Fixes part of #5759
The other part is passing files arguments, which we make steps towards
in #9614

Co-authored-by: hauntsaninja <>
  • Loading branch information
hauntsaninja authored Oct 24, 2020
1 parent 161ee24 commit a0542c1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,13 @@ def find_modules_recursive(self, module: str) -> List[BuildSource]:
if mod not in hits:
hits.add(mod)
result += self.find_modules_recursive(module + '.' + mod)
elif os.path.isdir(module_path) and module in self.ns_packages:
# Even more subtler: handle recursive decent into PEP 420
elif os.path.isdir(module_path):
# Even subtler: handle recursive decent into PEP 420
# namespace packages that are explicitly listed on the command
# line with -p/--packages.
for item in sorted(self.fscache.listdir(module_path)):
if os.path.isdir(os.path.join(module_path, item)):
result += self.find_modules_recursive(module + '.' + item)
item, _ = os.path.splitext(item)
result += self.find_modules_recursive(module + '.' + item)
return result


Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,17 @@ def bar(a: int, b: int) -> str:
[out]
src/anamespace/foo/bar.py:2: error: Incompatible return value type (got "int", expected "str")

[case testNestedPEP420Packages]
# cmd: mypy -p bottles --namespace-packages
[file bottles/jars/secret/glitter.py]
x = 0 # type: str
[file bottles/jars/sprinkle.py]
from bottles.jars.secret.glitter import x
x + 1
[out]
bottles/jars/secret/glitter.py:1: error: Incompatible types in assignment (expression has type "int", variable has type "str")
bottles/jars/sprinkle.py:2: error: Unsupported operand types for + ("str" and "int")

[case testFollowImportStubs1]
# cmd: mypy main.py
[file mypy.ini]
Expand Down

0 comments on commit a0542c1

Please sign in to comment.