[ty] Add meta-type tests for legavy TypeVars (#18453)

## Summary

Follow up to the comment by @dcreager
[here](https://github.com/astral-sh/ruff/pull/18439#discussion_r2123802784).
This commit is contained in:
David Peter 2025-06-04 09:44:44 +02:00 committed by GitHub
parent 9e8a7e9353
commit 293d4ac388
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -196,4 +196,33 @@ def constrained(f: T):
reveal_type(f()) # revealed: int | str
```
## Meta-type
The meta-type of a typevar is the same as the meta-type of the upper bound, or the union of the
meta-types of the constraints:
```py
from typing import TypeVar
T_normal = TypeVar("T_normal")
def normal(x: T_normal):
reveal_type(type(x)) # revealed: type
T_bound_object = TypeVar("T_bound_object", bound=object)
def bound_object(x: T_bound_object):
reveal_type(type(x)) # revealed: type
T_bound_int = TypeVar("T_bound_int", bound=int)
def bound_int(x: T_bound_int):
reveal_type(type(x)) # revealed: type[int]
T_constrained = TypeVar("T_constrained", int, str)
def constrained(x: T_constrained):
reveal_type(type(x)) # revealed: type[int] | type[str]
```
[generics]: https://typing.python.org/en/latest/spec/generics.html