Typing and Generics

typing.Optional

Equivalent to T | None. It describes a nullable value, not whether a function argument may be omitted; a default must be declared separately.

Formal Syntax & Structure

python syntaxpython
typing.Optional[T]

Return Value

type expression

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import Optional
def label(value: Optional[str] = None) -> str: return 'missing' if value is None else valueprint((label(), label('')))
Python Reference