Skip to content

Commit 5f468da

Browse files
committed
Monkeypatch rx.Base for python3.14 compatible annotations
1 parent 9099a2e commit 5f468da

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

reflex/base.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
if find_spec("pydantic") and find_spec("pydantic.v1"):
66
from pydantic.v1 import BaseModel
77

8-
class Base(BaseModel):
8+
from reflex.utils.compat import ModelMetaclassLazyAnnotations
9+
10+
class Base(BaseModel, metaclass=ModelMetaclassLazyAnnotations):
911
"""The base class subclassed by all Reflex classes.
1012
1113
This class wraps Pydantic and provides common methods such as

reflex/utils/compat.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,31 @@ async def windows_hot_reload_lifespan_hack():
3131
pass
3232

3333

34+
if find_spec("pydantic") and find_spec("pydantic.v1"):
35+
from pydantic.v1.main import ModelMetaclass
36+
37+
class ModelMetaclassLazyAnnotations(ModelMetaclass):
38+
"""Compatibility metaclass to resolve python3.14 style lazy annotations."""
39+
40+
def __new__(mcs, name: str, bases: tuple, namespace: dict, **kwargs):
41+
"""Resolve python3.14 style lazy annotations before passing off to pydantic v1.
42+
43+
Args:
44+
name: The class name.
45+
bases: The base classes.
46+
namespace: The class namespace.
47+
**kwargs: Additional keyword arguments.
48+
49+
Returns:
50+
The created class.
51+
"""
52+
if (_anotate := namespace.get("__annotate_func__")) is not None:
53+
namespace["__annotations__"] = _anotate(0)
54+
return super().__new__(mcs, name, bases, namespace, **kwargs)
55+
else:
56+
ModelMetaclassLazyAnnotations = type # type: ignore[assignment]
57+
58+
3459
def sqlmodel_field_has_primary_key(field_info: "FieldInfo") -> bool:
3560
"""Determines if a field is a primary.
3661

0 commit comments

Comments
 (0)