[Python-ideas] Using rightarrow "->" for typing annotation of functions

MRAB python at mrabarnett.plus.com
Wed Apr 24 20:54:15 EDT 2019


On 2019-04-24 22:42, Guido van Rossum wrote:
> Thanks for posting. I agree that Callable is ugly (even hideous :-), but 
> when we introduced type annotations in PEP 484, we didn't want to 
> introduce new syntax. The existing syntax (using -> in function 
> headings) was supported since Python 3.0.
> 
> Since then we've introduced other new syntax (in particular PEP 526) so 
> we could indeed try adding something better for Callable.
> 
> I think we should probably at least have parentheses around the 
> arguments, so you'd write
> 
> f: (int) -> str
> g: (int, str) -> float
> 
> That looks elegant.
> 
> But we should also try to support optional arguments and keyword arguments.
> 
> Also, some groups of people would like to see a more concise notation 
> for lambdas, and maybe they'd want to write
> 
> x = (a, b) -> a + b
> 
> as sugar for
> 
> x = lambda a, b: a + b
> 
> We probably can't have both, so we should at least decide which is more 
> important.
> 
> Too bad we can't use Unicode arrows. :-)
> 
Some languages use ->; some others use =>.

As Python already uses -> for the return type, it could use => for lambdas.

> On Wed, Apr 24, 2019 at 2:30 PM Vaibhav Karve <vaibhavskarve at gmail.com 
> <mailto:vaibhavskarve at gmail.com>> wrote:
> 
>     (Note: This idea is about a particular static typecheking (typing?)
>     annotation syntax).
>     The idea is that currently the use of the "->" (right arrow) is
>     restricted to only function definition annotation. Can we extent it
>     to declaration of type for functions even outside their definitions?
>     Example:
> 
>     Currently we write:
>          f: Callable[[int, Dict[str, int]], str]  # declaring the type
>     of some fake function
> 
>     This would be much cleaner if we could write:
>          f: int -> Dict[str, int] -> str   # One of the possibilities
> 
>     or even:
>          f: int, Dict[str, int] -> str      # Another possibility
> 
>     I have no idea how this will affect the existing syntax (and if this
>     will have any bad repercussions/notational misuse). I just thought
>     it would be nicer to:
>     a) Not have to spell out Callable
>     b) Not have to use all those square brackets
>     c) Have the same notation work for both the function annotation as
>     well as for declaring the type.
> 
>     This is my first time posting an idea to python-ideas. So apologies
>     if i am not following some conventions that i might not be aware of.


More information about the Python-ideas mailing list