mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
[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:
parent
9e8a7e9353
commit
293d4ac388
1 changed files with 29 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue