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

Open
jcal-15 opened this issue Dec 25, 2024 · 1 comment
Open

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

jcal-15 opened this issue Dec 25, 2024 · 1 comment
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

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

3 participants