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

Address an issue with pydantic v2 models as Vars #3396

Merged
merged 3 commits into from
Jun 7, 2024

Conversation

kroo
Copy link
Contributor

@kroo kroo commented May 28, 2024

It looks like there's an issue with state vars which are pydantic v2 models... Here's a reproducible test case:

import reflex as rx
from pydantic import BaseModel
from reflex.utils.serializers import serializer

class User(BaseModel):
    has_image: bool = False

@serializer
def serialize_user(user: User) -> dict:
    return user.dict()

class State(rx.State):
    user: User = None

def index() -> rx.Component:
    return rx.container(
        rx.cond(State.user,
                rx.text(State.user.has_image),
                rx.text("No user"))
    )

app = rx.App()
app.add_page(index)

This app works only with pydantic <2 installed:

reflex-test $ reflex run
...
AttributeError: 'FieldInfo' object has no attribute 'outer_type_'
reflex-test $ pip install pydantic==1.10.15
─────────────────────────────────── Starting Reflex App ───────────────────────────────────
Compiling: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 13/13 0:00:00
─────────────────────────────────────── App Running ───────────────────────────────────────
App running at: http://localhost:3000

Looks like this is caused by outer_type_ no longer existing in pydantic v2. I'm guessing this was introduced back in v0.4.6.

This change explicitly ignores pydantic v2 models in get_attribute_access_type, rather than trying to treat them as v1 models.

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you successfully ran tests with your changes locally?

It looks like there's an issue with state vars
which are pydantic v2 models...  Here's a
reproducible test case:

```python
import reflex as rx
from pydantic import BaseModel
from reflex.utils.serializers import serializer

class User(BaseModel):
    has_image: bool = False

@Serializer
def serialize_user(user: User) -> dict:
    return user.dict()

class State(rx.State):
    user: User = None

def index() -> rx.Component:
    return rx.container(
        rx.cond(State.user,
                rx.text(State.user.has_image),
                rx.text("No user"))
    )

app = rx.App()
app.add_page(index)
```

This app works only with pydantic <2 installed:

```bash
reflex-test $ reflex run
...
AttributeError: 'FieldInfo' object has no attribute 'outer_type_'
reflex-test $ pip install pydantic==1.10.15
─────────────────────────────────── Starting Reflex App ───────────────────────────────────
Compiling: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 13/13 0:00:00
─────────────────────────────────────── App Running ───────────────────────────────────────
App running at: http://localhost:3000
```

Looks like this is caused by `outer_type_` no
[longer existing][1] in pydantic v2.  I'm guessing
this was introduced back in [v0.4.6][2].

1: pydantic/pydantic#7217
2: reflex-dev@86526cb

This change explicitly ignores pydantic v2 models in
`get_attribute_access_type`, rather than trying to treat
them as v1 models.
kroo added a commit to kroo/reflex-clerk that referenced this pull request May 28, 2024
@kroo kroo changed the title Addresses an issue with pydantic v2 models as Vars Address an issue with pydantic v2 models as Vars May 29, 2024
@masenf masenf merged commit 168501d into reflex-dev:main Jun 7, 2024
47 checks passed
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 this pull request may close these issues.

2 participants