Skip to content

Apply TypeVar defaults to functions (PEP 696) #15387

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 1 commit into from
Aug 12, 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
5 changes: 4 additions & 1 deletion mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
TypeVarLikeType,
TypeVarTupleType,
TypeVarType,
UninhabitedType,
UnpackType,
get_proper_type,
)
Expand All @@ -32,13 +33,15 @@ def get_target_type(
context: Context,
skip_unsatisfied: bool,
) -> Type | None:
p_type = get_proper_type(type)
if isinstance(p_type, UninhabitedType) and tvar.has_default():
return tvar.default
if isinstance(tvar, ParamSpecType):
return type
if isinstance(tvar, TypeVarTupleType):
return type
assert isinstance(tvar, TypeVarType)
values = tvar.values
p_type = get_proper_type(type)
if values:
if isinstance(p_type, AnyType):
return type
Expand Down
35 changes: 35 additions & 0 deletions test-data/unit/check-typevar-defaults.test
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,38 @@ T2 = TypeVar("T2", bound=List[str], default=List[int]) # E: TypeVar default mus
T3 = TypeVar("T3", int, str, default=bytes) # E: TypeVar default must be one of the constraint types
T4 = TypeVar("T4", int, str, default=Union[int, str]) # E: TypeVar default must be one of the constraint types
T5 = TypeVar("T5", float, str, default=int) # E: TypeVar default must be one of the constraint types

[case testTypeVarDefaultsFunctions]
from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple
from typing_extensions import TypeVarTuple, Unpack

T1 = TypeVar("T1", default=str)
T2 = TypeVar("T2", bound=str, default=str)
T3 = TypeVar("T3", bytes, str, default=str)
P1 = ParamSpec("P1", default=[int, str])
Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])

def callback1(x: str) -> None: ...

def func_a1(x: Union[int, T1]) -> T1: ...
reveal_type(func_a1(2)) # N: Revealed type is "builtins.str"
reveal_type(func_a1(2.1)) # N: Revealed type is "builtins.float"

def func_a2(x: Union[int, T1]) -> List[T1]: ...
reveal_type(func_a2(2)) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(func_a2(2.1)) # N: Revealed type is "builtins.list[builtins.float]"

def func_a3(x: Union[int, T2]) -> T2: ...
reveal_type(func_a3(2)) # N: Revealed type is "builtins.str"

def func_a4(x: Union[int, T3]) -> T3: ...
reveal_type(func_a4(2)) # N: Revealed type is "builtins.str"

def func_b1(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.str)"
reveal_type(func_b1(2)) # N: Revealed type is "def (builtins.int, builtins.str)"

def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
# reveal_type(func_c1(callback1)) # Revealed type is "builtins.tuple[str]" # TODO
# reveal_type(func_c1(2)) # Revealed type is "builtins.tuple[builtins.int, builtins.str]" # TODO
[builtins fixtures/tuple.pyi]