I have the following code snippet:
from typing import TypedDict
class Super(TypedDict):
foo: int
class SubA(Super):
bar: int
class SubB(Super):
zap: int
def print_props(inp: Super, key: str):
print(inp[key])
When I call the method print_props with either an instance of SubA or SubB it would be valid as they are sub types of Super.
But mypy will complain about inp[key] as the key must be literal "foo".
Is it possible to give mypy hints so that it is capable of deciding which keys are valid?
For example: "When print_props is called with an instance of SubB only "foo" and "zap" are valid."
I took a look at generics; I think it is possible to declare a type variable that is restricted to sub types of Super, but is it possible to express the dependency between the concrete type of the type variable (SubA or SubB) and the literal values key should then be restricted to?
SubAandSubBaren't subtypes ofSuper, though. They subclass fromTypedDict.