Mailing List Archive

Confusing naming of Optional type should be changed
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:
...
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/QT7RJSDMWKTGB6PNVMGZ2NY2D4PT75Y7/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: Confusing naming of Optional type should be changed [ In reply to ]
Given that it's now presumably preferred spelling is `str | None` in
Python 3.10, why would we not seek to deprecate `Optional[str]` instead
of inventing a replacement?

On Thu, 2022-06-30 at 17:15 +0000, nverich07@gmail.com wrote:
> 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:
>     ...
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-leave@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/QT7RJSDMWKTGB6PNVMGZ2NY2D4PT75Y7/
> Code of Conduct: http://python.org/psf/codeofconduct/
Re: Confusing naming of Optional type should be changed [ In reply to ]
Ah, to be honest, I completely forgot about deprecation for some reason :)
Yes, that would definitely be a much better idea to phase out Optional and probably Union in the future actually. Thanks for bringing it up!
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/OGHCYNZ7UFE4FOCEUTZTTA43KRJU6CGA/
Code of Conduct: http://python.org/psf/codeofconduct/