Skip to content

Commit 75fcee1

Browse files
committed
improve signature of tuple constructor
1 parent da82f20 commit 75fcee1

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

crates/ty_python_semantic/resources/mdtest/type_compendium/tuple.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def f(x: Iterable[int], y: list[str], z: Never, aa: list[Never], bb: LiskovUncom
5555

5656
reveal_type(tuple((1, 2))) # revealed: tuple[Literal[1], Literal[2]]
5757

58-
reveal_type(tuple([1])) # revealed: tuple[object, ...]
58+
reveal_type(tuple([1])) # revealed: tuple[Unknown | int, ...]
59+
60+
x1: tuple[int, ...] = tuple([1])
61+
reveal_type(x1) # revealed: tuple[int, ...]
5962

6063
# error: [invalid-argument-type]
6164
reveal_type(tuple[int]([1])) # revealed: tuple[int]

crates/ty_python_semantic/src/types.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6122,30 +6122,35 @@ impl<'db> Type<'db> {
61226122
}
61236123

61246124
Some(KnownClass::Tuple) => {
6125-
let object = Type::object();
6125+
let element_ty =
6126+
BoundTypeVarInstance::synthetic(db, "T", TypeVarVariance::Covariant);
61266127

61276128
// ```py
6128-
// class tuple:
6129+
// class tuple(Sequence[_T_co]):
61296130
// @overload
61306131
// def __new__(cls) -> tuple[()]: ...
61316132
// @overload
6132-
// def __new__(cls, iterable: Iterable[object]) -> tuple[object, ...]: ...
6133+
// def __new__(cls, iterable: Iterable[_T_co]) -> tuple[_T_co, ...]: ...
61336134
// ```
61346135
CallableBinding::from_overloads(
61356136
self,
61366137
[
61376138
Signature::new(Parameters::empty(), Some(Type::empty_tuple(db))),
6138-
Signature::new(
6139+
Signature::new_generic(
6140+
Some(GenericContext::from_typevar_instances(db, [element_ty])),
61396141
Parameters::new(
61406142
db,
61416143
[Parameter::positional_only(Some(Name::new_static(
61426144
"iterable",
61436145
)))
61446146
.with_annotated_type(
6145-
KnownClass::Iterable.to_specialized_instance(db, [object]),
6147+
KnownClass::Iterable.to_specialized_instance(
6148+
db,
6149+
[Type::TypeVar(element_ty)],
6150+
),
61466151
)],
61476152
),
6148-
Some(Type::homogeneous_tuple(db, object)),
6153+
Some(Type::homogeneous_tuple(db, Type::TypeVar(element_ty))),
61496154
),
61506155
],
61516156
)

0 commit comments

Comments
 (0)