diff --git a/crates/ty_python_semantic/resources/mdtest/type_compendium/tuple.md b/crates/ty_python_semantic/resources/mdtest/type_compendium/tuple.md index 391450c10c..e2c45cc7f1 100644 --- a/crates/ty_python_semantic/resources/mdtest/type_compendium/tuple.md +++ b/crates/ty_python_semantic/resources/mdtest/type_compendium/tuple.md @@ -55,7 +55,10 @@ def f(x: Iterable[int], y: list[str], z: Never, aa: list[Never], bb: LiskovUncom reveal_type(tuple((1, 2))) # revealed: tuple[Literal[1], Literal[2]] -reveal_type(tuple([1])) # revealed: tuple[object, ...] +reveal_type(tuple([1])) # revealed: tuple[Unknown | int, ...] + +x1: tuple[int, ...] = tuple([1]) +reveal_type(x1) # revealed: tuple[int, ...] # error: [invalid-argument-type] reveal_type(tuple[int]([1])) # revealed: tuple[int] diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index 5036d213da..14d5a92487 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -6122,30 +6122,35 @@ impl<'db> Type<'db> { } Some(KnownClass::Tuple) => { - let object = Type::object(); + let element_ty = + BoundTypeVarInstance::synthetic(db, "T", TypeVarVariance::Covariant); // ```py - // class tuple: + // class tuple(Sequence[_T_co]): // @overload // def __new__(cls) -> tuple[()]: ... // @overload - // def __new__(cls, iterable: Iterable[object]) -> tuple[object, ...]: ... + // def __new__(cls, iterable: Iterable[_T_co]) -> tuple[_T_co, ...]: ... // ``` CallableBinding::from_overloads( self, [ Signature::new(Parameters::empty(), Some(Type::empty_tuple(db))), - Signature::new( + Signature::new_generic( + Some(GenericContext::from_typevar_instances(db, [element_ty])), Parameters::new( db, [Parameter::positional_only(Some(Name::new_static( "iterable", ))) .with_annotated_type( - KnownClass::Iterable.to_specialized_instance(db, [object]), + KnownClass::Iterable.to_specialized_instance( + db, + [Type::TypeVar(element_ty)], + ), )], ), - Some(Type::homogeneous_tuple(db, object)), + Some(Type::homogeneous_tuple(db, Type::TypeVar(element_ty))), ), ], )