Skip to content

Commit

Permalink
Fix 1
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p committed Mar 12, 2023
1 parent bbee4e4 commit c26ec99
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
8 changes: 4 additions & 4 deletions mypy/applytype.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,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 Expand Up @@ -103,9 +105,7 @@ def apply_generic_arguments(
target_type = get_target_type(
tvar, type, callable, report_incompatible_typevar_value, context, skip_unsatisfied
)
if isinstance(target_type, UninhabitedType) and tvar.has_default():
id_to_type[tvar.id] = tvar.default
elif target_type is not None:
if target_type is not None:
id_to_type[tvar.id] = target_type

param_spec = callable.param_spec()
Expand Down
29 changes: 20 additions & 9 deletions test-data/unit/check-typevar-defaults.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,32 @@ 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 func1(x: Union[int, T1]) -> List[T1]: ...
reveal_type(func1(2)) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(func1(2.1)) # N: Revealed type is "builtins.list[builtins.float]"
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 func2(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
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]"

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

def func3(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
# reveal_type(func3(callback1)) # Revealed type is "builtins.tuple[str]" # TODO
# reveal_type(func3(2)) # Revealed type is "builtins.tuple[builtins.int, builtins.str]" # TODO
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]

0 comments on commit c26ec99

Please sign in to comment.