Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: typing.Any cannot be used with isinstance()

I was wondering why Python's typing module does not support isinstance(<obj>, Any) and raises a TypeError. I would expect it to always return True. Is there a specific reason why it does not always return True?

  • The TypeError is raised here.
  • Example of the TypeError:
>>> from typing import Any
>>> isinstance(1, Any)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/peter/miniconda3/envs/prefect_utils/lib/python3.9/typing.py", line 338, in __instancecheck__
    raise TypeError(f"{self} cannot be used with isinstance()")
TypeError: typing.Any cannot be used with isinstance()
like image 514
Peter Avatar asked Oct 23 '25 02:10

Peter


2 Answers

The Any class docstring seems to state the reason:

"""Special type indicating an unconstrained type.

- Any is compatible with every type.
- Any assumed to have all methods.
- All values assumed to be instances of Any.

Note that all the above statements are true from the point of view of
static type checkers. At runtime, Any should not be used with instance
or class checks.
"""

def __instancecheck__(self, obj):
    raise TypeError("Any cannot be used with isinstance().")

def __subclasscheck__(self, cls):
    raise TypeError("Any cannot be used with issubclass().")

It's likely that treating these objects as an actual instance or subclass may lead to problems if they don't provide exactly what the instance or subclass should. Instead, they "fake" the fact they can be anything but not so much as to cause damage by being treated that way in the type hierarchy.

like image 56
paxdiablo Avatar answered Oct 25 '25 17:10

paxdiablo


Most things in typing are not intended for runtime type checking. Most types cannot be sensibly checked dynamically, so typing avoids pretending it's possible.


The Any type is not a proper type – it is compatible with any type in any usage. Depending on the use, it can be both the super and sub type of another type.
Most prominently, Any supports all operations. Therefore, isinstance(x, Any) == True would mean that x supports all operations, regardless of x' concrete type. This is not sensible for most proper types.


Consider for example checking an integer as an "instance of Any", which implies it supports slicing:

x = 3
if isinstance(x, Any):  # x supports any operation in this block
    print(x[:3])        # slicing is part of "all operations"
like image 43
MisterMiyagi Avatar answered Oct 25 '25 16:10

MisterMiyagi