October 10th, 2024

TypedDicts are better than you think

TypedDicts in Python 3.8 enhance type annotations for dictionaries, allowing optional fields, improving function signatures, and offering better type safety. Upcoming PEPs will introduce extra and read-only item features.

Read original articleLink Icon
TypedDicts are better than you think

TypedDicts, introduced in Python 3.8 through PEP-589, provide a way to create type annotations for dictionaries, enhancing the flexibility of data structures in Python. They are particularly useful for scenarios where not all fields are required, such as when implementing HTTP patch endpoints. For instance, a TypedDict can represent optional fields more effectively than dataclasses, which require all fields to be defined. The ability to specify fields as required or not required further increases the utility of TypedDicts. Additionally, PEP-692 allows for typing variadic keyword arguments using TypedDict, which can simplify function signatures and improve type checking. This is beneficial in scenarios like dependency injection, where TypedDicts can help ensure that the correct arguments are passed to functions. Upcoming features in PEP-728 and PEP-705 promise to enhance TypedDicts by allowing for the definition of extra items and read-only items, respectively. These advancements suggest that TypedDicts will continue to evolve, providing developers with more robust tools for type safety and data structure management in Python.

- TypedDicts enhance type annotations for dictionaries, allowing for optional fields.

- They are particularly useful for HTTP patch operations and dependency injection.

- PEP-692 enables typing of variadic keyword arguments, improving function signatures.

- Upcoming PEPs will introduce features for defining extra and read-only items in TypedDicts.

- TypedDicts offer better type safety and flexibility compared to traditional dataclasses.

Link Icon 10 comments
By @binary132 - 7 months
I feel like the dynamic typing crowd are slowly coming around to the fact that having a typesystem is just better.

While we’re on the topic, a better typesystem is better than a worse typesystem. Thanks for coming to my TED talk.

By @miohtama - 7 months
Reasons to use dataclass(slots=True) instead of TypedDict

- Faster attribute access: your code is faster

- Slotted classes take less RAM, less L1 cache pressure, your code is faster

- Wrist friendly to with .foobar instead of ["foobar"]

- Runtime error if you misspell an attribute name

By @calibas - 7 months
I feel like this needs to be mentioned more: If you don't have some kind of system to enforce types, then TypedDict does nothing at all.

You can store a float in an attribute annotated as a string, and default Python will not stop you, or display any kind of warning. The typing is purely for development, and does nothing once compiled.

If you want typing to actually be enforced, you need to use something like Pydantic.

By @jjtheblunt - 7 months
"thank you think" in a title is overtly condescending...you know your audience and their educational deficiencies?

"than i thought" would be compelling to read.

By @flysand7 - 7 months
> Here we have a problem, for subscription does None mean don't change or remove subscription

I read this like 10 times and I still dont understand this mess of grammar. Am I having a fucking stroke rn?

By @psd1 - 7 months
The article doesn't mention my train for loving TypedDict.

I worked on a python app built by a man with a stronger will to succeed than ability to code.

Data was passed as dicts. Many methods (in the god object, natch) destructured, unwrapped or mutated before passing the new dict to some other method.

TypedDict allowed me to annotate these shitty dicts. It became possible to reason about the code without spending two hours tracing code paths.

The real solution is to be better at code. But, given an app written by a dict fetishist, it's a pretty good solution.

By @benmmurphy - 7 months

  <Option<Option<subscription>>
By @mistahchris - 7 months
the link is 404ing, but i found the post in the author's github repo: https://github.com/Jamie-Chang/Jamie-Blog/blob/18c0aaf2266de...
By @BiteCode_dev - 7 months
TypedDict features are nice, but the syntax to declare it is aweful