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:
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.
