Skip to content

Commit

Permalink
migrate pydantic config classes to ConfigDict
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher committed Mar 1, 2024
1 parent fbaea8d commit 6f3d582
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
12 changes: 6 additions & 6 deletions reflex/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ class Base(pydantic.BaseModel):
frontend and backend should subclass this class.
"""

class Config:
"""Pydantic config."""

arbitrary_types_allowed = True
use_enum_values = True
extra = "allow"
# Pydantic config
model_config = pydantic.ConfigDict(
arbitrary_types_allowed=True,
use_enum_values=True,
extra="allow",
)

def json(self) -> str:
"""Convert the object to a json string.
Expand Down
8 changes: 4 additions & 4 deletions reflex/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ def get_url(self) -> str:
class Config(Base):
"""A Reflex config."""

class Config:
"""Pydantic config for the config."""

validate_assignment = True
# Pydantic config
model_config = pydantic.ConfigDict(
validate_assignment=True,
)

# The name of the app.
app_name: str
Expand Down
19 changes: 9 additions & 10 deletions reflex/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from reflex.utils import console, format
from reflex.utils.types import ArgsSpec
from reflex.vars import BaseVar, Var
from pydantic import ConfigDict

if TYPE_CHECKING:
from reflex.state import BaseState
Expand Down Expand Up @@ -147,11 +148,10 @@ class EventHandler(EventActionsMixin):
# The function to call in response to the event.
fn: Any

class Config:
"""The Pydantic config."""

# Needed to allow serialization of Callable.
frozen = True
# Pydantic config
model_config = ConfigDict(
frozen=True, # Needed to allow serialization of Callable.
)

@property
def is_background(self) -> bool:
Expand Down Expand Up @@ -219,11 +219,10 @@ class EventSpec(EventActionsMixin):
# TODO: pydantic v2 add rx.Var type annotation?
args: Tuple[Tuple[Any, Any], ...] = ()

class Config:
"""The Pydantic config."""

# Required to allow tuple fields.
frozen = True
# Pydantic config
model_config = ConfigDict(
frozen=True, # Required to allow tuple fields.
)

def with_args(self, args: Tuple[Tuple[Var, Var], ...]) -> EventSpec:
"""Copy the event spec, with updated args.
Expand Down
3 changes: 1 addition & 2 deletions reflex/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def __init_subclass__(cls):
non_default_primary_key_fields = [
field_name
for field_name, field in cls.model_fields.items()
if field_name != "id"
and getattr(field, "primary_key", None) is True
if field_name != "id" and getattr(field, "primary_key", None) is True
]
if non_default_primary_key_fields:
cls.model_fields.pop("id", None)
Expand Down
20 changes: 11 additions & 9 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2105,17 +2105,19 @@ class StateManagerMemory(StateManager):
# The dict of mutexes for each client
_states_locks: Dict[str, asyncio.Lock] = pydantic.PrivateAttr({})

class Config:
"""The Pydantic config."""

# TODO: pydantic v2
fields = {
"_states_locks": {"exclude": True},
}

# Pydantic config
model_config = pydantic.ConfigDict(
arbitrary_types_allowed=True,
use_enum_values=True,
extra="allow",
# json_encoders = {
# MutableProxy: lambda v: v.__wrapped__,
# MutableProxy: lambda v: v.__wrapped__, # this is currently done in _get_value
# }
# TODO: pydantic v2
# fields = {
# "_states_locks": {"exclude": True},
# }
)

async def get_state(self, token: str) -> BaseState:
"""Get the state for a token.
Expand Down

0 comments on commit 6f3d582

Please sign in to comment.