-
Notifications
You must be signed in to change notification settings - Fork 4
Add DataclassAdapter using Pydantic TypeAdapter #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b89bb43
Add DataclassAdapter using Pydantic TypeAdapter
github-actions[bot] c317c01
Merge branch 'main' into claude/issue-140-20251027-0058
strawgate d591c4e
refactor: create BasePydanticAdapter to eliminate code duplication
github-actions[bot] 0481973
Address CodeRabbit review feedback
github-actions[bot] acace30
Syncronize the syncronous part of the library
strawgate 65993f5
Merge branch 'main' into claude/issue-140-20251027-0058
strawgate dc14b75
run codegen
strawgate 0ced606
Small PR Feedback
strawgate d503c0d
More PR Cleanup
strawgate 6215242
Docstring updates
strawgate 01c12b1
type checking improvements
strawgate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
key-value/key-value-aio/src/key_value/aio/adapters/base/__init__.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from key_value.aio.adapters.pydantic.base import BasePydanticAdapter | ||
|
|
||
| __all__ = ["BasePydanticAdapter"] | ||
3 changes: 3 additions & 0 deletions
3
key-value/key-value-aio/src/key_value/aio/adapters/dataclass/__init__.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from key_value.aio.adapters.dataclass.adapter import DataclassAdapter | ||
|
|
||
| __all__ = ["DataclassAdapter"] |
69 changes: 69 additions & 0 deletions
69
key-value/key-value-aio/src/key_value/aio/adapters/dataclass/adapter.py
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from dataclasses import is_dataclass | ||
| from typing import Any, TypeVar, get_args, get_origin | ||
|
|
||
| from key_value.shared.type_checking.bear_spray import bear_spray | ||
| from pydantic.type_adapter import TypeAdapter | ||
|
|
||
| from key_value.aio.adapters.pydantic.base import BasePydanticAdapter | ||
| from key_value.aio.protocols.key_value import AsyncKeyValue | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| class DataclassAdapter(BasePydanticAdapter[T]): | ||
| """Adapter around a KVStore-compliant Store that allows type-safe persistence of dataclasses. | ||
| This adapter works with both standard library dataclasses and Pydantic dataclasses, | ||
| leveraging Pydantic's TypeAdapter for robust validation and serialization. | ||
| """ | ||
|
|
||
| _inner_type: type[Any] | ||
|
|
||
| # Beartype cannot handle the parameterized type annotation (type[T]) used here for this generic dataclass adapter. | ||
| # Using @bear_spray to bypass beartype's runtime checks for this specific method. | ||
| @bear_spray | ||
| def __init__( | ||
| self, | ||
| key_value: AsyncKeyValue, | ||
| dataclass_type: type[T], | ||
| default_collection: str | None = None, | ||
| raise_on_validation_error: bool = False, | ||
| ) -> None: | ||
| """Create a new DataclassAdapter. | ||
| Args: | ||
| key_value: The KVStore to use. | ||
| dataclass_type: The dataclass type to use. Can be a single dataclass or list[dataclass]. | ||
| default_collection: The default collection to use. | ||
| raise_on_validation_error: Whether to raise a ValidationError if the model is invalid. | ||
| Raises: | ||
| TypeError: If dataclass_type is not a dataclass type. | ||
| """ | ||
strawgate marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._key_value = key_value | ||
|
|
||
| origin = get_origin(dataclass_type) | ||
| self._is_list_model = origin is list | ||
|
|
||
| # Extract the inner type for list models | ||
| if self._is_list_model: | ||
| args = get_args(dataclass_type) | ||
| if not args: | ||
| msg = f"List type {dataclass_type} must have a type argument" | ||
| raise TypeError(msg) | ||
| self._inner_type = args[0] | ||
| else: | ||
| self._inner_type = dataclass_type # type: ignore[assignment] | ||
|
|
||
| # Validate that the inner type is a dataclass | ||
| if not is_dataclass(self._inner_type): | ||
| msg = f"{self._inner_type} is not a dataclass" | ||
| raise TypeError(msg) | ||
|
|
||
| self._type_adapter = TypeAdapter[T](dataclass_type) | ||
| self._default_collection = default_collection | ||
| self._raise_on_validation_error = raise_on_validation_error | ||
|
|
||
| def _get_model_type_name(self) -> str: | ||
| """Return the model type name for error messages.""" | ||
| return "dataclass" | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.