improve signature of `tuple` constructor

This commit is contained in:
Ibraheem Ahmed 2025-12-02 16:57:53 -05:00
parent 12e72041c6
commit 19c5cd8018
2 changed files with 15 additions and 7 deletions

View File

@ -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]

View File

@ -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))),
),
],
)