-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds typed dict extract fields support (#1253)
Adds typeddict support for extract_fields This enables more ergonomic usage of TypedDict with extract fields. I skipped adding support for returning any `Mapping`. Though that should be an easy addition. ```python from typing import TypedDict from hamilton.function_modifiers import extract_fields class MyDict(TypedDict): foo: str bar: int @extract_fields() def some_function() -> MyDict: return MyDict(foo="s", bar=1) ``` The above will automatically extract the fields foo and bar. You can also do: ```python from typing import TypedDict from hamilton.function_modifiers import extract_fields class MyDict(TypedDict): foo: str bar: int @extract_fields({"foo": str}) def some_function()->MyDict: return MyDict(foo="s", bar=1) ``` To only expose a subset of the fields. Squashed commits: * Adds sketch of improving extract_fields with typeddict This in response to #1252. We should be able to handle typeddict better. This sketches some ideas: 1. field validation should happen in .validate() not the constructor. 2. extract_fields shouldn't need fields if the typeddict is the annotation type. 3. we properly check that typeddict can be a return type. * Adds typeddict tests * Adding validation to cover all extract_fields paths * Adds Typeddict Extract fields subclass type check and test for it
- Loading branch information
Showing
2 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters