42

Now that Python 3.10 has been released, is there any preference when indicating that a parameter or returned value might be optional, i.e., can be None. So what is preferred:

Option 1:

def f(parameter: Optional[int]) -> Optional[str]:

Option 2:

def f(parameter: int | None) -> str | None:

Also, is there any preference between Type | None and None | Type?

2
  • 5
    The 3.10 docs don't deprecate Union or Optional in favour of the new syntax, so it's up to you.
    – jonrsharpe
    Oct 4, 2021 at 18:15
  • 2
    When in doubt, choose the shorter.
    – F.D.Castel
    Jun 26, 2022 at 0:51

4 Answers 4

52

PEP 604 covers these topics in the specification section.

The existing typing.Union and | syntax should be equivalent.

int | str == typing.Union[int, str]

The order of the items in the Union should not matter for equality.

(int | str) == (str | int)
(int | str | float) == typing.Union[str, float, int]

Optional values should be equivalent to the new union syntax

None | t == typing.Optional[t]

As @jonrsharpe comments, Union and Optional are not deprecated, so the Union and | syntax are acceptable.


Łukasz Langa, a Python core developer, replied on a YouTube live related to the Python 3.10 release that Type | None is preferred over Optional[Type] for Python 3.10+.

enter image description here

3
  • 3
    @Imiguelvargasf he would say that — he wrote the PEP ;) it's just a matter of personal preference imo, though I personally can't think of a situation in which I'd find the older syntax clearer or more readable. On the other hand, it's more backwards-compatible! Oct 4, 2021 at 18:24
  • 1
    @AlexWaygood - i don't see Lukasz Langa's name on the pep. are you sure he wrote it?
    – jkr
    Oct 5, 2021 at 11:14
  • @jakub my bad! Looks like I was getting PEP 585 and PEP 604 mixed up. Thanks for the correction. Too many typing PEPs, too little time, etc etc Oct 5, 2021 at 11:21
17

I would personally go with Option 2 moving forward.

Also, just wanted to add this for awareness, but Python 3.7+ can made to support this syntax using a __future__ import as shown below. This type checks just the same; I actually got the tip from the latest release notes from Pycharm, which I'm currently using.

from __future__ import annotations


def f(parameter: int | None) -> str | None:
    ...
3

Non-authoritative, but I would expect Optional when

  • there is a default value provided (probably None)
  • None would be unusual for the caller to pass

While I would expect some Union or | to be used when

  • there is not a default value and/or the default is not None
  • None is also a valid value

See related suggestions at How do I add default parameters to functions when using type hinting?

1
  • For default-None arguments I really feel that Optional[T] is so verbose, compared to other languages which have some form of T?. As type hints are a cosmetic, I sometimes just do foo(comment: str = None), and I think functions returning None (like __init__() for example) shouldn't have a return value annotation. -> None seems sick to me. But it's not a "standard", and a linter will attack me for that. Jul 7, 2022 at 9:03
3

This is entirely my opinion, but I think that while they are equivalent to the type checker, they express very different intentionality to the human reader. So they are not interchangeable.

If you're declaring a default value, then type it as def foo(bar: Optional[int] = None) which declares an explicit intent that you don't have to pass in an argument.

However, if you don't have a default value, it may be declared def foo(bar: int | None), which declares that this is a required argument where None is one of the valid argument value.

Most of the time, you want to do the former, so most of the time you should still use Optional[int]; but there may be situations where requiring that callers explicitly pass None may be desirable.

A secondary reason is that writing def foo(bar: int | None = None) just looks ugly and confusing with the repeated None.

2
  • 7
    Note that Optional has nothing to do with the argument being optional. The parameter bar = 12 also means that the argument bar is optional, but definitely not Optional. Sep 3, 2022 at 10:49
  • Interesting style guide, expressed in second and third paragraphs. However, these two shall be accepted by the coders before first paragraph fully applies ;)
    – Joël
    Oct 6, 2022 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.