Python 3.10+ uses pipe sign |
which many languages use as the or operator (PEP 604) to denote Typing.Union
.
So…
self.some_attribute: AnotherClass|None = None
More concise, no Typing
import needed.
FWIW, generally 3.10 also does away with many built containers needing Typing imports: list[int]
, not List[int]
(which still works).
Note: if using forward declarations, Optional["AnotherClass"]
is still needed, pipe doesn't work.
Also pipe handles more than None, since it’s a shorthand for Typing.Union
:
some_attribute: AnotherClass|YetAnotherClass|None