From c26ec99749318eba767a8660e000df2cab25b8da Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 12 Mar 2023 11:54:29 +0100 Subject: [PATCH] Fix 1 --- mypy/applytype.py | 8 +++--- test-data/unit/check-typevar-defaults.test | 29 +++++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/mypy/applytype.py b/mypy/applytype.py index 11b9a48287842..957df98c894ef 100644 --- a/mypy/applytype.py +++ b/mypy/applytype.py @@ -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 @@ -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() diff --git a/test-data/unit/check-typevar-defaults.test b/test-data/unit/check-typevar-defaults.test index 4d3474896cf37..c3d10286eb3a8 100644 --- a/test-data/unit/check-typevar-defaults.test +++ b/test-data/unit/check-typevar-defaults.test @@ -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]