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

BUG: Default Values in Model Are Incorrectly Set as 'ExpressionProxy' Objects #669

Open
shreypandey opened this issue Nov 16, 2024 · 3 comments · May be fixed by #673
Open

BUG: Default Values in Model Are Incorrectly Set as 'ExpressionProxy' Objects #669

shreypandey opened this issue Nov 16, 2024 · 3 comments · May be fixed by #673

Comments

@shreypandey
Copy link

When defining default values in a JsonModel, these values are unexpectedly being set as ExpressionProxy objects instead of the intended default values.

Expected Behavior:

Default values defined in the model should be directly set as the specified values.

Actual Behavior:

Default values are replaced with ExpressionProxy objects.

Steps to reproduce:

When running the following code:

import datetime
from typing import Optional

from redis_om import JsonModel, Field


class Customer(JsonModel):
    first_name: str
    last_name: str
    email: str
    join_date: datetime.date
    age: int = Field(default=18)
    bio: Optional[str] = "Super dope"


class Child(Customer):
    is_new: bool = Field(default=True)


andrew = Child(
    first_name="Andrew",
    last_name="Brookins",
    email="[email protected]",
    join_date=datetime.date.today(),
)

print("Age Field: ", andrew.age)
print("Bio Field: ", andrew.bio)
print("Model Field:", andrew.model_dump())

produces the following output:

Age Field:  <redis_om.model.model.ExpressionProxy object at 0x7fb70dcdf8e0>
Bio Field:  <redis_om.model.model.ExpressionProxy object at 0x7fb70db8c160>
Model Field: {'pk': '01JCV9FESAVVNAVBG2XJSTQGNN', 'first_name': 'Andrew', 'last_name': 'Brookins', 'email': '[email protected]', 'join_date': datetime.date(2024, 11, 17), 'age': <redis_om.model.model.ExpressionProxy object at 0x7fb70dcdf8e0>, 'bio': <redis_om.model.model.ExpressionProxy object at 0x7fb70db8c160>, 'is_new': True}

Additional Notes:

However, replacing JsonModel by pydantic BaseModel works fine

import datetime
from typing import Optional

from pydantic import BaseModel
from redis_om import Field


class Customer(BaseModel):
    first_name: str
    last_name: str
    email: str
    join_date: datetime.date
    age: int = Field(default=18)
    bio: Optional[str] = "Super dope"


class Child(Customer):
    is_new: bool = Field(default=True)


andrew = Child(
    first_name="Andrew",
    last_name="Brookins",
    email="[email protected]",
    join_date=datetime.date.today(),
)

print("Age Field: ", andrew.age)
print("Bio Field: ", andrew.bio)
print("Model Field:", andrew.model_dump())

This code produces the following output:

Age Field:  18
Bio Field:  Super dope
Model Field: {'first_name': 'Andrew', 'last_name': 'Brookins', 'email': '[email protected]', 'join_date': datetime.date(2024, 11, 17), 'age': 18, 'bio': 'Super dope', 'is_new': True}

Environment

  • Python: 3.10.15
  • Implementation: CPython
  • Platform: linux
  • redis-om version: 0.3.3
  • pydantic version: 2.9.2
@slorello89
Copy link
Member

interesting, it appears to have something to do with the inheretence of the model, will have to look into it.

@sethbuff
Copy link

We are having this issue as well. It's going to prevent us from upgrading to pydantic 2.0

@slorello89 slorello89 linked a pull request Nov 20, 2024 that will close this issue
@slorello89
Copy link
Member

For some odd reason in child classes it looks like some context WRT the defaults is being lost, I opened a PR to fix it (it just applies the defaults).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants