ruff/crates/ty_python_semantic/resources/mdtest/binary
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
..
booleans.md [ty] fix binary expression inference between boolean literals and `bool` instances (#18663) 2025-06-17 18:02:40 +01:00
classes.md Update class literal display to use `<class 'Foo'>` style (#17889) 2025-05-06 20:11:25 -04:00
custom.md Update class literal display to use `<class 'Foo'>` style (#17889) 2025-05-06 20:11:25 -04:00
in.md [ty] Minor tweaks to "list all members" docs and tests (#18388) 2025-05-30 13:36:57 -04:00
instances.md [ty] Understand homogeneous tuple annotations (#17998) 2025-05-12 22:02:25 -04:00
integers.md [ty] Implement Python's floor division semantics for `Literal` `int`s (#18249) 2025-05-22 10:42:29 -04:00
tuples.md [ty] Homogeneous and mixed tuples (#18600) 2025-06-20 18:23:54 -04:00
unions.md Rename Red Knot (#17820) 2025-05-03 19:49:15 +02:00