mirror of
https://github.com/nonebot/nonebot2.git
synced 2026-02-26 20:54:44 +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
|
||||
|
||||
@@ -30,6 +30,7 @@ from pydantic.networks import IPvAnyAddress
|
||||
from nonebot.compat import (
|
||||
PYDANTIC_V2,
|
||||
ConfigDict,
|
||||
LegacyUnionField,
|
||||
ModelField,
|
||||
PydanticUndefined,
|
||||
PydanticUndefinedType,
|
||||
@@ -424,7 +425,7 @@ class Config(BaseSettings):
|
||||
"""NoneBot {ref}`nonebot.drivers.ReverseDriver` 服务端监听的 IP/主机名。"""
|
||||
port: int = Field(default=8080, ge=1, le=65535)
|
||||
"""NoneBot {ref}`nonebot.drivers.ReverseDriver` 服务端监听的端口。"""
|
||||
log_level: Union[int, str] = "INFO"
|
||||
log_level: Union[int, str] = LegacyUnionField(default="INFO")
|
||||
"""NoneBot 日志输出等级,可以为 `int` 类型等级或等级名称。
|
||||
|
||||
参考 [记录日志](https://nonebot.dev/docs/appendices/log),[loguru 日志等级](https://loguru.readthedocs.io/en/stable/api/logger.html#levels)。
|
||||
|
||||
Reference in New Issue
Block a user