Suppose I have some code like
def get_x(d: dict) -> int:
d["x"]
However, I want to tell mypy that d
should only contain certain keys (only the "x" key for example). That way, if I make a mistake lower in the code trying to reference an invalid key of d
, mypy will trigger an error.
My question is:
- Is this possible? Can mypy validate dictionary keys?
- If yes, how is this done? If no, is there a preferred workaround?