Skip to content

Commit 0819777

Browse files
authored
[0.8] remove pydantic as a base class of state (#5396)
* remove pydantic as a base class of state * fix missing docstring * precommit * fix uni tests * make test upload more resillient * what * cancel faster maybe? * make internet slower * fix for rx.field
1 parent 2d74925 commit 0819777

File tree

11 files changed

+537
-219
lines changed

11 files changed

+537
-219
lines changed

reflex/compiler/utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from typing import Any
1212
from urllib.parse import urlparse
1313

14-
from pydantic.v1.fields import ModelField
15-
1614
from reflex import constants
1715
from reflex.components.base import (
1816
Body,
@@ -34,7 +32,7 @@
3432
from reflex.utils.exec import is_in_app_harness
3533
from reflex.utils.imports import ImportVar, ParsedImportDict
3634
from reflex.utils.prerequisites import get_web_dir
37-
from reflex.vars.base import Var
35+
from reflex.vars.base import Field, Var
3836

3937
# To re-export this function.
4038
merge_imports = imports.merge_imports
@@ -212,7 +210,7 @@ def compile_state(state: type[BaseState]) -> dict:
212210

213211

214212
def _compile_client_storage_field(
215-
field: ModelField,
213+
field: Field,
216214
) -> tuple[
217215
type[Cookie] | type[LocalStorage] | type[SessionStorage] | None,
218216
dict[str, Any] | None,

reflex/components/component.py

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import dataclasses
88
import functools
99
import inspect
10-
import sys
1110
import typing
1211
from abc import ABC, ABCMeta, abstractmethod
1312
from collections.abc import Callable, Iterator, Mapping, Sequence
@@ -20,10 +19,8 @@
2019
Annotated,
2120
Any,
2221
ClassVar,
23-
ForwardRef,
2422
Generic,
2523
TypeVar,
26-
_eval_type, # pyright: ignore [reportAttributeAccessIssue]
2724
cast,
2825
get_args,
2926
get_origin,
@@ -74,46 +71,6 @@
7471
from reflex.vars.object import ObjectVar
7572
from reflex.vars.sequence import LiteralArrayVar, LiteralStringVar, StringVar
7673

77-
78-
def resolve_annotations(
79-
raw_annotations: Mapping[str, type[Any]], module_name: str | None
80-
) -> dict[str, type[Any]]:
81-
"""Partially taken from typing.get_type_hints.
82-
83-
Resolve string or ForwardRef annotations into type objects if possible.
84-
85-
Args:
86-
raw_annotations: The raw annotations to resolve.
87-
module_name: The name of the module.
88-
89-
Returns:
90-
The resolved annotations.
91-
"""
92-
module = sys.modules.get(module_name, None) if module_name is not None else None
93-
94-
base_globals: dict[str, Any] | None = (
95-
module.__dict__ if module is not None else None
96-
)
97-
98-
annotations = {}
99-
for name, value in raw_annotations.items():
100-
if isinstance(value, str):
101-
if sys.version_info == (3, 10, 0):
102-
value = ForwardRef(value, is_argument=False)
103-
else:
104-
value = ForwardRef(value, is_argument=False, is_class=True)
105-
try:
106-
if sys.version_info >= (3, 13):
107-
value = _eval_type(value, base_globals, None, type_params=())
108-
else:
109-
value = _eval_type(value, base_globals, None)
110-
except NameError:
111-
# this is ok, it can be fixed with update_forward_refs
112-
pass
113-
annotations[name] = value
114-
return annotations
115-
116-
11774
FIELD_TYPE = TypeVar("FIELD_TYPE")
11875

11976

@@ -228,7 +185,7 @@ def __new__(cls, name: str, bases: tuple[type], namespace: dict[str, Any]) -> ty
228185
# Add the field to the class
229186
inherited_fields: dict[str, ComponentField] = {}
230187
own_fields: dict[str, ComponentField] = {}
231-
resolved_annotations = resolve_annotations(
188+
resolved_annotations = types.resolve_annotations(
232189
namespace.get("__annotations__", {}), namespace["__module__"]
233190
)
234191

reflex/event.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def substate_token(self) -> str:
9595
@dataclasses.dataclass(
9696
init=True,
9797
frozen=True,
98+
kw_only=True,
9899
)
99100
class EventActionsMixin:
100101
"""Mixin for DOM event actions."""
@@ -170,6 +171,7 @@ def temporal(self) -> Self:
170171
@dataclasses.dataclass(
171172
init=True,
172173
frozen=True,
174+
kw_only=True,
173175
)
174176
class EventHandler(EventActionsMixin):
175177
"""An event handler responds to an event to update the state."""
@@ -270,6 +272,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> EventSpec:
270272
@dataclasses.dataclass(
271273
init=True,
272274
frozen=True,
275+
kw_only=True,
273276
)
274277
class EventSpec(EventActionsMixin):
275278
"""An event specification.

0 commit comments

Comments
 (0)