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

pydantic Field with rx.foreach on state broken #4471

Open
benedikt-bartscher opened this issue Dec 3, 2024 · 4 comments · May be fixed by #4510
Open

pydantic Field with rx.foreach on state broken #4471

benedikt-bartscher opened this issue Dec 3, 2024 · 4 comments · May be fixed by #4510
Assignees
Labels
bug Something isn't working

Comments

@benedikt-bartscher
Copy link
Contributor

Describe the bug

Using pydantic Field in a state class fails with this error:

TypeError: You must provide an annotation for the state var tag. Annotation cannot be typing.Any.

IIRC this used to work and seems like a regression

To Reproduce

from pydantic.v1 import Field
import reflex as rx


class Tag(rx.Base):
    name: str = ""


class State(rx.State):
    tags: list[Tag] = Field(default_factory=list)


def index() -> rx.Component:
    return rx.container(
        rx.foreach(
            State.tags,
            lambda tag: rx.text(tag.name),
        )
    )


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

Expected behavior
no exception

Specifics (please complete the following information):

  • Python Version: 3.13
  • Reflex Version: main branch
  • OS: arch
  • Browser (Optional): --

Additional context

Copy link

linear bot commented Dec 3, 2024

@benedikt-bartscher
Copy link
Contributor Author

@adhami3310 maybe we can add our own default_factory to rx.field?

@adhami3310
Copy link
Member

maybe we can add our own default_factory to rx.field

for now rx.field does nothing, it's empirical that we need to move off of pydantic for our state classes, but for now it will remain a noop

as for the issue itself, i'm not sure where this got broken, we should probably bisect it

@Lendemor Lendemor added the bug Something isn't working label Dec 9, 2024
@masenf masenf assigned masenf and unassigned adhami3310 Dec 10, 2024
Copy link
Collaborator

masenf commented Dec 10, 2024

The crux of the issue is that rx.foreach does not quite handle optional var type, and assigning to Field causes the annotation to be wrapped in an Optional.

masenf added a commit that referenced this issue Dec 10, 2024
When determining whether a state var should be marked as optional, check that
it is missing both default and default_factory and is not required.

Fix #4471
@benedikt-bartscher benedikt-bartscher changed the title pydantic Field on state broken pydantic Field with rx.foreach on state broken Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants