Feature: 优化依赖注入在 pydantic v2 下的性能 (#2870)

This commit is contained in:
Ju4tCode
2024-08-11 15:15:59 +08:00
committed by GitHub
parent aeb75a6ce3
commit b59b1be6ff
3 changed files with 87 additions and 35 deletions

View File

@ -1,13 +1,14 @@
from typing import Any, Optional
from dataclasses import dataclass
from typing import Any, Optional, Annotated
import pytest
from pydantic import BaseModel
from pydantic import BaseModel, ValidationError
from nonebot.compat import (
DEFAULT_CONFIG,
Required,
FieldInfo,
TypeAdapter,
PydanticUndefined,
model_dump,
custom_validation,
@ -31,6 +32,21 @@ async def test_field_info():
assert FieldInfo(test="test").extra["test"] == "test"
@pytest.mark.asyncio
async def test_type_adapter():
t = TypeAdapter(Annotated[int, FieldInfo(ge=1)])
assert t.validate_python(2) == 2
with pytest.raises(ValidationError):
t.validate_python(0)
assert t.validate_json("2") == 2
with pytest.raises(ValidationError):
t.validate_json("0")
@pytest.mark.asyncio
async def test_model_dump():
class TestModel(BaseModel):