migrate pydantic config classes to ConfigDict

This commit is contained in:
Benedikt Bartscher 2024-03-01 18:23:42 +01:00
parent b3593537be
commit 0240541d0b
No known key found for this signature in database
5 changed files with 32 additions and 31 deletions

View File

@ -50,12 +50,12 @@ class Base(pydantic.BaseModel):
frontend and backend should subclass this class. frontend and backend should subclass this class.
""" """
class Config: # Pydantic config
"""Pydantic config.""" model_config = pydantic.ConfigDict(
arbitrary_types_allowed=True,
arbitrary_types_allowed = True use_enum_values=True,
use_enum_values = True extra="allow",
extra = "allow" )
def json(self) -> str: def json(self) -> str:
"""Convert the object to a json string. """Convert the object to a json string.

View File

@ -127,10 +127,10 @@ class DBConfig(Base):
class Config(Base): class Config(Base):
"""A Reflex config.""" """A Reflex config."""
class Config: # Pydantic config
"""Pydantic config for the config.""" model_config = pydantic.ConfigDict(
validate_assignment=True,
validate_assignment = True )
# The name of the app. # The name of the app.
app_name: str app_name: str

View File

@ -16,6 +16,8 @@ from typing import (
Union, Union,
) )
from pydantic import ConfigDict
from reflex import constants from reflex import constants
from reflex.base import Base from reflex.base import Base
from reflex.utils import console, format from reflex.utils import console, format
@ -147,11 +149,10 @@ class EventHandler(EventActionsMixin):
# The function to call in response to the event. # The function to call in response to the event.
fn: Any fn: Any
class Config: # Pydantic config
"""The Pydantic config.""" model_config = ConfigDict(
frozen=True, # Needed to allow serialization of Callable.
# Needed to allow serialization of Callable. )
frozen = True
@property @property
def is_background(self) -> bool: def is_background(self) -> bool:
@ -219,11 +220,10 @@ class EventSpec(EventActionsMixin):
# TODO: pydantic v2 add rx.Var type annotation? # TODO: pydantic v2 add rx.Var type annotation?
args: Tuple[Tuple[Any, Any], ...] = () args: Tuple[Tuple[Any, Any], ...] = ()
class Config: # Pydantic config
"""The Pydantic config.""" model_config = ConfigDict(
frozen=True, # Required to allow tuple fields.
# Required to allow tuple fields. )
frozen = True
def with_args(self, args: Tuple[Tuple[Var, Var], ...]) -> EventSpec: def with_args(self, args: Tuple[Tuple[Var, Var], ...]) -> EventSpec:
"""Copy the event spec, with updated args. """Copy the event spec, with updated args.

View File

@ -62,8 +62,7 @@ class Model(Base, sqlmodel.SQLModel):
non_default_primary_key_fields = [ non_default_primary_key_fields = [
field_name field_name
for field_name, field in cls.model_fields.items() for field_name, field in cls.model_fields.items()
if field_name != "id" if field_name != "id" and getattr(field, "primary_key", None) is True
and getattr(field, "primary_key", None) is True
] ]
if non_default_primary_key_fields: if non_default_primary_key_fields:
cls.model_fields.pop("id", None) cls.model_fields.pop("id", None)

View File

@ -2104,17 +2104,19 @@ class StateManagerMemory(StateManager):
# The dict of mutexes for each client # The dict of mutexes for each client
_states_locks: Dict[str, asyncio.Lock] = pydantic.PrivateAttr({}) _states_locks: Dict[str, asyncio.Lock] = pydantic.PrivateAttr({})
class Config: # Pydantic config
"""The Pydantic config.""" model_config = pydantic.ConfigDict(
arbitrary_types_allowed=True,
# TODO: pydantic v2 use_enum_values=True,
fields = { extra="allow",
"_states_locks": {"exclude": True},
}
# json_encoders = { # 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: async def get_state(self, token: str) -> BaseState:
"""Get the state for a token. """Get the state for a token.