[Python-ideas] Using rightarrow "->" for typing annotation of functions
levkivskyi at gmail.com
Thu Apr 25 16:50:43 EDT 2019
TBH, I don't think it is so bad that it requires a new syntax. But I am not
strongly against it either. What I would like to add here is that if we
will go with the replacement:
Callable[[X, Y], Z] becomes (X, Y) -> Z
then we should also go with
Union[X, Y] becomes X | Y
Tuple[X, Y] becomes (X, Y)
Optional[X] becomes X?
(Intersection[X, Y] when added becomes X & Y)
The current syntax (although really verbose) is consistent, so if we want
to improve it I would like to keep consistency.
Also if we are going forward with this, should we allow mixing old and new
syntax? Will the old syntax be deprecated after we introduce the new one?
On Wed, 24 Apr 2019 at 14:43, Guido van Rossum <guido at python.org> 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
> 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?)*
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas