A new shorthand for Optional

The pipe symbol | is used to represent Union in versions 3.10+.
Union[str, int] can be abbreviated as str | int.

Additionally, Optional[T] can be simplified to T | None making use of union.

Of course, I realize that readability should take precedence over conciseness.
My suggestion is to introduce a syntax like T? to abbreviate Optional[T]. Sorry if this is not a good proposal; please let me know of the problems with it.

This was previously a PEP for which the sponsor withdrew support: PEP 645 – Allow writing optional types as x? | peps.python.org.

1 Like