Skip to content

Add Pep561 tests for stub-only package with namespace packages #11141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mypy/test/testmodulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down Expand Up @@ -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")),
Expand Down
Original file line number Diff line number Diff line change
@@ -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'],
)
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions test-data/packages/typedpkg_ns_b-stubs/setup.py
Original file line number Diff line number Diff line change
@@ -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'],
)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def bf(a: bool) -> bool: ...
10 changes: 10 additions & 0 deletions test-data/packages/typedpkg_ns_b/setup.py
Original file line number Diff line number Diff line change
@@ -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'],
)
2 changes: 2 additions & 0 deletions test-data/packages/typedpkg_ns_b/typedpkg_ns/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# namespace pkg
__import__("pkg_resources").declare_namespace(__name__)
Empty file.
2 changes: 2 additions & 0 deletions test-data/packages/typedpkg_ns_b/typedpkg_ns/b/bbb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def bf(a):
return not a
56 changes: 42 additions & 14 deletions test-data/unit/pep561.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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"