Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Optional typing for more than a single type

Since the documentation only uses Optional with a single type (Optional[X]), I was wondering if Union is required if I have an argument that accepts either a string, a list, or None.

def func(
    arg: Optional[str, list]
)
def func(
    arg: Optional[Union[str, list]]
)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Since Optional[...] is shorthand for Union[..., None], they are essentially the same, and you can just use Optional[str, list], since that’s the same as Union[str, list, None].

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading