The following function with argument of type type accepts both
regular types and union types and works as expected.
However Pylance treats passing union type as error.
def foo(t: type):
print(t)
foo(int)
# OK
foo(int | str)
# Pylance:
# Argument of type "type[int] | type[str]"
# cannot be assigned to parameter "t" of type "type"
# in function "foo"
What is the correct type hint for any type including union types?
UPDATE:
Seems to be Pyright issue #7110 fixed in recent version.
Here's the type definition of
isinstance()fromtypeshed(permalink):_ClassInfois what you need.On the other hand, if you can modify the function in question, maybe you should apply composition and let it accept a predicate instead: