On 1/25/21 12:09 AM, Tuukka Mustonen wrote:
Just want to note that while this would be a good addition:
class MyTypedDict(TypedDict, total=False): foo: Required[str] bar: Required[int|None] baz: float|None qed: bool
...it's still "the wrong way around". This is what people usually want:
class MyTypedDict(TypedDict): foo: str bar: int|None baz: float|None|Missing qed: bool|Missing
I agree that it would be preferable to introduce a spelling for "optional key" rather than "required key", but there just doesn't seem to be a way to get a reasonable spelling: * We can't easily introduce any new single-word wrapper like Missing[...] because any good name that means "optional key" will necessarily be too close to Optional[...] and will be confusing when used in combination with Optional[...]. For example Optional[Missing[...]] is confusing. * So that leaves us with spelling the *opposite* of "optional key", which is a "required key". There's no name collision with any existing typing construct by just using Required[...], which is the obvious name for a "required key" wrapper.
Were there actually any comments on Daniel Moisset's suggestion of `| Missing`? Guido replied but maybe I don't understand the difference - if `Required[str]`, which defines the existence of the field (left side) attached to the type (right side), is considered acceptable approach, what's bad with `x|Missing`, which simply does the same?
The syntax `x|Missing` implies the introduction of a new constant (`Missing`) that's like `None`. Using such a constant as a magic flag is unusual. We typically use wrappers like Final[...], Annotated[...], etc instead. -- David Foster | Seattle, WA, USA Contributor to TypedDict support for mypy