Hello everyone, Since PEP with new callable syntax was rejected, I would like to propose another idea. It is quite simple, but yet powerful enough for our main use-cases. We can use `Callable[{'arg_name': arg_type}, ret_type]` syntax. It is an allowed syntax and python3.10+ can work with it out of the box: ``` Python 3.10.0 (default, Nov 1 2021, 10:24:06) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
from typing import Callable Callable[{}, int] typing.Callable[[{}], int]
It supports multi-line readable format:
func: Callable[{ ... 'a': int, ... 'b': str, ... }, int]
How can it solve our main problems?
1. It clearly supports argument names: `{'arg_name': int}`
2. It can support positional only arguments via: `{'__arg_name': int}` or `{'/arg_name': int}`
3. It can support args and kwargs: `{'*': int, '**': str}`
4. It can support kw-only arguments: `{'*kw_arg': type}`
5. It can support any arguments with default values: `{'required_arg': int, 'has_default': typing.NotRequired[str]}`, because the exact default value is not a part of the function signature
This change does not require any parser changes, only semantic ones in `typing.py` and type-checkers.
Old `Callable` semantics with `[]` will stay the same.
Probably older python versions would need to use `typing_extensions.Callable` to have this feature.
Old pythons raise an error for `{}` use in `Callable`:
Python 3.7.11 (default, Jul 16 2021, 15:35:56)
from typing import Callable Callable[{}, int] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/sobolev/.pyenv/versions/3.7.11/lib/python3.7/typing.py", line 760, in __getitem__ raise TypeError(f"Callable[args, result]: args must be a list." TypeError: Callable[args, result]: args must be a list. Got {}
Later we can reuse quite similar syntax for in-place typed-dict definitions:
def expects_user(user: {'email': str, 'age': int}): print(user['email'], user['age']) ``` What do other think about this idea? :)