Update Pydantic patch for Lite that emulates PydanticV2 API using V1 (#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>
This commit is contained in:
Yuichiro Tachibana (Tsuchiya) 2024-02-09 23:43:46 +09:00 committed by GitHub
parent a6d9f3bb3c
commit 1e68561167
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": minor
---
feat:Update Pydantic patch for Lite that emulates PydanticV2 API using V1

View File

@ -7,7 +7,7 @@ import secrets
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
@ -15,8 +15,8 @@ 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.
@ -27,7 +27,20 @@ else:
# 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