nonebot2/website/versioned_docs/version-2.3.1/api/compat.md
noneflow[bot] 622e8e8af3 🔖 Release 2.3.1
2024-05-20 14:19:50 +00:00

3.7 KiB

sidebar_position description
16 nonebot.compat 模块

nonebot.compat

本模块为 Pydantic 版本兼容层模块

为兼容 Pydantic V1 与 V2 版本,定义了一系列兼容函数与类供使用。

var Required

  • 类型: untyped

  • 说明: Alias of Ellipsis for compatibility with pydantic v1

library-attr PydanticUndefined

  • 说明: Pydantic Undefined object

library-attr PydanticUndefinedType

  • 说明: Pydantic Undefined type

var DEFAULT_CONFIG

  • 类型: untyped

  • 说明: Default config for validations

class FieldInfo(default=PydanticUndefined, **kwargs)

  • 说明: FieldInfo class with extra property for compatibility with pydantic v1

  • 参数

    • default (Any)

    • **kwargs (Any)

property extra

  • 类型: dict[str, Any]

  • 说明

    Extra data that is not part of the standard pydantic fields.

    For compatibility with pydantic v1.

class ModelField(<auto>)

  • 说明: ModelField class for compatibility with pydantic v1

  • 参数

    auto

instance-var name

  • 类型: str

  • 说明: The name of the field.

instance-var annotation

  • 类型: Any

  • 说明: The annotation of the field.

instance-var field_info

  • 类型: FieldInfo

  • 说明: The FieldInfo of the field.

classmethod construct(name, annotation, field_info=None)

  • 说明: Construct a ModelField from given infos.

  • 参数

    • name (str)

    • annotation (Any)

    • field_info (FieldInfo | None)

  • 返回

    • Self

method get_default()

  • 说明: Get the default value of the field.

  • 参数

    empty

  • 返回

    • Any

def extract_field_info(field_info)

  • 说明: Get FieldInfo init kwargs from a FieldInfo instance.

  • 参数

    • field_info (BaseFieldInfo)
  • 返回

    • dict[str, Any]

def model_field_validate(model_field, value, config=None)

  • 说明: Validate the value pass to the field.

  • 参数

    • model_field (ModelField)

    • value (Any)

    • config (ConfigDict | None)

  • 返回

    • Any

def model_fields(model)

  • 说明: Get field list of a model.

  • 参数

    • model (type[BaseModel])
  • 返回

    • list[ModelField]

def model_config(model)

  • 说明: Get config of a model.

  • 参数

    • model (type[BaseModel])
  • 返回

    • Any

def model_dump(model, include=None, exclude=None, by_alias=False, exclude_unset=False, exclude_defaults=False, exclude_none=False)

  • 参数

    • model (BaseModel)

    • include (set[str] | None)

    • exclude (set[str] | None)

    • by_alias (bool)

    • exclude_unset (bool)

    • exclude_defaults (bool)

    • exclude_none (bool)

  • 返回

    • dict[str, Any]

def type_validate_python(type_, data)

  • 说明: Validate data with given type.

  • 参数

    • type_ (type[T])

    • data (Any)

  • 返回

    • T

def type_validate_json(type_, data)

  • 说明: Validate JSON with given type.

  • 参数

    • type_ (type[T])

    • data (str | bytes)

  • 返回

    • T

def custom_validation(class_)

  • 说明: Use pydantic v1 like validator generator in pydantic v2

  • 参数

    • class_ (type[CVC])
  • 返回

    • type[CVC]