-
Notifications
You must be signed in to change notification settings - Fork 119
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
Refactoring Redis OM Python to use pydantic 2.0 types and validators #603
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
42ef61d
refactoring model to use pydantic 2.0 types and validators
slorello89 69cb9ef
couple more tests
slorello89 69c7891
lintining
slorello89 cc6b4b7
more linting
slorello89 dc3107c
bumpying mypy version
slorello89 1571c4a
more linting
slorello89 5a01b1d
adding tests for #591
slorello89 1098942
adding tests with uuid
slorello89 37bf652
readme fixes
slorello89 31d99ac
addressing comments
slorello89 8f46dd9
fixing typo in NOT_IN
slorello89 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,99 @@ | ||
from dataclasses import dataclass, is_dataclass | ||
from typing import ( | ||
Any, | ||
Callable, | ||
Deque, | ||
Dict, | ||
FrozenSet, | ||
List, | ||
Mapping, | ||
Sequence, | ||
Set, | ||
Tuple, | ||
Type, | ||
Union, | ||
) | ||
|
||
from pydantic.version import VERSION as PYDANTIC_VERSION | ||
from typing_extensions import Annotated, Literal, get_args, get_origin | ||
|
||
|
||
PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.") | ||
|
||
if PYDANTIC_V2: | ||
from pydantic.v1 import BaseModel, validator | ||
from pydantic.v1.fields import FieldInfo, ModelField, Undefined, UndefinedType | ||
from pydantic.v1.json import ENCODERS_BY_TYPE | ||
from pydantic.v1.main import ModelMetaclass, validate_model | ||
|
||
def use_pydantic_2_plus(): | ||
return True | ||
|
||
from pydantic import BaseModel, TypeAdapter | ||
from pydantic import ValidationError as ValidationError | ||
from pydantic import validator | ||
from pydantic._internal._model_construction import ModelMetaclass | ||
from pydantic._internal._repr import Representation | ||
from pydantic.deprecated.json import ENCODERS_BY_TYPE | ||
from pydantic.fields import FieldInfo | ||
from pydantic.v1.main import validate_model | ||
from pydantic.v1.typing import NoArgAnyCallable | ||
from pydantic.v1.utils import Representation | ||
from pydantic_core import PydanticUndefined as Undefined | ||
from pydantic_core import PydanticUndefinedType as UndefinedType | ||
|
||
@dataclass | ||
class ModelField: | ||
field_info: FieldInfo | ||
name: str | ||
mode: Literal["validation", "serialization"] = "validation" | ||
|
||
@property | ||
def alias(self) -> str: | ||
a = self.field_info.alias | ||
return a if a is not None else self.name | ||
|
||
@property | ||
def required(self) -> bool: | ||
return self.field_info.is_required() | ||
|
||
@property | ||
def default(self) -> Any: | ||
return self.get_default() | ||
|
||
@property | ||
def type_(self) -> Any: | ||
return self.field_info.annotation | ||
|
||
def __post_init__(self) -> None: | ||
self._type_adapter: TypeAdapter[Any] = TypeAdapter( | ||
Annotated[self.field_info.annotation, self.field_info] | ||
) | ||
|
||
def get_default(self) -> Any: | ||
if self.field_info.is_required(): | ||
return Undefined | ||
return self.field_info.get_default(call_default_factory=True) | ||
|
||
def validate( | ||
self, | ||
value: Any, | ||
values: Dict[str, Any] = {}, # noqa: B006 | ||
*, | ||
loc: Tuple[Union[int, str], ...] = (), | ||
) -> Tuple[Any, Union[List[Dict[str, Any]], None]]: | ||
return ( | ||
self._type_adapter.validate_python(value, from_attributes=True), | ||
None, | ||
) | ||
|
||
def __hash__(self) -> int: | ||
# Each ModelField is unique for our purposes, to allow making a dict from | ||
# ModelField to its JSON Schema. | ||
return id(self) | ||
|
||
else: | ||
from pydantic import BaseModel, validator | ||
from pydantic.fields import FieldInfo, ModelField, Undefined, UndefinedType | ||
from pydantic.json import ENCODERS_BY_TYPE | ||
from pydantic.main import ModelMetaclass, validate_model | ||
from pydantic.typing import NoArgAnyCallable | ||
from pydantic.utils import Representation | ||
|
||
def use_pydantic_2_plus(): | ||
return False |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's helpful, I have seen some shim implementations that just use try except.. but this may not necessarily be better. Just offering some other ways to approach this:
https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/pydantic_v1/dataclasses.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is more or less the way that FastAPI does it. a try/except might be a bit more inclusive. . . hmm thoughts @banker or @bsbodden ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try/except approach makes me feel icky. Are we thinking of dropping Pydantic 1.0 support completely? It's seems a lot of project are doing that nowadays.