I prefer frozen datatypes as they prevent accidental modification of data that isn’t supposed to be modified.
Often I have to move around datas in dicts (i.e. read from a json).
To have this data properly typed I used TypedDict
.
But I can’t set the TypedDict
to “ReadOnly”.
Currently I do something like this
from typing import TypedDict
import json
import types
class Data(TypedDict):
foo: str
bar: int
def read_data() -> Data:
with open("file.json", "r") as f:
return cast(Data, types.MappingProxyType(json.load(f)))
Which works fine to prevent runtime modification of data. But I would like the TypeChecker to understand as well, that setting data is invalid.
Would it make sense to introduce a TypedMapping
to Python which is basically a TypedDict
but without the __setitem__
method and del methods.
If yes would it also make sense to create a class methods for TypedDict like TypeDict.from_typed_mapping(DataMapping)
and TypedMapping.from_typed_dict(Data)
?
This would help to only define larger TypedDicts and use them in different contexts.
Also this could help to write the types for types.MappingProxyType
. Because as I user I would expect that if I insert a TypedDict inside it, a TypedMapping is detected by the type checker.
I am aware that a Mapping[str, str]
is different of dict[str, str]
as Mapping[str, str]
can contain SubclassOfStr("a")
wheras dict[str, str]
can’t. That might be worth to consider.