[Python-ideas] Using rightarrow "->" for typing annotation of functions
Guido van Rossum
guido at python.org
Wed Apr 24 17:42:05 EDT 2019
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
Too bad we can't use Unicode arrows. :-)
On Wed, Apr 24, 2019 at 2:30 PM Vaibhav Karve <vaibhavskarve at gmail.com>
> (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?
> 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.
> Vaibhav Karve
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
--Guido van Rossum (python.org/~guido)
*Pronouns: he/him/his **(why is my pronoun here?)*
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas