Skip to content
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(..., Optional[Any]) always returns False #128232

Closed
jcal-15 opened this issue Dec 25, 2024 · 3 comments
Closed

isinstance(..., Optional[Any]) always returns False #128232

jcal-15 opened this issue Dec 25, 2024 · 3 comments
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@jcal-15
Copy link

jcal-15 commented Dec 25, 2024

Bug report

Bug description:

isinstance(..., Optional[Any]) is always returning False. Based off of behavior of other types I am pretty sure that isinstance should raise a TypeError

# Add a code block here, if required
>>> from typing import Optional, Any

# ✅ `Any` raises a TypeError
>>> isinstance(1, Any)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    isinstance(1, Any)
    ~~~~~~~~~~^^^^^^^^
  File "C:\Program Files\Python313\Lib\typing.py", line 589, in __instancecheck__
    raise TypeError("typing.Any cannot be used with isinstance()")
TypeError: typing.Any cannot be used with isinstance()

# ✅ Generic types raise type errors
>>> isinstance(1, list[int])
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    isinstance(1, list[int])
    ~~~~~~~~~~^^^^^^^^^^^^^^
TypeError: isinstance() argument 2 cannot be a parameterized generic

# ✅ Optional of Generic types raise type errors
>>> isinstance(1, Optional[list[int]])
Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
    isinstance(1, Optional[list[int]])
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\typing.py", line 1786, in __instancecheck__
    return self.__subclasscheck__(type(obj))
           ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "C:\Program Files\Python313\Lib\typing.py", line 1790, in __subclasscheck__
    if issubclass(cls, arg):
       ~~~~~~~~~~^^^^^^^^^^
TypeError: issubclass() argument 2 cannot be a parameterized generic
>>>

# ❌ Optional of `Any` returns false
>>> isinstance(1, Optional[Any])
False
>>> isinstance(Any, Optional[Any])
False
>>> isinstance("Any", Optional[Any])
False
>>>

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Windows

@jcal-15 jcal-15 added the type-bug An unexpected behavior, bug, or error label Dec 25, 2024
@JelleZijlstra
Copy link
Member

This appears to be because Optional[Any] produces a typing.Union, and its __instancecheck__ is implemented by delegating to __subclasscheck__, and somehow you can do issubclass() on Any:

>>> issubclass(int, Any)
False

@serhiy-storchaka
Copy link
Member

This looks related to #88834 (or the same issue).

@serhiy-storchaka
Copy link
Member

Closing as a duplicate of #88834.

@serhiy-storchaka serhiy-storchaka closed this as not planned Won't fix, can't repro, duplicate, stale Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants