Skip to content
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

Use settable __pydantic_fields__ #32

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions iceaxe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def __new__(
mcs._cached_args[cls] = raw_kwargs

# If we have already set the class's fields, we should wrap them
if hasattr(cls, "model_fields"):
cls.model_fields = {
if hasattr(cls, "__pydantic_fields__"):
cls.__pydantic_fields__ = {
field: info
if isinstance(info, DBFieldInfo)
else DBFieldInfo.extend_field(
Expand Down Expand Up @@ -98,6 +98,14 @@ def _extract_kwarg(cls, kwargs: dict[str, Any], key: str, default: Any = None):

return default

@property
def model_fields(self) -> dict[str, DBFieldInfo]: # type: ignore
# model_fields must be reimplemented in our custom metaclass, otherwise
# clients will get the super typehinting signature when they try
# to access Model.model_fields. This overrides the ClassVar typehint
# that's placed in the TableBase itself.
return super().model_fields # type: ignore


class UniqueConstraint(BaseModel):
columns: list[str]
Expand Down
11 changes: 9 additions & 2 deletions iceaxe/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

P = ParamSpec("P")

_Unset: Any = PydanticUndefined


class DBFieldInputs(_FieldInfoInputs, total=False):
primary_key: bool
Expand Down Expand Up @@ -116,11 +118,16 @@ def func(
index: bool = False,
check_expression: str | None = None,
is_json: bool = False,
default: Any = PydanticUndefined,
default: Any = _Unset,
default_factory: (
Callable[[], Any] | Callable[[dict[str, Any]], Any] | None
) = _Unset,
*args: P.args,
**kwargs: P.kwargs,
):
raw_field = PydanticField(default=default, **kwargs) # type: ignore
raw_field = PydanticField(
default=default, default_factory=default_factory, **kwargs
) # type: ignore

# The Any request is required for us to be able to assign fields to any
# arbitrary type, like `value: str = Field()`
Expand Down
Loading
Loading