overloading.py icon indicating copy to clipboard operation
overloading.py copied to clipboard

TypeError: Unions cannot be used with issubclass()

Open emmeowzing opened this issue 8 years ago • 0 comments

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).

emmeowzing avatar Mar 18 '17 14:03 emmeowzing