From a0542c1d7a6ce3088da37d696b8fa2f5d67ebd57 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sat, 24 Oct 2020 12:12:23 -0700 Subject: [PATCH] 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 <> --- mypy/modulefinder.py | 8 ++++---- test-data/unit/cmdline.test | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index c5109fa53f3d..576354c5abcb 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -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 diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 541f63d10039..271b7c4f3e68 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -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]