[New-bugs-announce] [issue29262] Provide a way to check for *real* typing.Union instances

flying sheep report at bugs.python.org
Fri Jan 13 10:45:47 EST 2017


New submission from flying sheep:

typing.Union prevents the use of `isinstance` and `issubclass` via a hook.

This is presumably to prevent errors that would arise if someone tried to do issubclass(A, Union[A, B]) or isinstance(A(), Union[A, B]), because Union isn’t specified in the PEP to allow a check like this, and doesn’t implement it. (Instead it throws said error)

However, as far as I can see there is no blessed way to check if an object was returned by Union.__getitem__(). A simple way that works is:

sig = inspect.signature(f)
ann = sig.parameters['arg1'].annotation
is_an_union = isinstance(ann, typing._Union)

but _Union is a private class, and an implementation detail.

is there a blessed way to do this? If not, one should be added.

----------
components: Library (Lib)
messages: 285410
nosy: flying sheep
priority: normal
severity: normal
status: open
title: Provide a way to check for *real* typing.Union instances
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29262>
_______________________________________


More information about the New-bugs-announce mailing list