Hi @alicederyn I’m glad you’re working on TypedMapping! This has been an idea for a while.
Some feedback from reading the PEP:
Abstract
- It might be nice to say up-front that:
- TypedMapping is like a TypedDict but with read-only keys.
- (I think this message is very easy to understand.)
- It implements only the read-only methods from
Mapping
but not the write methods fromdict
.- (Mentioning
Mapping
gives a hint/explanation about why this new type is called a TypedMapping.)
- (Mentioning
- TypedMapping is like a TypedDict but with read-only keys.
-
As
TypedDict
is a mutable type, it is difficult to correctly annotate methods which accept read-only parameters in a way that doesn’t prevent valid inputs.- A more direct phrasing might be: “TypedMapping can be used to annotate methods which accept read-only dictionary-like parameters, unlike TypedDict which also permits write operations.”
- (I think a phrasing that emphasizes what TypedMapping is rather than what it is not is easier to understand.)
- A more direct phrasing might be: “TypedMapping can be used to annotate methods which accept read-only dictionary-like parameters, unlike TypedDict which also permits write operations.”
Specification
-
- The runtime type of a
TypedMapping
object is not constrained to be adict
.
- Probably worth being explicit that it is constrained to be a
Mapping
. - May want to add/clarify: …although it can be a
dict
.- I speculate dict literals will be a common way to create instances of TypedMapping, so it’s nice to mention clearly that its possible to do.
- The runtime type of a
-
Multiple inheritance and TypedDict
- I feel a bit uneasy about permitting a class to inherit from both a TypedMapping and a TypedDict (or even a vanilla dict) at the same time. It makes a partially mutable type, muddying the distinction between a fully immutable and a fully mutable type. Maybe this feature could be omitted from the PEP?
- Are there compelling use cases for defining a dict with some fields as mutable and others as immutable? (I can’t think of any easily.)
- If there are such cases, would it make more sense to propose a syntax to mark individual fields of a TypedDict as read-only instead?
- Perhaps something like the
Required[]
orNotRequired[]
syntax could be used, maybe usingFinal[]
to mark read-only values?
- Perhaps something like the
- I feel a bit uneasy about permitting a class to inherit from both a TypedMapping and a TypedDict (or even a vanilla dict) at the same time. It makes a partially mutable type, muddying the distinction between a fully immutable and a fully mutable type. Maybe this feature could be omitted from the PEP?
Backwards Compatibility
-
This PEP changes the rules for how
TypedDict
behaves (allowing subclasses to inherit fromTypedMapping
protocols […]- FYI: If we remove the ability to inherit from both TypedDict and TypedMapping at the same time, as suggested above, this backward compatibility consideration goes away, leaving no notable backward compatibility considerations remaining.
Reference Implementation
-
No reference implementation exists yet.
- I’d recommend getting an implementation drafted/reviewed for at least one typechecker (ex: mypy, pyright) to discover any remaining edge cases in this PEP’s specification that would be worth mentioning, before submitting the PEP for Steering Council approval.
Fin
Thanks again for taking the time to bring TypedMapping to life!