ruff/crates/ty_python_semantic/resources/mdtest/narrow
Douglas Creager ea812d0813
[ty] Homogeneous and mixed tuples (#18600)
We already had support for homogeneous tuples (`tuple[int, ...]`). This
PR extends this to also support mixed tuples (`tuple[str, str,
*tuple[int, ...], str str]`).

A mixed tuple consists of a fixed-length (possibly empty) prefix and
suffix, and a variable-length portion in the middle. Every element of
the variable-length portion must be of the same type. A homogeneous
tuple is then just a mixed tuple with an empty prefix and suffix.

The new data representation uses different Rust types for a fixed-length
(aka heterogeneous) tuple. Another option would have been to use the
`VariableLengthTuple` representation for all tuples, and to wrap the
"variable + suffix" portion in an `Option`. I don't think that would
simplify the method implementations much, though, since we would still
have a 2×2 case analysis for most of them.

One wrinkle is that the definition of the `tuple` class in the typeshed
has a single typevar, and canonically represents a homogeneous tuple.
When getting the class of a tuple instance, that means that we have to
summarize our detailed mixed tuple type information into its
"homogeneous supertype". (We were already doing this for heterogeneous
types.)

A similar thing happens when concatenating two mixed tuples: the
variable-length portion and suffix of the LHS, and the prefix and
variable-length portion of the RHS, all get unioned into the
variable-length portion of the result. The LHS prefix and RHS suffix
carry through unchanged.

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
2025-06-20 18:23:54 -04:00
..
conditionals [ty] basic narrowing on attribute and subscript expressions (#17643) 2025-06-17 11:07:46 +02:00
assert.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
assignment.md [ty] basic narrowing on attribute and subscript expressions (#17643) 2025-06-17 11:07:46 +02:00
bool-call.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
boolean.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
complex_target.md [ty] Homogeneous and mixed tuples (#18600) 2025-06-20 18:23:54 -04:00
hasattr.md [ty] Only consider a type `T` a subtype of a protocol `P` if all of `P`'s members are fully bound on `T` (#18466) 2025-06-04 19:39:14 +00:00
isinstance.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
issubclass.md Update class literal display to use `<class 'Foo'>` style (#17889) 2025-05-06 20:11:25 -04:00
match.md [ty] Understand classes that inherit from subscripted `Protocol[]` as generic (#17832) 2025-05-09 17:39:15 +01:00
post_if_statement.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00
truthiness.md [ty] Reachability constraints (#18621) 2025-06-17 09:24:28 +02:00
type.md [ty] Surface matched overload diagnostic directly (#18452) 2025-06-20 08:36:49 +05:30
type_guards.md [ty] Homogeneous and mixed tuples (#18600) 2025-06-20 18:23:54 -04:00
while.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00