My code defines lots of typing types, and I need to isinstance
lots of objects against them at runtime. Something like this:
Type1 = list[int]
Type2 = list[str]
# etc.
# get data from the outside, e.g. read from a file
data = get_data()
# NOT WORKING
if isinstance(data, Type1):
"do something"
elif isinstance(data, Type2):
"do something else"
else:
"etc."
I browsed stackoverflow and found this answer which suggested using pydantic. This is what I’ve been using (I modified the answer to be compatible with pydantic 2.0):
from pydantic import TypeAdapter, ValidationError
# drop-in replacement for isinstance
def pydantic_isinstance(obj, pydantic_type):
try:
TypeAdapter(pydantic_type).validate_python(obj)
return True
except ValidationError:
return False
I’m not entirely happy with this solution because unlike the built-in isinstance
, this does not integrate with static type checkers (I use pyright). For instance, inside the block if pydantic_isinstance(data, list[int]): ...
, pyright does not recognize that data
is of type list[int]
.
In short, I need a runtime type validator that also works with static type checkers. Does it exist? How would I implement one? I prefer staying with pyright, but if a solution only exists with another type checker, I will consider migrating.