PEP 705: Read-only TypedDict items

This came up in the discussion for creating a Readable type modifier: Need a way to type-hint attributes that is compatible with duck-typing - #19 by randolf-scholz

While for a TypedDict the distinction between a Readable and ReadOnly key barely matters, the distinction becomes more important when applied to a Protocol and nominal types.

Since according to the inheritance rules outlined in PEP 705 we are allowed to override a ReadOnly key with a writeable key it might be better to call the modifier Readable to ensure future consistency, if/when we want to extend the type modifier to work with additional types.

That being said, I do think the name ReadOnly better conveys the promise that this key will never be written to inside a function that uses it, so I understand why it is a more attractive name for the modifier, but it would also be a shame if we reject a future PEP that defines a Readable modifier that is distinct from ReadOnly based on the fact that this PEP was accepted with ReadOnly meaning Readable.

There is some precedent for the distinction between Readable and ReadOnly with the Buffer protocol placeholders in typeshed, where we have ReadableBuffer and ReadOnlyBuffer. These were introduced as part of an earlier version of PEP 688, which initially aimed to include mutability as part of the Buffer protocol.

1 Like