Skip to content

Commit 293d4ac

Browse files
authored
[ty] Add meta-type tests for legavy TypeVars (#18453)
## Summary Follow up to the comment by @dcreager [here](#18439 (comment)).
1 parent 9e8a7e9 commit 293d4ac

File tree

1 file changed

+29
-0
lines changed
  • crates/ty_python_semantic/resources/mdtest/generics/legacy

1 file changed

+29
-0
lines changed

crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,33 @@ def constrained(f: T):
196196
reveal_type(f()) # revealed: int | str
197197
```
198198

199+
## Meta-type
200+
201+
The meta-type of a typevar is the same as the meta-type of the upper bound, or the union of the
202+
meta-types of the constraints:
203+
204+
```py
205+
from typing import TypeVar
206+
207+
T_normal = TypeVar("T_normal")
208+
209+
def normal(x: T_normal):
210+
reveal_type(type(x)) # revealed: type
211+
212+
T_bound_object = TypeVar("T_bound_object", bound=object)
213+
214+
def bound_object(x: T_bound_object):
215+
reveal_type(type(x)) # revealed: type
216+
217+
T_bound_int = TypeVar("T_bound_int", bound=int)
218+
219+
def bound_int(x: T_bound_int):
220+
reveal_type(type(x)) # revealed: type[int]
221+
222+
T_constrained = TypeVar("T_constrained", int, str)
223+
224+
def constrained(x: T_constrained):
225+
reveal_type(type(x)) # revealed: type[int] | type[str]
226+
```
227+
199228
[generics]: https://typing.python.org/en/latest/spec/generics.html

0 commit comments

Comments
 (0)