Sfoglia il codice sorgente

migrate pydantic config classes to ConfigDict

Benedikt Bartscher 1 anno fa
parent
commit
0240541d0b
5 ha cambiato i file con 32 aggiunte e 31 eliminazioni
  1. 6 6
      reflex/base.py
  2. 4 4
      reflex/config.py
  3. 10 10
      reflex/event.py
  4. 1 2
      reflex/model.py
  5. 11 9
      reflex/state.py

+ 6 - 6
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.

+ 4 - 4
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

+ 10 - 10
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.

+ 1 - 2
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)

+ 11 - 9
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.