Is it possible to enforce the presence of certain keys but ignore all others? My use case is parsing JSON payloads from an event bus where I want to ignore fields that may be present but that I don't care to include in the TypedDict.
This functionality would be similar to json-schema's additionalProperties: true keyword.
For example:
from typing import TypedDict, NotRequired
from trycast import trycast
class MyType(TypedDict):
a: str
b: NotRequired[str | None]
# currently fails validation because "c" is in the dict
trycast(MyType, {"a": "foo", "b": "bar", "c": "baz"})
Is it possible to enforce the presence of certain keys but ignore all others? My use case is parsing JSON payloads from an event bus where I want to ignore fields that may be present but that I don't care to include in the
TypedDict.This functionality would be similar to json-schema's
additionalProperties: truekeyword.For example: