Writing `tuple[T, U, V]` as `(T, U, V)`

It seems to me like it would be an easy win for making Python type annotations feel more natural to allow the syntax (str, int) as an alternative to tuple[str, int]. Tuples are a very important concept in Python, and the syntax seems very natural to me:

def min_max(inputs: list[int]) -> (int, int):
    ...

As far as I can tell, no parser changes would be required. get_type_hints() should maybe be changed to convert (T, U, ...) to tuple[T, U, ...].

One slight problem is that (str, int) cannot be used to signify a tuple bound for a type var in the PEP 695 syntax, because class A[T: (str, int)] already means that T is either exactly str or exactly int.

Examples from other programming languages

C#

(int, int) MinMax(int[] input)

Rust

fn min_max(input: &[i32]) -> (i32, i32)

Haskell

minMax :: [Int] -> (Int, Int)

Typescript (technically a typed array)

function minMax(input: number[]): [number, number]

Swift (more like a named tuple actually)

func minMax(input: [Int]) -> (min: Int, max: Int)

Zig

fn minMax(input: []const i32) struct { i32, i32 }
5 Likes

Another problem is that the AST for these is the same:

SomeGeneric[(int, str)]  # single type parameter, tuple[int, str]
SomeGeneric[int, str]  # two type parameters, int and str
6 Likes