reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
20.34k stars 1.17k forks source link

rx.Field pydantic validation issues #4235

Closed benedikt-bartscher closed 1 week ago

benedikt-bartscher commented 3 weeks ago

Describe the bug

cc @adhami3310

E           pydantic.v1.error_wrappers.ValidationError: 1 validation error for TypedState
E           base
E             instance of Field expected (type=type_error.arbitrary_type; expected_arbitrary_type=Field)

To Reproduce

def test_typed_state() -> None:
    class TypedState(rx.State):
        base: rx.Field[str] = rx.field("")

    _ = TypedState(base="str")

Expected behavior no errror

Specifics (please complete the following information):

linear[bot] commented 3 weeks ago

ENG-3992 rx.Field pydantic validation issues

adhami3310 commented 3 weeks ago

i assumed this would be the case, i can prob turn off validation for that type? or modify it?

anyways, how do you hit this in normal reflex code?

benedikt-bartscher commented 3 weeks ago

i assumed this would be the case, i can prob turn off validation for that type? or modify it?

anyways, how do you hit this in normal reflex code?

i am just hitting this in a pytest, seems not to happen at runtime

benedikt-bartscher commented 3 weeks ago

If the validation does not run during normal runtime, I would suggest disabling it for pytest as well. Otherwise, we could try to "unwrap" the annotation/type f.e. in __init_subclass__ in a way that pydantic recognizes the inner type

We could then add some tests for the new typed events and vars. Btw, this is awesome, thank you very much for the new typing experience (I just removed hundreds of ignore comments).

Unrelated to this issue, but could we add @rx.event typing for @rx.background? It currently works if I chain both decorators.

adhami3310 commented 3 weeks ago

could we add @rx.event typing for @rx.background? It currently works if I chain both decorators

we're thinking of doing @rx.event(background=True), anyways you can make an issue so i remember to do this