This is an interesting idea, but I wonder if it’s worth generalizing a bit more. With PEP 705, we’ll also have the ReadOnly qualifier on TypedDicts, and it’s natural to want a read-only version of an existing TypedDict. What if we could instead write:
Map[NotRequired, SomeTypedDict]
Map[ReadOnly, SomeTypedDict]
(And also Map[Required, SomeTypedDict]
, though I don’t know when you’d want that.)
Map
has of course been proposed in other contexts too, mostly around TypeVarTuple. As long as the different uses are conceptually similar, I don’t think that should block us from using it in multiple places in the type system.
There’s also desire in this thread for supporting something like Partial on dataclasses, which might mean something like “all fields may be None”. That idea creates bigger issues, because dataclasses are nominal and not structural types. Still, if we find a coherent way to add this concept to the type system, it could also be expressed using Map[]
, though I’m struggling to find a good spelling:
Map[Optional, SomeDataclass]
(I’d like to get rid ofOptional
, and doesn’t generalize to unions with other values)Map[None.__or__, SomeDataclass]
(yuck)Map[lambda T: T | None, SomeDataclass]
(yuck)Map[InputType | None, SomeDataclass]
, whereInputType
is a new primitive intyping
(generalizes well and doesn’t look ugly, but introduces new complexity)
Still, the conceptual problems with structural transformations on a nominal type mean that this shouldn’t be in the initial PEP proposing the concept for TypedDicts.