Skip to content
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

Correctly handle runtime type applications of variadic types #16240

Merged
merged 2 commits into from
Oct 15, 2023
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
34 changes: 29 additions & 5 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from mypy.maptype import map_instance_to_supertype
from mypy.meet import is_overlapping_types, narrow_declared_type
from mypy.message_registry import ErrorMessage
from mypy.messages import MessageBuilder
from mypy.messages import MessageBuilder, format_type
from mypy.nodes import (
ARG_NAMED,
ARG_POS,
Expand Down Expand Up @@ -116,10 +116,12 @@
from mypy.type_visitor import TypeTranslator
from mypy.typeanal import (
check_for_explicit_any,
fix_instance,
has_any_from_unimported_type,
instantiate_type_alias,
make_optional_type,
set_any_tvars,
validate_instance,
)
from mypy.typeops import (
callable_type,
Expand Down Expand Up @@ -166,10 +168,12 @@
TypeVarLikeType,
TypeVarTupleType,
TypeVarType,
UnboundType,
UninhabitedType,
UnionType,
UnpackType,
find_unpack_in_list,
flatten_nested_tuples,
flatten_nested_unions,
get_proper_type,
get_proper_types,
Expand Down Expand Up @@ -4637,15 +4641,35 @@ class C(Generic[T, Unpack[Ts]]): ...
similar to how it is done in other places using split_with_prefix_and_suffix().
"""
vars = t.variables
args = flatten_nested_tuples(args)

# TODO: this logic is duplicated with semanal_typeargs.
for tv, arg in zip(t.variables, args):
if isinstance(tv, ParamSpecType):
if not isinstance(
get_proper_type(arg), (Parameters, ParamSpecType, AnyType, UnboundType)
):
self.chk.fail(
"Can only replace ParamSpec with a parameter types list or"
f" another ParamSpec, got {format_type(arg, self.chk.options)}",
ctx,
)
return [AnyType(TypeOfAny.from_error)] * len(vars)

if not vars or not any(isinstance(v, TypeVarTupleType) for v in vars):
return list(args)
assert t.is_type_obj()
info = t.type_object()
# We reuse the logic from semanal phase to reduce code duplication.
fake = Instance(info, args, line=ctx.line, column=ctx.column)
if not validate_instance(fake, self.chk.fail):
fix_instance(
fake, self.chk.fail, self.chk.note, disallow_any=False, options=self.chk.options
)
args = list(fake.args)

prefix = next(i for (i, v) in enumerate(vars) if isinstance(v, TypeVarTupleType))
suffix = len(vars) - prefix - 1
if len(args) < len(vars) - 1:
self.msg.incompatible_type_application(len(vars), len(args), ctx)
return [AnyType(TypeOfAny.from_error)] * len(vars)

tvt = vars[prefix]
assert isinstance(tvt, TypeVarTupleType)
start, middle, end = split_with_prefix_and_suffix(tuple(args), prefix, suffix)
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,19 @@ g(cb, y='a', x=0) # E: Argument "y" to "g" has incompatible type "str"; expecte
# E: Argument "x" to "g" has incompatible type "int"; expected "str"
[builtins fixtures/paramspec.pyi]

[case testParamSpecBadRuntimeTypeApplication]
from typing import ParamSpec, TypeVar, Generic, Callable

R = TypeVar("R")
P = ParamSpec("P")
class C(Generic[P, R]):
x: Callable[P, R]

bad = C[int, str]() # E: Can only replace ParamSpec with a parameter types list or another ParamSpec, got "int"
reveal_type(bad) # N: Revealed type is "__main__.C[Any, Any]"
reveal_type(bad.x) # N: Revealed type is "def (*Any, **Any) -> Any"
[builtins fixtures/paramspec.pyi]

[case testParamSpecNoCrashOnUnificationAlias]
import mod
[file mod.pyi]
Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1845,3 +1845,23 @@ def foo2(func: Callable[[Unpack[Args]], T], *args: Unpack[Args2]) -> T:
def foo3(func: Callable[[int, Unpack[Args2]], T], *args: Unpack[Args2]) -> T:
return submit(func, 1, *args)
[builtins fixtures/tuple.pyi]

[case testTypeVarTupleRuntimeTypeApplication]
from typing import Generic, TypeVar, Tuple
from typing_extensions import Unpack, TypeVarTuple

T = TypeVar("T")
S = TypeVar("S")
Ts = TypeVarTuple("Ts")
class C(Generic[T, Unpack[Ts], S]): ...

Ints = Tuple[int, int]
x = C[Unpack[Ints]]()
reveal_type(x) # N: Revealed type is "__main__.C[builtins.int, builtins.int]"

y = C[Unpack[Tuple[int, ...]]]()
reveal_type(y) # N: Revealed type is "__main__.C[builtins.int, Unpack[builtins.tuple[builtins.int, ...]], builtins.int]"

z = C[int]() # E: Bad number of arguments, expected: at least 2, given: 1
reveal_type(z) # N: Revealed type is "__main__.C[Any, Unpack[builtins.tuple[Any, ...]], Any]"
[builtins fixtures/tuple.pyi]