You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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>>>fromtypingimportOptional, Any# ✅ `Any` raises a TypeError>>>isinstance(1, Any)
Traceback (mostrecentcalllast):
File"<python-input-1>", line1, in<module>isinstance(1, Any)
~~~~~~~~~~^^^^^^^^File"C:\Program Files\Python313\Lib\typing.py", line589, in__instancecheck__raiseTypeError("typing.Any cannot be used with isinstance()")
TypeError: typing.Anycannotbeusedwithisinstance()
# ✅ Generic types raise type errors>>>isinstance(1, list[int])
Traceback (mostrecentcalllast):
File"<python-input-2>", line1, in<module>isinstance(1, list[int])
~~~~~~~~~~^^^^^^^^^^^^^^TypeError: isinstance() argument2cannotbeaparameterizedgeneric# ✅ Optional of Generic types raise type errors>>>isinstance(1, Optional[list[int]])
Traceback (mostrecentcalllast):
File"<python-input-10>", line1, in<module>isinstance(1, Optional[list[int]])
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^File"C:\Program Files\Python313\Lib\typing.py", line1786, in__instancecheck__returnself.__subclasscheck__(type(obj))
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^File"C:\Program Files\Python313\Lib\typing.py", line1790, in__subclasscheck__ifissubclass(cls, arg):
~~~~~~~~~~^^^^^^^^^^TypeError: issubclass() argument2cannotbeaparameterizedgeneric>>># ❌ 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
The text was updated successfully, but these errors were encountered:
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:
Bug report
Bug description:
isinstance(..., Optional[Any])
is always returning False. Based off of behavior of other types I am pretty sure thatisinstance
should raise aTypeError
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: