I have the following inheritance structure:
class S:
...
class A(S):
...
class B(S):
...
I'd like to conceptually do something like the following:
class Foo:
T = TypeVar('T', bound=S)
items: dict[type[T], T] = dict()
def get(self, t: type[T]) -> T:
return self.items[t]
In words, I have a dict that maps from subtypes (A, B) to instances of those subtypes (A(), B()). I want to type hint a method that wraps this dictionary, and I want static analysis to show that it always returns an instance of the input type specifically, not just an instance of the supertype.
How can I type hint this properly?