跳到内容

注解处理器

__get_pydantic_core_schema____get_pydantic_json_schema__ 一起使用的类型注解。

GetJsonSchemaHandler

调用下一个 JSON 模式生成函数的处理程序。

属性

名称 类型 描述
mode JsonSchemaMode

Json 模式模式,可以是 validationserialization

resolve_ref_schema

resolve_ref_schema(
    maybe_ref_json_schema: JsonSchemaValue,
) -> JsonSchemaValue

获取 {"$ref": ...} 模式的真实模式。如果给定的模式不是 $ref 模式,它将按原样返回。这意味着您不必在调用此函数之前进行检查。

参数

名称 类型 描述 默认
maybe_ref_json_schema JsonSchemaValue

可能是 $ref 模式的 JsonSchemaValue。

required

引发

类型 描述
LookupError

如果找不到 ref。

返回

名称 类型 描述
JsonSchemaValue JsonSchemaValue

没有 $ref 的 JsonSchemaValue。

源代码在 pydantic/annotated_handlers.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def resolve_ref_schema(self, maybe_ref_json_schema: JsonSchemaValue, /) -> JsonSchemaValue:
    """Get the real schema for a `{"$ref": ...}` schema.
    If the schema given is not a `$ref` schema, it will be returned as is.
    This means you don't have to check before calling this function.

    Args:
        maybe_ref_json_schema: A JsonSchemaValue which may be a `$ref` schema.

    Raises:
        LookupError: If the ref is not found.

    Returns:
        JsonSchemaValue: A JsonSchemaValue that has no `$ref`.
    """
    raise NotImplementedError

GetCoreSchemaHandler

调用下一个 CoreSchema 模式生成函数的处理程序。

field_name property

field_name: str | None

获取与此验证器最接近的字段的名称。

generate_schema

generate_schema(source_type: Any) -> CoreSchema

生成与当前上下文无关的模式。如果您正在处理序列的模式生成并希望为其项目生成模式,请使用此函数。否则,您最终可能会执行类似将 intended 用于序列本身的 min_length 约束应用于其项目之类的操作!

参数

名称 类型 描述 默认
source_type Any

输入类型。

required

返回

名称 类型 描述
CoreSchema CoreSchema

生成的 pydantic-core CoreSchema。

源代码在 pydantic/annotated_handlers.py
84
85
86
87
88
89
90
91
92
93
94
95
96
97
def generate_schema(self, source_type: Any, /) -> core_schema.CoreSchema:
    """Generate a schema unrelated to the current context.
    Use this function if e.g. you are handling schema generation for a sequence
    and want to generate a schema for its items.
    Otherwise, you may end up doing something like applying a `min_length` constraint
    that was intended for the sequence itself to its items!

    Args:
        source_type: The input type.

    Returns:
        CoreSchema: The `pydantic-core` CoreSchema generated.
    """
    raise NotImplementedError

resolve_ref_schema

resolve_ref_schema(
    maybe_ref_schema: CoreSchema,
) -> CoreSchema

获取 definition-ref 模式的真实模式。如果给定的模式不是 definition-ref 模式,它将按原样返回。这意味着您不必在调用此函数之前进行检查。

参数

名称 类型 描述 默认
maybe_ref_schema CoreSchema

一个 CoreSchema,基于 ref 或不基于 ref

required

引发

类型 描述
LookupError

如果找不到 ref

返回

类型 描述
CoreSchema

一个具体的 CoreSchema

源代码在 pydantic/annotated_handlers.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def resolve_ref_schema(self, maybe_ref_schema: core_schema.CoreSchema, /) -> core_schema.CoreSchema:
    """Get the real schema for a `definition-ref` schema.
    If the schema given is not a `definition-ref` schema, it will be returned as is.
    This means you don't have to check before calling this function.

    Args:
        maybe_ref_schema: A `CoreSchema`, `ref`-based or not.

    Raises:
        LookupError: If the `ref` is not found.

    Returns:
        A concrete `CoreSchema`.
    """
    raise NotImplementedError