mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-04 02:26:11 +08:00
* ⬆️ 适配 Pydantic V2 * 🐛 修复测试报错 * 🐛 适配忘记的 from_orm * 🐛 忘记的 class-based `config` * 🐛 更新 red 适配器版本
29 lines
733 B
Python
29 lines
733 B
Python
from typing import Literal, overload
|
|
|
|
from pydantic import BaseModel
|
|
from nonebot.compat import PYDANTIC_V2
|
|
|
|
__all__ = ("model_validator", "model_rebuild")
|
|
|
|
|
|
if PYDANTIC_V2:
|
|
from pydantic import model_validator as model_validator
|
|
|
|
def model_rebuild(model: type[BaseModel]):
|
|
return model.model_rebuild()
|
|
|
|
else:
|
|
from pydantic import root_validator
|
|
|
|
@overload
|
|
def model_validator(*, mode: Literal["before"]): ...
|
|
|
|
@overload
|
|
def model_validator(*, mode: Literal["after"]): ...
|
|
|
|
def model_validator(*, mode: Literal["before", "after"]):
|
|
return root_validator(pre=mode == "before", allow_reuse=True)
|
|
|
|
def model_rebuild(model: type[BaseModel]):
|
|
return model.update_forward_refs()
|