El jue, 30 jun 2022 a las 10:46, <nveric...@gmail.com> escribió: > (I apologize in advance if I've posted anything incorrectly before, I > believe I might have sent this in python-dev instead but not sure as it's > not appearing in the posts for my account). > > I am aware this is clarified in the Python documentation for the typing > module but I and others have thought the naming of Optional is still quite > confusing by itself. > > "Note that this is not the same concept as an optional argument, which is > one that has a default. An optional argument with a default does not > require the Optional qualifier on its type annotation just because it is > optional." - typing.Optional docs > > Google defines optional as this, "available to be chosen but not > obligatory." > > Pretend we have a function like this: > def test_func(param: Optional[str]) -> None: > ... > > The argument param is typed as Optional[str] meaning Union[str, None] or > str | None. Optional here if we follow the definition above, basically > means it can be str but not required or obligated to be that type. See the > problem with the naming? This is a function where param can be None or str, > not just it can be str but not obligated. Some interpretations may think > optional means left to one's choice as well. The docs even have to clarify > the use of Optional with typing because of this confusion. > > I believe this has been proposed before (not sure) but something like > Nullable or Noneable (not sure about naming) would make much more sense in > the context of typing. > def test_func(param: Noneable[str]) -> None: > ... > > It also would work and still make sense if there is a default value for > the argument: > def test_func(param: Noneable[str] = None) -> None: > ... > > def test_func(param: Noneable[str] = "hello world") -> None: > ... >
This problem has already been solved: in Python 3.10, thanks to PEP 604, you can write `str | None` instead of `Optional[str]`. > _______________________________________________ > Python-ideas mailing list -- python-ideas@python.org > To unsubscribe send an email to python-ideas-le...@python.org > https://mail.python.org/mailman3/lists/python-ideas.python.org/ > Message archived at > https://mail.python.org/archives/list/python-ideas@python.org/message/GV3ZHUYFAO2GM7PP4UX4JEMBEBOHRNQW/ > Code of Conduct: http://python.org/psf/codeofconduct/ >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/DRQWYCGPAEBIIH3DTALTMAJVKAEL53NL/ Code of Conduct: http://python.org/psf/codeofconduct/