Skip to content

Commit 7a24e6d

Browse files
committed
pre-commit and comments
1 parent 879a9dc commit 7a24e6d

File tree

2 files changed

+13
-2
lines changed
  • crates/ty_python_semantic

2 files changed

+13
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def _(p: P, q: Q):
1919
## Instantiating tuples
2020

2121
Like all classes, tuples can be instantiated by invoking the `tuple` class. When instantiating a
22-
specialization of `tuple` we check that the values passed in match the element types
23-
defined in the specialization.
22+
specialization of `tuple` we check that the values passed in match the element types defined in the
23+
specialization.
2424

2525
```toml
2626
[environment]

crates/ty_python_semantic/src/types/class.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,10 @@ impl<'db> ClassType<'db> {
622622
Some(spec) => {
623623
let tuple = spec.tuple(db);
624624
let tuple_len = tuple.len();
625+
625626
if tuple_len.minimum() == 0 && tuple_len.maximum().is_none() {
627+
// If the tuple has no length restrictions,
628+
// any iterable is allowed as long as the iterable has the correct element type.
626629
let mut tuple_elements = tuple.all_elements();
627630
iterable_parameter = iterable_parameter.with_annotated_type(
628631
KnownClass::Iterable
@@ -634,16 +637,24 @@ impl<'db> ClassType<'db> {
634637
"Tuple specialization should not have more than one element when it has no length restriction"
635638
);
636639
} else {
640+
// But if the tuple is of a fixed length, or has a minimum length, we require a tuple rather
641+
// than an iterable, as a tuple is the only kind of iterable for which we can
642+
// specify a fixed length, or that the iterable must be at least a certain length.
637643
iterable_parameter =
638644
iterable_parameter.with_annotated_type(Type::instance(db, self));
639645
}
640646
}
641647
None => {
648+
// If the tuple isn't specialized at all, we allow any argument as long as it is iterable.
642649
iterable_parameter = iterable_parameter
643650
.with_annotated_type(KnownClass::Iterable.to_instance(db));
644651
}
645652
}
646653

654+
// We allow the `iterable` parameter to be omitted for:
655+
// - a zero-length tuple
656+
// - an unspecialized tuple
657+
// - a tuple with no minimum length
647658
if specialization.is_none_or(|spec| spec.tuple(db).len().minimum() == 0) {
648659
iterable_parameter = iterable_parameter.with_default_type(TupleType::empty(db));
649660
}

0 commit comments

Comments
 (0)