From 293d4ac388715c0b5cbef1d0c80f4a0ae0246078 Mon Sep 17 00:00:00 2001 From: David Peter Date: Wed, 4 Jun 2025 09:44:44 +0200 Subject: [PATCH] [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). --- .../mdtest/generics/legacy/variables.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md b/crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md index b58507f43c..cf3cff8177 100644 --- a/crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md +++ b/crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md @@ -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