Skip to content

TypeError: Unions cannot be used with issubclass() #6

@emmeowzing

Description

@emmeowzing

While running the previous test case I've included in #5, I also came across problems in PyCharm where the builtin issubclass is called with arguments type_ and typing.Union (e.g. L#457, L#475, etc.). By the following example, and the source in the standard library, you cannot call issubclass on typing.Union, since it can't be subclassed.

>>> from typing import Union
>>> issubclass(str, Union[str])
True
>>> issubclass(str, Union[str, float])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/typing.py", line 770, in __subclasscheck__
    raise TypeError("Unions cannot be used with issubclass().")
TypeError: Unions cannot be used with issubclass().

The former only works because the constructor of typing.Union returns the single type (c.f. this comment in the docstring, and this if-statement in the __new__ block).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions