Is there a non-covariant `Type[T]`?

410 Views Asked by At

Suppose I'm trying to write type hints for a library function that registers a deserializer for a user-defined type: the user should provide a type T along with a function decode: str -> T.

The most natural way I can think to write this with python's PEP-484 type hints is the following:

from typing import Callable, Type, TypeVar
T = TypeVar("T")
def register_decoder(type_: Type[T], decode: Callable[[str], T]):
    ...

Unfortunately for me, the fact that Type[T] is covariant in T means this is not quite strict enough on the decode function: at least in pyright, the invocation register_decoder(int, decode=str) passes typecheck, with the type variable T resolved as the union int | str:

pyright's inference of int|str

Is there a way to type-hint this method that enforces the constraint that decode returns instances of type_, so that this example raises an error because str does not return int? One thing that would do the job is a non-covariant equivalent of Type[T] that accepts only the exact class object T rather than any subtype, but I'm not sure anything like this exists in Python.

1

There are 1 best solutions below

3
flakes On

Using mypy --strict gives me the expected error. Must be something specific to the linter you are using.

from typing import Callable, Type, TypeVar
T = TypeVar("T")
def register_decoder(type_: Type[T], decode: Callable[[str], T]) -> None:
    return

register_decoder(int, str)
>mypy --strict test.py
test.py:6: error: Argument 2 to "register_decoder" has incompatible type "Type[str]"; expected "Callable[[str], int]"
Found 1 error in 1 file (checked 1 source file)

>python --version
Python 3.9.5

>mypy --version
mypy 0.910