Skip to content

Commit

Permalink
Address an issue with pydantic v2 models as Vars (#3396)
Browse files Browse the repository at this point in the history
* Addresses an issue with pydantic v2 models as Vars

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: 86526cb

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

* ruff formatting

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
  • Loading branch information
kroo and masenf authored Jun 7, 2024
1 parent 4e990d2 commit 168501d
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion reflex/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ def get_attribute_access_type(cls: GenericType, name: str) -> GenericType | None
attr = getattr(cls, name, None)
if hint := get_property_hint(attr):
return hint
if hasattr(cls, "__fields__") and name in cls.__fields__:
if (
hasattr(cls, "__fields__")
and name in cls.__fields__
and hasattr(cls.__fields__[name], "outer_type_")
):
# pydantic models
field = cls.__fields__[name]
type_ = field.outer_type_
Expand Down

0 comments on commit 168501d

Please sign in to comment.