Skip to content

Optional and other type tests in 3.6 Union #389

Description

@hsolbrig

In 3.5, I was able to test for optional and parameter types by:

from typing import Union, Optional

def is_optional(t):
   return issubclass(type(None), t)

def is_valid(e, t):
   return issubclass(type(e), t)

is_optional(Union[int, str])
> False
is_optional(Union[int, str, None])
> True
is_valid("abc", Union[int, str])
> True
is_valid(["abc"], Union[int, str])
> False

The issubclass method is no longer allowed in 3.6. How do I accomplish the equivalent in 3.6 on?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions