Add support for functions returning variadic tuples (tuple[T, ...])#727
Merged
tonybaloney merged 1 commit intotonybaloney:mainfrom Nov 29, 2025
Merged
Add support for functions returning variadic tuples (tuple[T, ...])#727tonybaloney merged 1 commit intotonybaloney:mainfrom
tuple[T, ...])#727tonybaloney merged 1 commit intotonybaloney:mainfrom
Conversation
785df7f to
f470a5d
Compare
tonybaloney
approved these changes
Nov 29, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In PR #669, the parser recognised variadic tuples (
tuple[T, ...], variable-length tuples with homogeneous element types, but that was about it. This PR adds full support for Python functions that return tuples of unknown length but known element type, such astuple[int, ...]ortuple[tuple[int, str], ...].Design
The implementation introduces a new Python type construct
VariadicTupleTypethat represents tuples with an ellipsis syntax (tuple[T, ...]). Unlike fixed-length tuples (TupleType) which have a known number of elements with potentially different types, variadic tuples have a variable number of elements where all elements are of the same type.Note that variadic tuples are only supported in return position. This PR does not support variadic tuples in argument position, which is something that can be added in the future. Meanwhile, returning variadic tuples seems a lot more common & useful in Python.
Key Design Decisions:
Immutable Collections: Variadic tuples are mapped to .NET's
ImmutableArray<T>rather thanIReadOnlyList<T>to provide better performance and immutability guarantees that match Python's tuple semantics. Another option would be to return a regular array since the immutability does not need to be strictly maintained on the C# side.Type Safety: The implementation preserves full type safety by generating strongly-typed importer classes that handle the conversion from Python tuples to .NET immutable arrays.
Consistent API: The feature integrates seamlessly with the existing type system and follows the same patterns as other collection types (sequences, dictionaries, etc.). Behaviourally, however, the tuple elements are marshaled and converted eagerly (which can be seen as a feature).
Technical Implementation
Core Components
1. Type System Extensions
VariadicTupleType: A new record type inPythonTypeSpec.csrepresentingtuple[T, ...]syntaxTypeReflection.AsPredefinedType()to mapVariadicTupleTypetoImmutableArray<T>forFromPythonconversionsPythonTypeSpecsubclasses for recognised Python types #669.2. Runtime Support
VarTuple<T, TImporter>: A new importer class inPyObjectImporters.csthat handles the conversion from Python tuple objects toImmutableArray<T>ImmutableArray.CreateBuilder<T>()for optimal performance when converting variable-length Python tuples, including dimensioning the array to the correct capacity.3. Code Generation
ResultConversionCodeGeneratorto generate appropriate conversion code for variadic tuple return typesImmutableArray<T>for variadic tuple resultsExample Usage
Python Code
Generated C# Interface
Breaking Changes
None. This is a pure additive feature that doesn't affect existing APIs or functionality.