-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
isinstance
doesn't work on type aliases that use a union
#11673
Comments
I've also added explicit from typing import TypeAlias
A = int | str
B: TypeAlias = int | str
foo: object
isinstance(foo, A) # error
isinstance(foo, B) # error
isinstance(foo, int | str) # no error, but should be? Output:
|
In runtime >>> isinstance(1, int | str)
True
>>> isinstance(b'', int | str)
False So, I guess the problem is that |
Half of this is fixed in #11650
|
@esoma awesome! Thank you! 👍 |
@dgutson That PR didn't receive much attention and I don't use mypy anymore so I haven't pursued it any further. |
This is still broken.
import ipaddress as i
from typing import TypeAlias, TypeVar
IPAddress: TypeAlias = i.IPv4Address | i.IPv6Address
IPInterface: TypeAlias = i.IPv4Interface | i.IPv6Interface
_T = TypeVar('_T', bound=IPInterface | IPAddress)
def get_sort_key(ip: _T) -> tuple[int, IPAddress]:
# https://github.com/python/mypy/issues/11673
ipaddr = ip.ip if isinstance(ip, IPInterface) else ip
return i.get_mixed_type_key(ipaddr)
def sort_ips(addresses: set[_T]) -> list[_T]:
return sorted(addresses, key=get_sort_key) |
@esoma out of curiosity, which type checker did you switch to? |
The main problem is that we don't pass |
@DetachHead |
https://mypy-play.net/?mypy=master&python=3.10&gist=da37bef9ace54d59c18631a0a9f5bef2
The text was updated successfully, but these errors were encountered: