Skip to content

Commit

Permalink
Update Pydantic patch for Lite that emulates PydanticV2 API using V1 (#…
Browse files Browse the repository at this point in the history
…7365)

* Update Pydantic patch for Lite that emulates PydanticV2 API using V1

* add changeset

* Tell the static type checker to ignore the patched Pydantic classes

* Remove `type: ignore`

* lint

* Delete model_config in the patch

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
whitphx and gradio-pr-bot committed Feb 9, 2024
1 parent a6d9f3b commit 1e68561
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-pants-flow.md
@@ -0,0 +1,5 @@
---
"gradio": minor
---

feat:Update Pydantic patch for Lite that emulates PydanticV2 API using V1
21 changes: 17 additions & 4 deletions gradio/data_classes.py
Expand Up @@ -7,16 +7,16 @@
import shutil
from abc import ABC, abstractmethod
from enum import Enum, auto
from typing import Any, List, Optional, Union
from typing import TYPE_CHECKING, Any, List, Optional, Union

from fastapi import Request
from gradio_client.utils import traverse
from typing_extensions import Literal

from . import wasm_utils

if not wasm_utils.IS_WASM:
from pydantic import BaseModel, RootModel, ValidationError # type: ignore
if not wasm_utils.IS_WASM or TYPE_CHECKING:
from pydantic import BaseModel, RootModel, ValidationError
else:
# XXX: Currently Pyodide V2 is not available on Pyodide,
# so we install V1 for the Wasm version.
Expand All @@ -27,7 +27,20 @@

# Map V2 method calls to V1 implementations.
# Ref: https://docs.pydantic.dev/latest/migration/#changes-to-pydanticbasemodel
class BaseModel(BaseModelV1):
class BaseModelMeta(type(BaseModelV1)):
def __new__(cls, name, bases, dct):
# Override `dct` to dynamically create a `Config` class based on `model_config`.
if "model_config" in dct:
config_class = type("Config", (), {})
for key, value in dct["model_config"].items():
setattr(config_class, key, value)
dct["Config"] = config_class
del dct["model_config"]

model_class = super().__new__(cls, name, bases, dct)
return model_class

class BaseModel(BaseModelV1, metaclass=BaseModelMeta):
pass

BaseModel.model_dump = BaseModel.dict # type: ignore
Expand Down

0 comments on commit 1e68561

Please sign in to comment.