diff --git a/mypy/test/testmodulefinder.py b/mypy/test/testmodulefinder.py index 4f839f641e7b..d26e7c1efe0c 100644 --- a/mypy/test/testmodulefinder.py +++ b/mypy/test/testmodulefinder.py @@ -184,6 +184,13 @@ def test__packages_with_ns(self) -> None: ("ns_pkg_untyped.b.c", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), ("ns_pkg_untyped.a.a_var", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), + # Namespace package without stub package + ("ns_pkg_w_stubs", self.path("ns_pkg_w_stubs")), + ("ns_pkg_w_stubs.typed", self.path("ns_pkg_w_stubs-stubs", "typed", "__init__.pyi")), + ("ns_pkg_w_stubs.typed_inline", + self.path("ns_pkg_w_stubs", "typed_inline", "__init__.py")), + ("ns_pkg_w_stubs.untyped", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), + # Regular package with py.typed ("pkg_typed", self.path("pkg_typed", "__init__.py")), ("pkg_typed.a", self.path("pkg_typed", "a.py")), @@ -239,6 +246,13 @@ def test__packages_without_ns(self) -> None: ("ns_pkg_untyped.b.c", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), ("ns_pkg_untyped.a.a_var", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), + # Namespace package without stub package + ("ns_pkg_w_stubs", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), + ("ns_pkg_w_stubs.typed", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), + ("ns_pkg_w_stubs.typed_inline", + self.path("ns_pkg_w_stubs", "typed_inline", "__init__.py")), + ("ns_pkg_w_stubs.untyped", ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS), + # Regular package with py.typed ("pkg_typed", self.path("pkg_typed", "__init__.py")), ("pkg_typed.a", self.path("pkg_typed", "a.py")), diff --git a/test-data/packages/typedpkg_ns/typedpkg_ns/ns/__init__.py b/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs-stubs/typed/__init__.pyi similarity index 100% rename from test-data/packages/typedpkg_ns/typedpkg_ns/ns/__init__.py rename to test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs-stubs/typed/__init__.pyi diff --git a/test-data/packages/typedpkg_ns/typedpkg_ns/ns/py.typed b/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/typed/__init__.py similarity index 100% rename from test-data/packages/typedpkg_ns/typedpkg_ns/ns/py.typed rename to test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/typed/__init__.py diff --git a/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/typed_inline/__init__.py b/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/typed_inline/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/typed_inline/py.typed b/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/typed_inline/py.typed new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/untyped/__init__.py b/test-data/packages/modulefinder-site-packages/ns_pkg_w_stubs/untyped/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/typedpkg_ns/setup.py b/test-data/packages/typedpkg_ns_a/setup.py similarity index 50% rename from test-data/packages/typedpkg_ns/setup.py rename to test-data/packages/typedpkg_ns_a/setup.py index 9285e89104bb..3dab731cada9 100644 --- a/test-data/packages/typedpkg_ns/setup.py +++ b/test-data/packages/typedpkg_ns_a/setup.py @@ -1,10 +1,10 @@ -from setuptools import setup, find_packages +from setuptools import setup setup( name='typedpkg_namespace.alpha', version='1.0.0', - packages=find_packages(), namespace_packages=['typedpkg_ns'], zip_safe=False, - package_data={'typedpkg_ns.ns': ['py.typed']} + package_data={'typedpkg_ns.a': ['py.typed']}, + packages=['typedpkg_ns.a'], ) diff --git a/test-data/packages/typedpkg_ns/typedpkg_ns/__init__.py b/test-data/packages/typedpkg_ns_a/typedpkg_ns/__init__.py similarity index 100% rename from test-data/packages/typedpkg_ns/typedpkg_ns/__init__.py rename to test-data/packages/typedpkg_ns_a/typedpkg_ns/__init__.py diff --git a/test-data/packages/typedpkg_ns_a/typedpkg_ns/a/__init__.py b/test-data/packages/typedpkg_ns_a/typedpkg_ns/a/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/typedpkg_ns/typedpkg_ns/ns/bbb.py b/test-data/packages/typedpkg_ns_a/typedpkg_ns/a/bbb.py similarity index 100% rename from test-data/packages/typedpkg_ns/typedpkg_ns/ns/bbb.py rename to test-data/packages/typedpkg_ns_a/typedpkg_ns/a/bbb.py diff --git a/test-data/packages/typedpkg_ns_a/typedpkg_ns/a/py.typed b/test-data/packages/typedpkg_ns_a/typedpkg_ns/a/py.typed new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/typedpkg_ns_b-stubs/setup.py b/test-data/packages/typedpkg_ns_b-stubs/setup.py new file mode 100644 index 000000000000..a5d7df83eeea --- /dev/null +++ b/test-data/packages/typedpkg_ns_b-stubs/setup.py @@ -0,0 +1,14 @@ +""" +This setup file installs packages to test mypy's PEP 561 implementation +""" + +from distutils.core import setup + +setup( + name='typedpkg_ns_b-stubs', + author="The mypy team", + version='0.1', + namespace_packages=['typedpkg_ns-stubs'], + package_data={'typedpkg_ns-stubs.b': ['__init__.pyi', 'bbb.pyi']}, + packages=['typedpkg_ns-stubs.b'], +) diff --git a/test-data/packages/typedpkg_ns_b-stubs/typedpkg_ns-stubs/b/__init__.pyi b/test-data/packages/typedpkg_ns_b-stubs/typedpkg_ns-stubs/b/__init__.pyi new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/typedpkg_ns_b-stubs/typedpkg_ns-stubs/b/bbb.pyi b/test-data/packages/typedpkg_ns_b-stubs/typedpkg_ns-stubs/b/bbb.pyi new file mode 100644 index 000000000000..e00e9e52c05f --- /dev/null +++ b/test-data/packages/typedpkg_ns_b-stubs/typedpkg_ns-stubs/b/bbb.pyi @@ -0,0 +1 @@ +def bf(a: bool) -> bool: ... diff --git a/test-data/packages/typedpkg_ns_b/setup.py b/test-data/packages/typedpkg_ns_b/setup.py new file mode 100644 index 000000000000..4f0d0d954a73 --- /dev/null +++ b/test-data/packages/typedpkg_ns_b/setup.py @@ -0,0 +1,10 @@ +from setuptools import setup + +setup( + name='typedpkg_namespace.beta', + version='1.0.0', + namespace_packages=['typedpkg_ns'], + zip_safe=False, + package_data={'typedpkg_ns.b': []}, + packages=['typedpkg_ns.b'], +) diff --git a/test-data/packages/typedpkg_ns_b/typedpkg_ns/__init__.py b/test-data/packages/typedpkg_ns_b/typedpkg_ns/__init__.py new file mode 100644 index 000000000000..3ac255b8a577 --- /dev/null +++ b/test-data/packages/typedpkg_ns_b/typedpkg_ns/__init__.py @@ -0,0 +1,2 @@ +# namespace pkg +__import__("pkg_resources").declare_namespace(__name__) diff --git a/test-data/packages/typedpkg_ns_b/typedpkg_ns/b/__init__.py b/test-data/packages/typedpkg_ns_b/typedpkg_ns/b/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/test-data/packages/typedpkg_ns_b/typedpkg_ns/b/bbb.py b/test-data/packages/typedpkg_ns_b/typedpkg_ns/b/bbb.py new file mode 100644 index 000000000000..f10802daace9 --- /dev/null +++ b/test-data/packages/typedpkg_ns_b/typedpkg_ns/b/bbb.py @@ -0,0 +1,2 @@ +def bf(a): + return not a diff --git a/test-data/unit/pep561.test b/test-data/unit/pep561.test index 033d0c52c5ae..73c222a523f8 100644 --- a/test-data/unit/pep561.test +++ b/test-data/unit/pep561.test @@ -119,10 +119,10 @@ reveal_type(a) testTypedPkgSimpleEditableEgg.py:5: note: Revealed type is "builtins.tuple[builtins.str]" [case testTypedPkgNamespaceImportFrom] -# pkgs: typedpkg, typedpkg_ns +# pkgs: typedpkg, typedpkg_ns_a from typedpkg.pkg.aaa import af -from typedpkg_ns.ns.bbb import bf -from typedpkg_ns.ns.dne import dne +from typedpkg_ns.a.bbb import bf +from typedpkg_ns.a.dne import dne af("abc") bf(False) @@ -132,16 +132,16 @@ af(False) bf(2) dne("abc") [out] -testTypedPkgNamespaceImportFrom.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.ns.dne" +testTypedPkgNamespaceImportFrom.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.a.dne" testTypedPkgNamespaceImportFrom.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports testTypedPkgNamespaceImportFrom.py:10: error: Argument 1 to "af" has incompatible type "bool"; expected "str" testTypedPkgNamespaceImportFrom.py:11: error: Argument 1 to "bf" has incompatible type "int"; expected "bool" [case testTypedPkgNamespaceImportAs] -# pkgs: typedpkg, typedpkg_ns +# pkgs: typedpkg, typedpkg_ns_a import typedpkg.pkg.aaa as nm; af = nm.af -import typedpkg_ns.ns.bbb as am; bf = am.bf -from typedpkg_ns.ns.dne import dne +import typedpkg_ns.a.bbb as am; bf = am.bf +from typedpkg_ns.a.dne import dne af("abc") bf(False) @@ -151,16 +151,16 @@ af(False) bf(2) dne("abc") [out] -testTypedPkgNamespaceImportAs.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.ns.dne" +testTypedPkgNamespaceImportAs.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.a.dne" testTypedPkgNamespaceImportAs.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports testTypedPkgNamespaceImportAs.py:10: error: Argument 1 has incompatible type "bool"; expected "str" testTypedPkgNamespaceImportAs.py:11: error: Argument 1 has incompatible type "int"; expected "bool" [case testTypedPkgNamespaceRegImport] -# pkgs: typedpkg, typedpkg_ns +# pkgs: typedpkg, typedpkg_ns_a import typedpkg.pkg.aaa; af = typedpkg.pkg.aaa.af -import typedpkg_ns.ns.bbb; bf = typedpkg_ns.ns.bbb.bf -from typedpkg_ns.ns.dne import dne +import typedpkg_ns.a.bbb; bf = typedpkg_ns.a.bbb.bf +from typedpkg_ns.a.dne import dne af("abc") bf(False) @@ -171,7 +171,7 @@ bf(2) dne("abc") [out] -testTypedPkgNamespaceRegImport.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.ns.dne" +testTypedPkgNamespaceRegImport.py:4: error: Cannot find implementation or library stub for module named "typedpkg_ns.a.dne" testTypedPkgNamespaceRegImport.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports testTypedPkgNamespaceRegImport.py:10: error: Argument 1 has incompatible type "bool"; expected "str" testTypedPkgNamespaceRegImport.py:11: error: Argument 1 has incompatible type "int"; expected "bool" @@ -188,9 +188,37 @@ import a a.py:1: error: Unsupported operand types for + ("int" and "str") [case testTypedPkgNamespaceRegFromImportTwice] -# pkgs: typedpkg_ns -from typedpkg_ns import ns +# pkgs: typedpkg_ns_a +from typedpkg_ns import a -- dummy should trigger a second iteration [file dummy.py.2] [out] [out2] + +[case testNamespacePkgWStubs] +# pkgs: typedpkg_ns_a, typedpkg_ns_b, typedpkg_ns_b-stubs +# flags: --no-namespace-packages +import typedpkg_ns.a.bbb as a +import typedpkg_ns.b.bbb as b +a.bf(False) +b.bf(False) +a.bf(1) +b.bf(1) +[out] +testNamespacePkgWStubs.py:4: error: Skipping analyzing "typedpkg_ns.b.bbb": found module but no type hints or library stubs +testNamespacePkgWStubs.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +testNamespacePkgWStubs.py:4: error: Skipping analyzing "typedpkg_ns.b": found module but no type hints or library stubs +testNamespacePkgWStubs.py:7: error: Argument 1 to "bf" has incompatible type "int"; expected "bool" + +[case testNamespacePkgWStubsWithNamespacePackagesFlag] +# pkgs: typedpkg_ns_a, typedpkg_ns_b, typedpkg_ns_b-stubs +# flags: --namespace-packages +import typedpkg_ns.a.bbb as a +import typedpkg_ns.b.bbb as b +a.bf(False) +b.bf(False) +a.bf(1) +b.bf(1) +[out] +testNamespacePkgWStubsWithNamespacePackagesFlag.py:7: error: Argument 1 to "bf" has incompatible type "int"; expected "bool" +testNamespacePkgWStubsWithNamespacePackagesFlag.py:8: error: Argument 1 to "bf" has incompatible type "int"; expected "bool"