Skip to content

Commit c26ec99

Browse files
committed
Fix 1
1 parent bbee4e4 commit c26ec99

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

mypy/applytype.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ def get_target_type(
3232
context: Context,
3333
skip_unsatisfied: bool,
3434
) -> Type | None:
35+
p_type = get_proper_type(type)
36+
if isinstance(p_type, UninhabitedType) and tvar.has_default():
37+
return tvar.default
3538
if isinstance(tvar, ParamSpecType):
3639
return type
3740
if isinstance(tvar, TypeVarTupleType):
3841
return type
3942
assert isinstance(tvar, TypeVarType)
4043
values = tvar.values
41-
p_type = get_proper_type(type)
4244
if values:
4345
if isinstance(p_type, AnyType):
4446
return type
@@ -103,9 +105,7 @@ def apply_generic_arguments(
103105
target_type = get_target_type(
104106
tvar, type, callable, report_incompatible_typevar_value, context, skip_unsatisfied
105107
)
106-
if isinstance(target_type, UninhabitedType) and tvar.has_default():
107-
id_to_type[tvar.id] = tvar.default
108-
elif target_type is not None:
108+
if target_type is not None:
109109
id_to_type[tvar.id] = target_type
110110

111111
param_spec = callable.param_spec()

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,32 @@ from typing import TypeVar, ParamSpec, List, Union, Callable, Tuple
7777
from typing_extensions import TypeVarTuple, Unpack
7878

7979
T1 = TypeVar("T1", default=str)
80+
T2 = TypeVar("T2", bound=str, default=str)
81+
T3 = TypeVar("T3", bytes, str, default=str)
8082
P1 = ParamSpec("P1", default=(int, str))
8183
Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])
8284

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

85-
def func1(x: Union[int, T1]) -> List[T1]: ...
86-
reveal_type(func1(2)) # N: Revealed type is "builtins.list[builtins.str]"
87-
reveal_type(func1(2.1)) # N: Revealed type is "builtins.list[builtins.float]"
87+
def func_a1(x: Union[int, T1]) -> T1: ...
88+
reveal_type(func_a1(2)) # N: Revealed type is "builtins.str"
89+
reveal_type(func_a1(2.1)) # N: Revealed type is "builtins.float"
8890

89-
def func2(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
91+
def func_a2(x: Union[int, T1]) -> List[T1]: ...
92+
reveal_type(func_a2(2)) # N: Revealed type is "builtins.list[builtins.str]"
93+
reveal_type(func_a2(2.1)) # N: Revealed type is "builtins.list[builtins.float]"
9094

91-
reveal_type(func2(callback1)) # N: Revealed type is "def (x: builtins.str)"
92-
reveal_type(func2(2)) # N: Revealed type is "def (builtins.int, builtins.str)"
95+
def func_a3(x: Union[int, T2]) -> T2: ...
96+
reveal_type(func_a3(2)) # N: Revealed type is "builtins.str"
9397

94-
def func3(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
95-
# reveal_type(func3(callback1)) # Revealed type is "builtins.tuple[str]" # TODO
96-
# reveal_type(func3(2)) # Revealed type is "builtins.tuple[builtins.int, builtins.str]" # TODO
98+
def func_a4(x: Union[int, T3]) -> T3: ...
99+
reveal_type(func_a4(2)) # N: Revealed type is "builtins.str"
100+
101+
def func_b1(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
102+
reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.str)"
103+
reveal_type(func_b1(2)) # N: Revealed type is "def (builtins.int, builtins.str)"
104+
105+
def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
106+
# reveal_type(func_c1(callback1)) # Revealed type is "builtins.tuple[str]" # TODO
107+
# reveal_type(func_c1(2)) # Revealed type is "builtins.tuple[builtins.int, builtins.str]" # TODO
97108
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)