replace-union-with-optional

Enforces the use of Optional[T] over Union[T, None] and Union[None, T].

Message

Optional[T] is preferred over Union[T, None] or Union[None, T].

References

Valid examples

def func() -> Optional[str]:
    pass
def func() -> Optional[Dict]:
    pass
def func() -> Union[str, int, None]:
    pass

Invalid examples

def func() -> Union[str, None]:
    pass
Show more
from typing import Optional
def func() -> Union[Dict[str, int], None]:
    pass

Suggested fix

from typing import Optional
def func() -> Optional[Dict[str, int]]:
    pass
from typing import Optional
def func() -> Union[str, None]:
    pass

Suggested fix

from typing import Optional
def func() -> Optional[str]:
    pass
from typing import Optional
def func() -> Union[Dict, None]:
    pass

Suggested fix

from typing import Optional
def func() -> Optional[Dict]:
    pass