I want to define a dict whose key and value are share the same generic type and has some constraint with it. The following is the example of such a situation. However, applying mypy to the following code cause an error:
tmp.py:8: error: Type variable "tmp.AT" is unbound
tmp.py:8: note: (Hint: Use "Generic[AT]" or "Protocol[AT]" base class to bind "AT" inside a class)
tmp.py:8: note: (Hint: Use "AT" in function signature to bind "AT" inside a function)
Found 1 error in 1 file (checked 1 source file)
How to fix this? The reason why I need such a dict is that I want to add a type constraint between dict's key and value.
tmp.py:
from typing import Dict, Generic, TypeVar, Type, List
class A: pass
class B(A): pass
class C(A): pass
AT = TypeVar("AT", bound=A)
d: Dict[Type[AT], List[AT]] = {}