Fallenreaper
Fallenreaper

Reputation: 10684

Does python typing support multiple types?

I was looking at typings set up on the web but I was curious if I can have multiple types. Example:

self.test: str or None = None

It shows valid on my intellisense but I wasnt sure if it computed it different.

I was trying to implement something equivalent to typescript.

test: number|null = null;

But I didn't see specific types in this regard. Pretty much all my items have the option of being None/Null.

Upvotes: 1

Views: 1648

Answers (1)

Most Wanted
Most Wanted

Reputation: 7019

You should use Union https://docs.python.org/3/library/typing.html#typing.Union

from typing import Union
self.test: Union[str, None]

You can use Optional[X] as a shorthand for Union[X, None].

Upvotes: 4

Related Questions