On Thu, Dec 16, 2021 at 6:57 PM Steven Troxler <steven.troxler@gmail.com> wrote:
Hello all,
Thanks everyone for comments on our earlier thread [1] about callable type syntax. We now have a draft PEP [2] proposing an arrow-based syntax for callable types, for example:
``` (int, str) -> bool # equivalent to Callable[[int, str], bool] ```
In support of the PEP we also have: - a reference implementation of the parser [3] to ensure the grammar is correct (tests [5], [6], [7]) - a detailed specification of planned runtime behavior [4], which is not yet in the reference implementation
We'd like to get your feedback about the PEP in general, and especially details and edge cases we need to consider regarding runtime behavior.
Cheers, Steven Troxler
--------- [1] Earlier python-dev thread https://mail.python.org/archives/list/python-dev@python.org/thread/VBHJOS3LO... [2] PEP 677: https://www.python.org/dev/peps/pep-0677/ [3] Reference implementation of Parser: https://github.com/stroxler/cpython/tree/callable-type-syntax--shorthand [4] Details on the runtime behavior: https://docs.google.com/document/d/15nmTDA_39Lo-EULQQwdwYx_Q1IYX4dD5WPnHbFG7...
[5] Ast tests for parser changes: https://github.com/stroxler/cpython/blob/20eb59fdca0d6d8dbe4efa3b04038c7c220... [6] Easy-read tests of examples from the PEP: https://github.com/stroxler/cpython/blob/callable-type-syntax--shorthand/Lib... [7] Test sanity checking hundreds of examples pulled from typeshed: https://github.com/stroxler/cpython/blob/callable-type-syntax--shorthand/Lib...
Having a symbol (->) for typing when a similar expression uses a word (lambda) seems somewhat backwards to me. If wonder: back when lambda was added, were symbols like -> considered? If so, how do the arguments for `lambda` vs. a symbol compare with arguments for `Callable` vs. an arrow? I also wonder how many of the cases would be made much more readable by naming the callable type -- as you would do with any complex expression -- especially if PEP 646 Variadic Generics enables you to create specialized callable types like AsyncCallable or Decorator. For example, this would be quite clear to me: Customizer = AsyncCallable[[Response, list[UserSetting]], Response] def customize_response( response: Response, customizer: Customizer, ) -> Response: ... Naming probably won't help too much with more abstract higher-order functions like the flat_map example. But, if `(int) -> list[int]` makes things clear to you, Haskell might be a better choice than Python... --- BTW, has anyone thought about using a colon to parametrize Callable? Callable[[Response, list[UserSetting]]: Response] /joke