Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4247,6 +4247,13 @@ impl<'db> Type<'db> {
Some(KnownClass::Tuple) => {
let object = Type::object(db);

// ```py
// class tuple:
// @overload
// def __new__(cls) -> tuple[()]: ...
// @overload
// def __new__(cls, iterable: Iterable[object]) -> tuple[object, ...]: ...
// ```
CallableBinding::from_overloads(
self,
[
Expand Down Expand Up @@ -4308,6 +4315,13 @@ impl<'db> Type<'db> {
let instantiated = Type::instance(db, ClassType::from(alias));

let parameters = if alias.origin(db).is_known(db, KnownClass::Tuple) {
// ```py
// class tuple:
// @overload
// def __new__(cls: type[tuple[()]], iterable: tuple[()] = ()) -> tuple[()]: ...
// @overload
// def __new__[T](cls: type[tuple[T, ...]], iterable: tuple[T, ...]) -> tuple[T, ...]: ...
// ```
let spec = alias.specialization(db).tuple(db);
let mut parameter =
Parameter::positional_only(Some(Name::new_static("iterable")))
Expand Down
5 changes: 4 additions & 1 deletion crates/ty_python_semantic/src/types/call/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,13 +973,16 @@ impl<'db> Bindings<'db> {
}

Some(KnownClass::Tuple) if overload_index == 1 => {
// `tuple(range(42))` => `tuple[int, ...]`
// BUT `tuple((1, 2))` => `tuple[Literal[1], Literal[2]]` rather than `tuple[Literal[1, 2], ...]`
if let [Some(argument)] = overload.parameter_types() {
let overridden_return =
argument.into_tuple().map(Type::Tuple).unwrap_or_else(|| {
// Some awkward special handling is required here because of the fact
// that calling `try_iterate()` on `Never` returns `Never`,
// but `tuple[Never, ...]` eagerly simplifies to `tuple[()]`,
// which will cause us to emit false positives if we index into the tuple
// which will cause us to emit false positives if we index into the tuple.
// Using `tuple[Unknown, ...]` avoids these false positives.
let specialization = if argument.is_never() {
Type::unknown()
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ impl KnownClass {
| Self::Float
| Self::Enum
| Self::ABCMeta
| KnownClass::Iterable
| Self::Iterable
// Empty tuples are AlwaysFalse; non-empty tuples are AlwaysTrue
| Self::NamedTuple
// Evaluating `NotImplementedType` in a boolean context was deprecated in Python 3.9
Expand Down
Loading