I know it’s too late to change it, but as though exercise, I’ve been thinking if one day Python was recreated from scratch, could typing annotation for tuple be improved:
Currently there’s a big inconsistency between tuple[str] vs list[str]. It’s a very common error (from a quick search on github, it’s easy to find hundreds of examples).
Couldn’t have it been better to have tuple[X] be consistent with list[X] and instead introduce a new special syntax for tuple[X, ...]:
Before:
x: tuple[T]
x: tuple[int, float]
x: tuple[int, ...]
After:
x: (T,)
x: (int, float)
x: (int, ...) # Or tuple[int]
This would of course create the issue of knowing when a (int,) should be a tuple vs a types.GenericAlias, but maybe typing.get_type_hints() could normalize tuple to some special class, like:
def zip(x: _T1, y: _T2) -> (_T1, _T2):
...
typing.get_type_hints(zip) == {
'return': TupleArgs[_T1, _T2]
}
Another edge case is that there’s no distinction between x[int, int] and x[(int, int)], but maybe that’s another thing to fix ![]()
It’s not really serious, and I’m sure there’s lot of edge cases I didn’t concidered, but it’s interesting to think about.