Skip to content

Latest commit

 

History

History
18 lines (12 loc) · 521 Bytes

use-union-type-hints.md

File metadata and controls

18 lines (12 loc) · 521 Bytes

Use union type hints in Python

Python 3.5+ allows optional type hints. To type hint a union (i.e. x or y), use the Union type:

from typing import Union

def hello_or_nothing(msg: str) -> Union[str, None]:
  if msg == "hello":
    return "hello"
  else:
    return None

See the documentation.

Note: there is an explicit Optional type which is equivalent to Union[x, None].