pythonpython-typingunion-typespyright

Type hint function accepting union types


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.


Solution

  • Here's the type definition of isinstance() from typeshed (permalink):

    if sys.version_info >= (3, 10):
        _ClassInfo: TypeAlias = type | types.UnionType | tuple[_ClassInfo, ...]
    else:
        _ClassInfo: TypeAlias = type | tuple[_ClassInfo, ...]
    
    def isinstance(__obj: object, __class_or_tuple: _ClassInfo) -> bool: ...
    

    _ClassInfo is 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:

    def foo(predicate: Callable[[Any], bool]) -> None:
      ...