[Python-Dev] PEP 484 proposal: don't default to Optional if argument default is None
Ivan Levkivskyi
levkivskyi at gmail.com
Wed May 10 06:31:20 EDT 2017
There is a discrepancy now between PEP 484 and PEP 526:
def f(x: int = None): ... # OK
x: int = None # Error
I think the two rules should be "in sync", in view of this I agree with the
proposal.
Concerning verbosity and a long name Optional there are many reasonable
workarounds.
One is already mentioned from typing import Optional as O. Another
(unrelated to `` = None`` pattern)
is https://github.com/python/typing/issues/420 that allows to avoid
Optional altogether in patterns like this:
def func(x: Optional[X]) -> Optional[Y]:
if x is None:
return None
# do some stuff with 'x'
With @maybe decorator proposed in
https://github.com/python/typing/issues/420
this will be simplified to:
@maybe
def func(x: X) -> Y:
if x is None:
return None
# do some stuff with 'x'
Even if @maybe will not make it to typing, one still can define such (or
similar) decorators
(especially with @decorated_type and extended Callable syntax).
In view of this, I think verbosity is not a problem at all.
--
Ivan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170510/518f178d/attachment.html>
More information about the Python-Dev
mailing list