From 0240541d0b706dc1df1135b3dbc936b83311c423 Mon Sep 17 00:00:00 2001 From: Benedikt Bartscher Date: Fri, 1 Mar 2024 18:23:42 +0100 Subject: [PATCH] migrate pydantic config classes to ConfigDict --- reflex/base.py | 12 ++++++------ reflex/config.py | 8 ++++---- reflex/event.py | 20 ++++++++++---------- reflex/model.py | 3 +-- reflex/state.py | 20 +++++++++++--------- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/reflex/base.py b/reflex/base.py index 1b282e83f..ecc7b1683 100644 --- a/reflex/base.py +++ b/reflex/base.py @@ -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. diff --git a/reflex/config.py b/reflex/config.py index 3965afe7a..d30a58cee 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -127,10 +127,10 @@ class DBConfig(Base): 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 diff --git a/reflex/event.py b/reflex/event.py index 26ef59d0a..75ae96a86 100644 --- a/reflex/event.py +++ b/reflex/event.py @@ -16,6 +16,8 @@ from typing import ( Union, ) +from pydantic import ConfigDict + from reflex import constants from reflex.base import Base from reflex.utils import console, format @@ -147,11 +149,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: @@ -219,11 +220,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. diff --git a/reflex/model.py b/reflex/model.py index 5448c665a..c556a4482 100644 --- a/reflex/model.py +++ b/reflex/model.py @@ -62,8 +62,7 @@ class Model(Base, sqlmodel.SQLModel): 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) diff --git a/reflex/state.py b/reflex/state.py index 26669b98b..6ddef6a3d 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -2104,17 +2104,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.