Hi.
PEP 585 PEP 585 – Type Hinting Generics In Standard Collections | peps.python.org introduced the feature of builtin containers, like list, tuple and set having the square-bracket operator to create a parameterized container type, e.g. list[str] instad of using typing.List[str].
This is a great feature as it allows the typing system to slowly become more and more integrated with the language.
Then I have another suggestion, which naturally extends on this feature, which is to use brackets or parenthesis inside type annotations, e.g.
def my_fun(arr: [str], tup: (int,int,int), dic:{int:str}) -> (int, {float}):
pass
This example shows a function my_fun that accepts a list[str], a tuple[int,int,int] and a dict[int,str]. Then it returns a tuple[int, set[float]] Given these containers are being “instantiated” inside type annotations it is entirely unambiguous that they represent parameterized types.
This feature naturally comes from the cumbersome notation needed whenever a function wants to return two or more values. Currently, one has to type def my_fun(...) -> tuple[int, set[float]] which is repetitive and noisy. I’d rather simplify it to def my_fun(...) -> (int, set[float]) at least.
But I’m a bit confused about the status of this syntax as I see some tools (pycharm and mypy at least) which seem to support the -> (...) or -> [...] syntax for function return types, although both these cases are always handled as a tuple. And I can’t find anywhere in the typing PEPs any explicit mention to return with -> (...).
Anyway, this suggestion is more generic and applies to more container types and also to function arguments.
Thank you.
PS: I’ve seen mentions on github about the tuple syntax being rejected long time ago, before the first typing PEPs. But it might be worthwhile to revisit it.
PSS: I’m hope I’m not getting the terminology entirely wrong.