Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions reflex/compiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from typing import Any
from urllib.parse import urlparse

from pydantic.v1.fields import ModelField

from reflex import constants
from reflex.components.base import (
Body,
Expand All @@ -34,7 +32,7 @@
from reflex.utils.exec import is_in_app_harness
from reflex.utils.imports import ImportVar, ParsedImportDict
from reflex.utils.prerequisites import get_web_dir
from reflex.vars.base import Var
from reflex.vars.base import Field, Var

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


def _compile_client_storage_field(
field: ModelField,
field: Field,
) -> tuple[
type[Cookie] | type[LocalStorage] | type[SessionStorage] | None,
dict[str, Any] | None,
Expand Down
45 changes: 1 addition & 44 deletions reflex/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dataclasses
import functools
import inspect
import sys
import typing
from abc import ABC, ABCMeta, abstractmethod
from collections.abc import Callable, Iterator, Mapping, Sequence
Expand All @@ -20,10 +19,8 @@
Annotated,
Any,
ClassVar,
ForwardRef,
Generic,
TypeVar,
_eval_type, # pyright: ignore [reportAttributeAccessIssue]
cast,
get_args,
get_origin,
Expand Down Expand Up @@ -74,46 +71,6 @@
from reflex.vars.object import ObjectVar
from reflex.vars.sequence import LiteralArrayVar, LiteralStringVar, StringVar


def resolve_annotations(
raw_annotations: Mapping[str, type[Any]], module_name: str | None
) -> dict[str, type[Any]]:
"""Partially taken from typing.get_type_hints.

Resolve string or ForwardRef annotations into type objects if possible.

Args:
raw_annotations: The raw annotations to resolve.
module_name: The name of the module.

Returns:
The resolved annotations.
"""
module = sys.modules.get(module_name, None) if module_name is not None else None

base_globals: dict[str, Any] | None = (
module.__dict__ if module is not None else None
)

annotations = {}
for name, value in raw_annotations.items():
if isinstance(value, str):
if sys.version_info == (3, 10, 0):
value = ForwardRef(value, is_argument=False)
else:
value = ForwardRef(value, is_argument=False, is_class=True)
try:
if sys.version_info >= (3, 13):
value = _eval_type(value, base_globals, None, type_params=())
else:
value = _eval_type(value, base_globals, None)
except NameError:
# this is ok, it can be fixed with update_forward_refs
pass
annotations[name] = value
return annotations


FIELD_TYPE = TypeVar("FIELD_TYPE")


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

Expand Down
3 changes: 3 additions & 0 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def substate_token(self) -> str:
@dataclasses.dataclass(
init=True,
frozen=True,
kw_only=True,
)
class EventActionsMixin:
"""Mixin for DOM event actions."""
Expand Down Expand Up @@ -170,6 +171,7 @@ def temporal(self) -> Self:
@dataclasses.dataclass(
init=True,
frozen=True,
kw_only=True,
)
class EventHandler(EventActionsMixin):
"""An event handler responds to an event to update the state."""
Expand Down Expand Up @@ -270,6 +272,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> EventSpec:
@dataclasses.dataclass(
init=True,
frozen=True,
kw_only=True,
)
class EventSpec(EventActionsMixin):
"""An event specification.
Expand Down
Loading