Skip to content

Commit e6f5cb4

Browse files
committed
Apply TypeVar defaults (functions)
1 parent a6ee900 commit e6f5cb4

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

mypy/applytype.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
TypeVarLikeType,
1818
TypeVarTupleType,
1919
TypeVarType,
20+
UninhabitedType,
2021
UnpackType,
2122
get_proper_type,
2223
)
@@ -31,13 +32,15 @@ def get_target_type(
3132
context: Context,
3233
skip_unsatisfied: bool,
3334
) -> Type | None:
35+
p_type = get_proper_type(type)
36+
if isinstance(p_type, UninhabitedType) and tvar.has_default():
37+
return tvar.default
3438
if isinstance(tvar, ParamSpecType):
3539
return type
3640
if isinstance(tvar, TypeVarTupleType):
3741
return type
3842
assert isinstance(tvar, TypeVarType)
3943
values = tvar.values
40-
p_type = get_proper_type(type)
4144
if values:
4245
if isinstance(p_type, AnyType):
4346
return type

test-data/unit/check-typevar-defaults.test

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,38 @@ Ts1 = TypeVarTuple("Ts1", default=2) # E: The default argument to TypeVarTuple m
7272
Ts2 = TypeVarTuple("Ts2", default=int) # E: The default argument to TypeVarTuple must be an Unpacked tuple
7373
Ts3 = TypeVarTuple("Ts3", default=Tuple[int]) # E: The default argument to TypeVarTuple must be an Unpacked tuple
7474
[builtins fixtures/tuple.pyi]
75+
76+
[case testTypeVarDefaultsFunctions]
77+
from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple
78+
from typing_extensions import TypeVarTuple, Unpack
79+
80+
T1 = TypeVar("T1", default=str)
81+
T2 = TypeVar("T2", bound=str, default=str)
82+
T3 = TypeVar("T3", bytes, str, default=str)
83+
P1 = ParamSpec("P1", default=[int, str])
84+
Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])
85+
86+
def callback1(x: str) -> None: ...
87+
88+
def func_a1(x: Union[int, T1]) -> T1: ...
89+
reveal_type(func_a1(2)) # N: Revealed type is "builtins.str"
90+
reveal_type(func_a1(2.1)) # N: Revealed type is "builtins.float"
91+
92+
def func_a2(x: Union[int, T1]) -> List[T1]: ...
93+
reveal_type(func_a2(2)) # N: Revealed type is "builtins.list[builtins.str]"
94+
reveal_type(func_a2(2.1)) # N: Revealed type is "builtins.list[builtins.float]"
95+
96+
def func_a3(x: Union[int, T2]) -> T2: ...
97+
reveal_type(func_a3(2)) # N: Revealed type is "builtins.str"
98+
99+
def func_a4(x: Union[int, T3]) -> T3: ...
100+
reveal_type(func_a4(2)) # N: Revealed type is "builtins.str"
101+
102+
def func_b1(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
103+
reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.str)"
104+
reveal_type(func_b1(2)) # N: Revealed type is "def (builtins.int, builtins.str)"
105+
106+
def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
107+
# reveal_type(func_c1(callback1)) # Revealed type is "builtins.tuple[str]" # TODO
108+
# reveal_type(func_c1(2)) # Revealed type is "builtins.tuple[builtins.int, builtins.str]" # TODO
109+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)