4 ms·
You'll have issues if you ever rename things in the dict. Linting tools will pick up on every instance where you forgot to rename the fields of a class, but wo
by imron 2y ago
You'll have issues if you ever rename things in the dict.
Linting tools will pick up on every instance where you forgot to rename the fields of a class, but won't do the same for dicts.
- FreakLegion 2y agoTypedDicts solve the linting problem, but refactoring tools haven't caught up (unlike e.g. ForwardRef type annotations, which are strings but can be transformed alongside type literals).
- tomjakubowski 2y agoIs there any advantage to using a TypedDict for a record over a dataclass?
- FreakLegion 2y agoTypedDicts "aren't real" in the sense that they're a compile-time feature, so you're getting typing without any deserialization cost beyond the original JSON. Dataclasses and Pydantic models are slow to construct, so that's not nothing. This of course means TypeDicts don't give you run-time validation. For that, and for full-blown custom types in general, I tend to favor msgspec Structs: https://jcristharif.com/msgspec/benchmarks.html#json-serialization-validation https://jcristharif.com/msgspec/benchmarks.html#json-seriali....
- orf 2y ago> Dataclasses and Pydantic models are slow to construct Citation needed? Pydantic is really quite fast, and you can pass raw JSON responses into it. It may be slower (depending on the validators or structure), but I’d expect it to be comparably fast to the stdlib JSON module.
- FreakLegion 2y agoPydantic's JSON parsing is faster than the built-in module, on par with orjson, but creating model instances and run-time type checking net out to be much slower. I linked msgspec's benchmarks in the previous post.
- newaccountman2 2y ago> Citation needed? Pydantic is really quite fast Pydantic v1 was slow enough for them to write a lot of the core logic in Rust for Pydantic v2, and for the previous sloth to have been an argument people launched against it if you look back at threads on here and Reddit comparing it to other libraries.