mirror of
https://github.com/nonebot/nonebot2.git
synced 2026-01-16 00:32:15 +00:00
🐛 Fix: log level 配置项无法使用 int 类型配置 (#3732)
This commit is contained in:
@@ -11,7 +11,7 @@ FrontMatter:
|
||||
|
||||
from collections.abc import Generator
|
||||
from dataclasses import dataclass, is_dataclass
|
||||
from functools import cached_property
|
||||
from functools import cached_property, wraps
|
||||
from typing import (
|
||||
TYPE_CHECKING,
|
||||
Annotated,
|
||||
@@ -25,13 +25,14 @@ from typing import (
|
||||
Union,
|
||||
overload,
|
||||
)
|
||||
from typing_extensions import Self, get_args, get_origin, is_typeddict
|
||||
from typing_extensions import ParamSpec, Self, get_args, get_origin, is_typeddict
|
||||
|
||||
from pydantic import VERSION, BaseModel
|
||||
|
||||
from nonebot.typing import origin_is_annotated
|
||||
|
||||
T = TypeVar("T")
|
||||
P = ParamSpec("P")
|
||||
|
||||
PYDANTIC_V2 = int(VERSION.split(".", 1)[0]) == 2
|
||||
|
||||
@@ -49,6 +50,7 @@ __all__ = (
|
||||
"PYDANTIC_V2",
|
||||
"ConfigDict",
|
||||
"FieldInfo",
|
||||
"LegacyUnionField",
|
||||
"ModelField",
|
||||
"PydanticUndefined",
|
||||
"PydanticUndefinedType",
|
||||
@@ -71,7 +73,7 @@ __autodoc__ = {
|
||||
|
||||
|
||||
if PYDANTIC_V2: # pragma: pydantic-v2
|
||||
from pydantic import GetCoreSchemaHandler
|
||||
from pydantic import Field, GetCoreSchemaHandler
|
||||
from pydantic import TypeAdapter as TypeAdapter
|
||||
from pydantic import field_validator as field_validator
|
||||
from pydantic import model_validator as model_validator
|
||||
@@ -94,6 +96,17 @@ if PYDANTIC_V2: # pragma: pydantic-v2
|
||||
DEFAULT_CONFIG = ConfigDict(extra="allow", arbitrary_types_allowed=True)
|
||||
"""Default config for validations"""
|
||||
|
||||
def _get_legacy_union_field(func: Callable[P, T]) -> Callable[P, T]:
|
||||
@wraps(func)
|
||||
def wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
|
||||
kwargs["union_mode"] = "left_to_right"
|
||||
return func(*args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
LegacyUnionField = _get_legacy_union_field(Field)
|
||||
LegacyUnionField.__doc__ = "Mark field to use legacy left to right union mode"
|
||||
|
||||
class FieldInfo(BaseFieldInfo): # pyright: ignore[reportGeneralTypeIssues]
|
||||
"""FieldInfo class with extra property for compatibility with pydantic v1"""
|
||||
|
||||
@@ -292,6 +305,8 @@ else: # pragma: pydantic-v1
|
||||
extra = Extra.allow
|
||||
arbitrary_types_allowed = True
|
||||
|
||||
from pydantic.fields import Field as LegacyUnionField
|
||||
|
||||
class FieldInfo(BaseFieldInfo):
|
||||
def __init__(self, default: Any = PydanticUndefined, **kwargs: Any):
|
||||
# preprocess default value to make it compatible with pydantic v2
|
||||
|
||||
Reference in New Issue
Block a user