Skip to content

stubtest: check typevar and paramspec #12851

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 3 commits into from
May 26, 2022
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
30 changes: 28 additions & 2 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import re
import sys
import types
import typing
import typing_extensions
import warnings
from functools import singledispatch
from pathlib import Path
Expand Down Expand Up @@ -866,8 +868,32 @@ def verify_overloadedfuncdef(
def verify_typevarexpr(
stub: nodes.TypeVarExpr, runtime: MaybeMissing[Any], object_path: List[str]
) -> Iterator[Error]:
if False:
yield None
if isinstance(runtime, Missing):
# We seem to insert these typevars into NamedTuple stubs, but they
# don't exist at runtime. Just ignore!
if stub.name == "_NT":
return
yield Error(object_path, "is not present at runtime", stub, runtime)
return
if not isinstance(runtime, TypeVar):
yield Error(object_path, "is not a TypeVar", stub, runtime)
return


@verify.register(nodes.ParamSpecExpr)
def verify_paramspecexpr(
stub: nodes.ParamSpecExpr, runtime: MaybeMissing[Any], object_path: List[str]
) -> Iterator[Error]:
if isinstance(runtime, Missing):
yield Error(object_path, "is not present at runtime", stub, runtime)
return
maybe_paramspec_types = (
getattr(typing, "ParamSpec", None), getattr(typing_extensions, "ParamSpec", None)
)
paramspec_types = tuple([t for t in maybe_paramspec_types if t is not None])
if not paramspec_types or not isinstance(runtime, paramspec_types):
yield Error(object_path, "is not a ParamSpec", stub, runtime)
return


def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
Expand Down
34 changes: 32 additions & 2 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ def __getitem__(self, typeargs: Any) -> object: ...
class TypeVar:
def __init__(self, name, covariant: bool = ..., contravariant: bool = ...) -> None: ...

class ParamSpec:
def __init__(self, name: str) -> None: ...

_T = TypeVar("_T")
_T_co = TypeVar("_T_co", covariant=True)
_K = TypeVar("_K")
Expand Down Expand Up @@ -329,8 +332,8 @@ def test_default_value(self) -> Iterator[Case]:
yield Case(
stub="""
from typing import TypeVar
T = TypeVar("T", bound=str)
def f6(text: T = ...) -> None: ...
_T = TypeVar("_T", bound=str)
def f6(text: _T = ...) -> None: ...
""",
runtime="def f6(text = None): pass",
error="f6",
Expand Down Expand Up @@ -1042,6 +1045,33 @@ def foo(self, x: int, y: bytes = ...) -> str: ...
error="X.__init__"
)

@collect_cases
def test_type_var(self) -> Iterator[Case]:
yield Case(
stub="from typing import TypeVar", runtime="from typing import TypeVar", error=None
)
yield Case(
stub="A = TypeVar('A')",
runtime="A = TypeVar('A')",
error=None,
)
yield Case(
stub="B = TypeVar('B')",
runtime="B = 5",
error="B",
)
if sys.version_info >= (3, 10):
yield Case(
stub="from typing import ParamSpec",
runtime="from typing import ParamSpec",
error=None
)
yield Case(
stub="C = ParamSpec('C')",
runtime="C = ParamSpec('C')",
error=None,
)


def remove_color_code(s: str) -> str:
return re.sub("\\x1b.*?m", "", s) # this works!
Expand Down