实验性 API¶
管道 API¶
实验性管道 API 功能。请谨慎使用此 API,它可能会发生变化。
_Pipeline dataclass
¶
_Pipeline(_steps: tuple[_Step, ...])
基类:Generic[_InT, _OutT]
验证、转换和解析步骤链的抽象表示。
transform ¶
转换上一步的输出。
如果用作管道中的第一步,则使用字段的类型。也就是说,转换在值解析为字段类型后应用。
源代码在 pydantic/experimental/pipeline.py
136 137 138 139 140 141 142 143 144 145 |
|
validate_as ¶
validate_as(
tp: EllipsisType, *, strict: bool = ...
) -> _Pipeline[_InT, Any]
validate_as(
tp: type[_NewOutT] | EllipsisType,
*,
strict: bool = False
) -> _Pipeline[_InT, Any]
将输入验证/解析为新类型。
如果未提供类型,则使用字段的类型。
默认情况下,类型在 Pydantic 的 lax
模式下解析,但您可以通过传入 strict=True
来启用 strict
模式。
源代码在 pydantic/experimental/pipeline.py
154 155 156 157 158 159 160 161 162 163 164 |
|
validate_as_deferred ¶
将输入解析为新类型,将类型的解析推迟到当前类完全定义之后。
当您需要在其自己的类型注释中引用该类时,这很有用。
源代码在 pydantic/experimental/pipeline.py
166 167 168 169 170 171 172 |
|
constrain ¶
constrain(constraint: Ge) -> _Pipeline[_InT, _NewOutGe]
constrain(constraint: Gt) -> _Pipeline[_InT, _NewOutGt]
constrain(constraint: Le) -> _Pipeline[_InT, _NewOutLe]
constrain(constraint: Lt) -> _Pipeline[_InT, _NewOutLt]
constrain(constraint: Len) -> _Pipeline[_InT, _NewOutLen]
constrain(
constraint: MultipleOf,
) -> _Pipeline[_InT, _NewOutT]
constrain(
constraint: Timezone,
) -> _Pipeline[_InT, _NewOutDatetime]
constrain(constraint: Predicate) -> _Pipeline[_InT, _OutT]
constrain(
constraint: Interval,
) -> _Pipeline[_InT, _NewOutInterval]
constrain(constraint: _Eq) -> _Pipeline[_InT, _OutT]
constrain(constraint: _NotEq) -> _Pipeline[_InT, _OutT]
constrain(constraint: _In) -> _Pipeline[_InT, _OutT]
constrain(constraint: _NotIn) -> _Pipeline[_InT, _OutT]
constrain(constraint: _ConstraintAnnotation) -> Any
将值约束为满足特定条件。
我们支持 annotated_types
中的大多数条件,以及正则表达式。
大多数情况下,您会调用 gt
、lt
、len
等快捷方法,因此您不需要直接调用此方法。
源代码在 pydantic/experimental/pipeline.py
225 226 227 228 229 230 231 232 233 |
|
predicate ¶
将值约束为满足特定谓词。
源代码在 pydantic/experimental/pipeline.py
235 236 237 |
|
gt ¶
gt(gt: _NewOutGt) -> _Pipeline[_InT, _NewOutGt]
将值约束为大于特定值。
源代码在 pydantic/experimental/pipeline.py
239 240 241 |
|
lt ¶
lt(lt: _NewOutLt) -> _Pipeline[_InT, _NewOutLt]
将值约束为小于特定值。
源代码在 pydantic/experimental/pipeline.py
243 244 245 |
|
ge ¶
ge(ge: _NewOutGe) -> _Pipeline[_InT, _NewOutGe]
将值约束为大于或等于特定值。
源代码在 pydantic/experimental/pipeline.py
247 248 249 |
|
le ¶
le(le: _NewOutLe) -> _Pipeline[_InT, _NewOutLe]
将值约束为小于或等于特定值。
源代码在 pydantic/experimental/pipeline.py
251 252 253 |
|
len ¶
将值约束为具有特定长度。
源代码在 pydantic/experimental/pipeline.py
255 256 257 |
|
multiple_of ¶
multiple_of(
multiple_of: _NewOutDiv,
) -> _Pipeline[_InT, _NewOutDiv]
multiple_of(
multiple_of: _NewOutMod,
) -> _Pipeline[_InT, _NewOutMod]
将值约束为特定数字的倍数。
源代码在 pydantic/experimental/pipeline.py
265 266 267 |
|
eq ¶
eq(value: _OutT) -> _Pipeline[_InT, _OutT]
将值约束为等于特定值。
源代码在 pydantic/experimental/pipeline.py
269 270 271 |
|
not_eq ¶
not_eq(value: _OutT) -> _Pipeline[_InT, _OutT]
将值约束为不等于特定值。
源代码在 pydantic/experimental/pipeline.py
273 274 275 |
|
in_ ¶
将值约束为在特定集合中。
源代码在 pydantic/experimental/pipeline.py
277 278 279 |
|
not_in ¶
将值约束为不在特定集合中。
源代码在 pydantic/experimental/pipeline.py
281 282 283 |
|
otherwise ¶
组合两个验证链,如果第一个链成功则返回第一个链的结果,如果失败则返回第二个链的结果。
源代码在 pydantic/experimental/pipeline.py
328 329 330 |
|
then ¶
将一个验证链的结果导入到另一个验证链中。
源代码在 pydantic/experimental/pipeline.py
334 335 336 |
|
参数模式 API¶
实验性模块,暴露了一个函数,用于生成验证可调用参数的核心模式。
generate_arguments_schema ¶
generate_arguments_schema(
func: Callable[..., Any],
schema_type: Literal[
"arguments", "arguments-v3"
] = "arguments-v3",
parameters_callback: (
Callable[[int, str, Any], Literal["skip"] | None]
| None
) = None,
config: ConfigDict | None = None,
) -> CoreSchema
生成函数参数的模式。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
func
|
Callable[..., Any]
|
要为其生成模式的函数。 |
必需 |
schema_type
|
Literal['arguments', 'arguments-v3']
|
要生成的模式类型。 |
'arguments-v3'
|
parameters_callback
|
Callable[[int, str, Any], Literal['skip'] | None] | None
|
一个可调用对象,将为每个参数调用。回调应接受三个必需参数:参数的索引、名称和类型注释(如果未注释,则为 |
None
|
config
|
ConfigDict | None
|
要使用的配置。 |
None
|
返回
类型 | 描述 |
---|---|
CoreSchema
|
生成的模式。 |
源代码在 pydantic/experimental/arguments_schema.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|