@@ -159,7 +159,7 @@ T3 = TypeVar("T3", bytes, str, default=str)
159159P1 = ParamSpec("P1", default=[int, str])
160160Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])
161161
162- def callback1(x: str ) -> None: ...
162+ def callback1(x: int ) -> None: ...
163163
164164def func_a1(x: Union[int, T1]) -> T1: ...
165165reveal_type(func_a1(2)) # N: Revealed type is "builtins.str"
@@ -175,15 +175,112 @@ reveal_type(func_a3(2)) # N: Revealed type is "builtins.str"
175175def func_a4(x: Union[int, T3]) -> T3: ...
176176reveal_type(func_a4(2)) # N: Revealed type is "builtins.str"
177177
178+ def func_a5(x: Union[int, Callable[[T1], None]]) -> Callable[[T1], None]: ...
179+ reveal_type(func_a5(callback1)) # N: Revealed type is "def (builtins.int)"
180+ reveal_type(func_a5(2)) # N: Revealed type is "def (builtins.str)"
181+
178182def func_b1(x: Union[int, Callable[P1, None]]) -> Callable[P1, None]: ...
179- reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.str )"
183+ reveal_type(func_b1(callback1)) # N: Revealed type is "def (x: builtins.int )"
180184reveal_type(func_b1(2)) # N: Revealed type is "def (builtins.int, builtins.str)"
181185
182186def func_c1(x: Union[int, Callable[[Unpack[Ts1]], None]]) -> Tuple[Unpack[Ts1]]: ...
183- # reveal_type(func_c1(callback1)) # Revealed type is "Tuple[str ]" # TODO
187+ # reveal_type(func_c1(callback1)) # Revealed type is "Tuple[int ]" # TODO
184188reveal_type(func_c1(2)) # N: Revealed type is "tuple[builtins.int, builtins.str]"
185189[builtins fixtures/tuple.pyi]
186190
191+ [case testTypeVarDefaultsClassInit]
192+ from typing import Generic, TypeVar, ParamSpec, List, Union, Callable, Tuple
193+ from typing_extensions import TypeVarTuple, Unpack
194+
195+ T1 = TypeVar("T1", default=str)
196+ T2 = TypeVar("T2", bound=str, default=str)
197+ T3 = TypeVar("T3", bytes, str, default=str)
198+ P1 = ParamSpec("P1", default=[int, str])
199+ Ts1 = TypeVarTuple("Ts1", default=Unpack[Tuple[int, str]])
200+
201+ def callback1(x: int) -> None: ...
202+
203+ class A1(Generic[T1]):
204+ def __init__(self, x: Union[int, T1]) -> None: ...
205+
206+ reveal_type(A1(2)) # N: Revealed type is "__main__.A1[builtins.str]"
207+ reveal_type(A1(2.1)) # N: Revealed type is "__main__.A1[builtins.float]"
208+
209+ class A2(Generic[T2]):
210+ def __init__(self, x: Union[int, T2]) -> None: ...
211+ reveal_type(A2(2)) # N: Revealed type is "__main__.A2[builtins.str]"
212+
213+ class A3(Generic[T3]):
214+ def __init__(self, x: Union[int, T3]) -> None: ...
215+ reveal_type(A3(2)) # N: Revealed type is "__main__.A3[builtins.str]"
216+
217+ class A4(Generic[T2]):
218+ def __init__(self, x: Union[int, Callable[[T1], None]]) -> None: ...
219+ reveal_type(A4(callback1)) # N: Revealed type is "__main__.A4[builtins.str]"
220+ reveal_type(A4(2)) # N: Revealed type is "__main__.A4[builtins.str]"
221+
222+ class B1(Generic[P1]):
223+ def __init__(self, x: Union[int, Callable[P1, None]]) -> None: ...
224+ reveal_type(B1(callback1)) # N: Revealed type is "__main__.B1[[x: builtins.int]]"
225+ reveal_type(B1(2)) # N: Revealed type is "__main__.B1[[builtins.int, builtins.str]]"
226+
227+ class C1(Generic[Unpack[Ts1]]):
228+ def __init__(self, x: Union[int, Callable[[Unpack[Ts1]], None]]) -> None: ...
229+ reveal_type(C1(callback1)) # Revealed type is "Tuple[int]" # TODO # N: Revealed type is "__main__.C1[builtins.int]"
230+ reveal_type(C1(2)) # N: Revealed type is "__main__.C1[builtins.int, builtins.str]"
231+ [builtins fixtures/tuple.pyi]
232+
233+ [case testTypeVarDefaultsInTypeApplications]
234+ from typing import Generic
235+ from typing_extensions import TypeVar
236+
237+ T1 = TypeVar("T1")
238+ T2 = TypeVar("T2", default=T1)
239+
240+ class MyClass(Generic[T1, T2]): ...
241+
242+ reveal_type(MyClass[int]) # N: Revealed type is "def () -> __main__.MyClass[builtins.int, builtins.int]"
243+
244+ # `T2` now defaults to `int` for `MyClass` from now on
245+ reveal_type(MyClass[str]) # N: Revealed type is "def () -> __main__.MyClass[builtins.str, builtins.str]"
246+ reveal_type(MyClass[bytes]) # N: Revealed type is "def () -> __main__.MyClass[builtins.bytes, builtins.bytes]"
247+
248+ c_int: type[MyClass[int]]
249+ c_str: type[MyClass[str]]
250+ a_str = MyClass[str]()
251+
252+ reveal_type(c_int) # N: Revealed type is "type[__main__.MyClass[builtins.int, builtins.int]]"
253+ reveal_type(c_str) # N: Revealed type is "type[__main__.MyClass[builtins.str, builtins.str]]"
254+ reveal_type(a_str) # N: Revealed type is "__main__.MyClass[builtins.str, builtins.str]"
255+
256+ class MyClass2(Generic[T1, T2]):
257+ def __init__(self, t: T2) -> None: ...
258+
259+ reveal_type(MyClass2[int]) # N: Revealed type is "def (t: builtins.int) -> __main__.MyClass2[builtins.int, builtins.int]"
260+
261+ # `T2` now defaults to `int` for `MyClass` from now on
262+ reveal_type(MyClass2[str]) # N: Revealed type is "def (t: builtins.str) -> __main__.MyClass2[builtins.str, builtins.str]"
263+ reveal_type(MyClass2[bytes]) # N: Revealed type is "def (t: builtins.bytes) -> __main__.MyClass2[builtins.bytes, builtins.bytes]"
264+
265+ d_int: type[MyClass2[int]]
266+ d_str: type[MyClass2[str]]
267+
268+ reveal_type(d_int) # N: Revealed type is "type[__main__.MyClass2[builtins.int, builtins.int]]"
269+ reveal_type(d_str) # N: Revealed type is "type[__main__.MyClass2[builtins.str, builtins.str]]"
270+ [builtins fixtures/tuple.pyi]
271+
272+ [case testDefaultsApplicationInAliasNoCrash]
273+ # https://github.com/python/mypy/issues/19186
274+ from typing import Generic, TypeVar, TypeAlias
275+
276+ T1 = TypeVar("T1")
277+ T2 = TypeVar("T2", default=T1)
278+
279+ Alias: TypeAlias = "MyClass[T1, T2]"
280+
281+ class MyClass(Generic["T1", "T2"]): ...
282+ [builtins fixtures/tuple.pyi]
283+
187284[case testTypeVarDefaultsClass1]
188285# flags: --disallow-any-generics
189286from typing import Generic, TypeVar, Union, overload
0 commit comments