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 articleTypedDicts, 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.
Related
Summary of Major Changes Between Python Versions
The article details Python updates from versions 3.7 to 3.12, highlighting async/await, Walrus operator, Type hints, F-strings, Assignment expressions, Typing enhancements, Structural Pattern Matching, Tomllib, and useful tools.
TensorDict: A GPU-accelerated Python dictionary
TensorDict is a PyTorch library for managing tensor data efficiently, enhancing code readability and modularity. It supports metadata, nested structures, and various tensor operations, with installation via pip or conda.
Approximating sum types in Python with Pydantic
Pydantic enables robust data models in Python, supporting sum types and discriminated unions for clear, type-safe definitions. It enhances maintainability and reliability by preventing invalid states in applications.
New Typing Features in Python 3.13
Python 3.13 will enhance its typing system with features like the ReadOnly type for TypedDicts, aiming to improve code reliability and developer productivity. The final release is set for October 1, 2024.
Don't let dicts spoil your code
Roman Imankulov critiques Python dictionaries for causing technical debt and complicating maintenance. He advocates for domain models, dataclasses, and Pydantic to enhance clarity and structure in evolving codebases.
While we’re on the topic, a better typesystem is better than a worse typesystem. Thanks for coming to my TED talk.
- 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
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.
"than i thought" would be compelling to read.
I read this like 10 times and I still dont understand this mess of grammar. Am I having a fucking stroke rn?
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.
<Option<Option<subscription>>
Related
Summary of Major Changes Between Python Versions
The article details Python updates from versions 3.7 to 3.12, highlighting async/await, Walrus operator, Type hints, F-strings, Assignment expressions, Typing enhancements, Structural Pattern Matching, Tomllib, and useful tools.
TensorDict: A GPU-accelerated Python dictionary
TensorDict is a PyTorch library for managing tensor data efficiently, enhancing code readability and modularity. It supports metadata, nested structures, and various tensor operations, with installation via pip or conda.
Approximating sum types in Python with Pydantic
Pydantic enables robust data models in Python, supporting sum types and discriminated unions for clear, type-safe definitions. It enhances maintainability and reliability by preventing invalid states in applications.
New Typing Features in Python 3.13
Python 3.13 will enhance its typing system with features like the ReadOnly type for TypedDicts, aiming to improve code reliability and developer productivity. The final release is set for October 1, 2024.
Don't let dicts spoil your code
Roman Imankulov critiques Python dictionaries for causing technical debt and complicating maintenance. He advocates for domain models, dataclasses, and Pydantic to enhance clarity and structure in evolving codebases.