PEP 705: Read-only TypedDict items

Overall I like this PEP and I look forward to being able to declare TypedDict as read-only. A few pieces of feedback:

  • The runtime implementation in typing-extensions throws an error if a mutable key overrides a read-only one in a base class. I think I put that in because PEP 705 at the time specified this behavior, but the current version doesn’t seem to say this explicitly. I would like to drop this runtime check, as it’s generally better for the runtime to be permissive. For example, users may want to type ignore this particular error in some cases, or future extensions of TypedDict may come up that interfere with this check.
  • I was sad to see that the readonly=True class argument is no longer in the PEP. I would personally want to default to declaring TypedDicts as read-only when possible, so I’d like to make that as easy as possible. The current iteration of the PEP would require repeating ReadOnly[] on every item, which is verbose.
  • Relatedly, there is a lack of symmetry with the feature set available for stating whether keys are required. In that area, we can mark an individual key as Required or NotRequired and set the default for the whole TypedDict (total=False). But for marking a TypedDict read-only, the only option is to use ReadOnly[] to mark an individual key. For more flexibility, we could add a read_only=True class argument and a Mutable[] qualifier to mark individual keys as mutable in a TypedDict that defaults to read-only.

I’ll be out for the next few weeks, but wanted to get these ideas into the discussion.

3 Likes