[issue44529] Using typing.Union in isinstance checks

2021-06-29 Thread Eric V. Smith


Change by Eric V. Smith :


--
resolution:  -> out of date
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44529] Using typing.Union in isinstance checks

2021-06-28 Thread Kaleb Barrett


Kaleb Barrett  added the comment:

Ah, this was implemented for 3.10 as a part of PEP 604. 
https://www.python.org/dev/peps/pep-0604/#isinstance-and-issubclass.

Feel free to close.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44529] Using typing.Union in isinstance checks

2021-06-28 Thread Jack DeVries


Jack DeVries  added the comment:

I think this already works unless you are using TypeVars::

t = Union[str, int]

isinstance(1, t)# True
isinstance('1', t)  # True

If you are using TypeVars, though, it cannot work:

T = TypeVar('my_type')
thing = Union[T, str]

isinstance('hi', thing)  # Exception

This makes sense, because ``T`` is just a name, not related to an actual object.

Sorry if I'm missing something, because I did try the non-working example and 
my exception actually says, "issubclass() arg 2 must be a class, a tuple of 
classes, or a union." The exception makes sense, but its difference from yours 
might mean I'm doing something differently.

--
nosy: +jack__d

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue44529] Using typing.Union in isinstance checks

2021-06-28 Thread Kaleb Barrett


New submission from Kaleb Barrett :

I have type aliases of Unions like so:

Request = Union[WriteRequest, ReadRequest]

I'd like to be able to use "Request" in an isinstance check rather than having 
to duplicate the information in a tuple to pass to the check. Currently I get:

TypeError: Subscripted generics cannot be used with class and instance checks

Seems like Unions could be used here interchangeably with tuples since they 
mean the same thing in this context. Of course this would only work if the 
element types are being capable of being used in isinstance checks.

--
messages: 396658
nosy: ktbarrett
priority: normal
severity: normal
status: open
title: Using typing.Union in isinstance checks
type: enhancement

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com