Skip to content

Commit 0986edf

Browse files
authored
[ty] Meta-type of type variables should be type[..] (#18439)
## Summary Came across this while debugging some ecosystem changes in #18347. I think the meta-type of a typevar-annotated variable should be equal to `type`, not `<class 'object'>`. ## Test Plan New Markdown tests.
1 parent 03f1f8e commit 0986edf

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,23 @@ def constrained[T: (Callable[[], int], Callable[[], str])](f: T):
766766
reveal_type(f()) # revealed: int | str
767767
```
768768

769+
## Meta-type
770+
771+
The meta-type of a typevar is the same as the meta-type of the upper bound, or the union of the
772+
meta-types of the constraints:
773+
774+
```py
775+
def normal[T](x: T):
776+
reveal_type(type(x)) # revealed: type
777+
778+
def bound_object[T: object](x: T):
779+
reveal_type(type(x)) # revealed: type
780+
781+
def bound_int[T: int](x: T):
782+
reveal_type(type(x)) # revealed: type[int]
783+
784+
def constrained[T: (int, str)](x: T):
785+
reveal_type(type(x)) # revealed: type[int] | type[str]
786+
```
787+
769788
[pep 695]: https://peps.python.org/pep-0695/

crates/ty_python_semantic/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5243,7 +5243,7 @@ impl<'db> Type<'db> {
52435243
Type::Tuple(_) => KnownClass::Tuple.to_class_literal(db),
52445244

52455245
Type::TypeVar(typevar) => match typevar.bound_or_constraints(db) {
5246-
None => KnownClass::Object.to_class_literal(db),
5246+
None => KnownClass::Type.to_instance(db),
52475247
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => bound.to_meta_type(db),
52485248
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
52495249
// TODO: If we add a proper `OneOf` connector, we should use that here instead

0 commit comments

Comments
 (0)