TypeAdapter
Bases: Generic[T]
Usage Documentation
タイプアダプタは、Pythonタイプに基づいて検証とシリアライゼーションを実行する柔軟な方法を提供します。
TypeAdapter
インスタンスは、そのようなメソッドを持たない型(データクラス、プリミティブ型など)に対して、BaseModel
インスタンスメソッドの機能の一部を公開します。
Note: TypeAdapter
インスタンスは型ではなく、フィールドの型注釈として使用することはできません。
Note: デフォルトでは、TypeAdapter
はmodel_config
またはTypeAdapter
コンストラクタconfig
のdefer_build=True
設定を考慮しません。また、モデルバリデータとシリアライザの構築を延期するには、設定のexperimental_defer_build_mode=('model','type_adapter')
を明示的に設定する必要があります。したがって、この機能は下位互換性を保証するためにオプトインされています。
Attributes:
Name | Type | Description |
---|---|---|
core_schema |
CoreSchema
|
型のコア・スキーマ。 |
validator(SchemaValidator) |
CoreSchema
|
型のスキーマ・バリデーター。 |
serializer |
SchemaSerializer
|
型のスキーマ・シリアライザ。 |
Parameters:
Name | Type | Description | Default |
---|---|---|---|
type |
Any
|
|
required |
config |
ConfigDict | None
|
|
None
|
_parent_depth |
int
|
ローカルネームスペースを構築するために親ネームスペースを検索するデプス。 |
2
|
module |
str | None
|
プラグインに渡されるモジュール(指定されている場合)。 |
None
|
Note
使用している型が上書きできない独自の設定を持っている場合(例:BaseModel
、TypedDict
、dataclass
)、TypeAdapter
をインスタンス化するときにconfig
引数を使用することはできません。この場合、type-adapter-config-unused
エラーが発生します。
Note
_parent_depth
引数の名前にはアンダースコアが付いています。これは、引数がプライベートであることを示し、使用しないようにするためです。
マイナーバージョンでは非推奨になる可能性があるため、動作/サポートの潜在的な変更に満足している場合にのみ使用することをお勧めします。
Compatibility with mypy
使用されているタイプによっては、mypy
がTypeAdapter
をインスタンス化するときにエラーが発生することがあります。回避策として、変数に明示的に注釈を付けることができます。
from typing import Union
from pydantic import TypeAdapter
ta: TypeAdapter[Union[str, int]] = TypeAdapter(Union[str, int]) # type: ignore[arg-type]
Returns:
Type | Description |
---|---|
None
|
指定された |
Source code in pydantic/type_adapter.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
core_schema
cached
property
¶
core_schema: CoreSchema
SchemaValidatorとSchemaSerializerを構築するために使用されるpydantic-coreスキーマ。
validator
cached
property
¶
validator: SchemaValidator | PluggableSchemaValidator
モデルのインスタンスを検証するために使用されるpydantic-core SchemaValidator。
serializer
cached
property
¶
serializer: SchemaSerializer
モデルのインスタンスをダンプするために使用されるpydantic-core SchemaSerializer。
validate_python ¶
validate_python(
object: Any,
/,
*,
strict: bool | None = None,
from_attributes: bool | None = None,
context: dict[str, Any] | None = None,
) -> T
モデルに対してPythonオブジェクトを検証します。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
object |
Any
|
モデルに対して検証するPythonオブジェクト。 |
required |
strict |
bool | None
|
型を厳密にチェックするかどうか。 |
None
|
from_attributes |
bool | None
|
オブジェクト属性からデータを抽出するかどうか。 |
None
|
context |
dict[str, Any] | None
|
バリデータに渡す追加のコンテキスト。 |
None
|
Note
Pydanticのdataclass
でTypeAdapter
を使用する場合、from_attributes
引数の使用はサポートされません。
Returns:
Type | Description |
---|---|
T
|
検証されたオブジェクトです。 |
Source code in pydantic/type_adapter.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
|
validate_json ¶
validate_json(
data: str | bytes,
/,
*,
strict: bool | None = None,
context: dict[str, Any] | None = None,
) -> T
Usage Documentation
JSON文字列またはバイトをモデルに対して検証します。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str | bytes
|
モデルに対して検証するJSONデータ。 |
required |
strict |
bool | None
|
型を厳密にチェックするかどうか。 |
None
|
context |
dict[str, Any] | None
|
検証中に使用する追加のコンテキスト。 |
None
|
Returns:
Type | Description |
---|---|
T
|
検証されたオブジェクトです。 |
Source code in pydantic/type_adapter.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
validate_strings ¶
validate_strings(
obj: Any,
/,
*,
strict: bool | None = None,
context: dict[str, Any] | None = None,
) -> T
検証オブジェクトには、モデルに対する文字列データが含まれています。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
検証する文字列データがオブジェクトに含まれています。 |
required |
strict |
bool | None
|
型を厳密にチェックするかどうか。 |
None
|
context |
dict[str, Any] | None
|
検証中に使用する追加のコンテキスト。 |
None
|
Returns:
Type | Description |
---|---|
T
|
検証されたオブジェクトです。 |
Source code in pydantic/type_adapter.py
374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
get_default_value ¶
get_default_value(
*,
strict: bool | None = None,
context: dict[str, Any] | None = None
) -> Some[T] | None
ラップされたタイプのデフォルト値を取得します。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict |
bool | None
|
型を厳密にチェックするかどうか。 |
None
|
context |
dict[str, Any] | None
|
バリデータに渡す追加のコンテキスト。 |
None
|
Returns:
Type | Description |
---|---|
Some[T] | None
|
存在する場合は |
Source code in pydantic/type_adapter.py
388 389 390 391 392 393 394 395 396 397 398 399 |
|
dump_python ¶
dump_python(
instance: T,
/,
*,
mode: Literal["json", "python"] = "python",
include: IncEx | None = None,
exclude: IncEx | None = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
round_trip: bool = False,
warnings: (
bool | Literal["none", "warn", "error"]
) = True,
serialize_as_any: bool = False,
context: dict[str, Any] | None = None,
) -> Any
適合した型のインスタンスをPythonオブジェクトにダンプします。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance |
T
|
シリアライズするPythonオブジェクト。 |
required |
mode |
Literal['json', 'python']
|
出力フォーマット。 |
'python'
|
include |
IncEx | None
|
出力に含めるフィールド。 |
None
|
exclude |
IncEx | None
|
出力から除外するフィールド。 |
None
|
by_alias |
bool
|
フィールド名にエイリアス名を使用するかどうか。 |
False
|
exclude_unset |
bool
|
設定されていないフィールドを除外するかどうか。 |
False
|
exclude_defaults |
bool
|
デフォルト値を持つフィールドを除外するかどうか。 |
False
|
exclude_none |
bool
|
None値を持つフィールドを除外するかどうか。 |
False
|
round_trip |
bool
|
デシリアライズと互換性のある方法でシリアライズされたデータを出力するかどうか。 |
False
|
warnings |
bool | Literal['none', 'warn', 'error']
|
シリアライゼーションエラーの処理方法。False/"none"はエラーを無視します。True/"warn"はエラーをログに記録します。"error"は |
True
|
serialize_as_any |
bool
|
ダック型のシリアライズ動作でフィールドをシリアライズするかどうか。 |
False
|
context |
dict[str, Any] | None
|
シリアライザに渡す追加のコンテキスト。 |
None
|
Returns:
Type | Description |
---|---|
Any
|
シリアライズされたオブジェクト。 |
Source code in pydantic/type_adapter.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
dump_json ¶
dump_json(
instance: T,
/,
*,
indent: int | None = None,
include: IncEx | None = None,
exclude: IncEx | None = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
round_trip: bool = False,
warnings: (
bool | Literal["none", "warn", "error"]
) = True,
serialize_as_any: bool = False,
context: dict[str, Any] | None = None,
) -> bytes
Usage Documentation
適応型のインスタンスをJSONにシリアライズします。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance |
T
|
シリアライズされるインスタンス。 |
required |
indent |
int | None
|
JSONインデントのスペースの数。 |
None
|
include |
IncEx | None
|
含めるフィールド。 |
None
|
exclude |
IncEx | None
|
除外するフィールド。 |
None
|
by_alias |
bool
|
フィールド名にエイリアス名を使用するかどうか。 |
False
|
exclude_unset |
bool
|
設定されていないフィールドを除外するかどうか。 |
False
|
exclude_defaults |
bool
|
デフォルト値を持つフィールドを除外するかどうか。 |
False
|
exclude_none |
bool
|
|
False
|
round_trip |
bool
|
ラウンドトリップを確実にするために、インスタンスをシリアライズしてデシリアライズするかどうか。 |
False
|
warnings |
bool | Literal['none', 'warn', 'error']
|
シリアライゼーションエラーの処理方法。False/"none"はエラーを無視します。True/"warn"はエラーをログに記録します。"error"は |
True
|
serialize_as_any |
bool
|
ダック型のシリアライズ動作でフィールドをシリアライズするかどうか。 |
False
|
context |
dict[str, Any] | None
|
シリアライザに渡す追加のコンテキスト。 |
None
|
Returns:
Type | Description |
---|---|
bytes
|
指定されたインスタンスのJSON表現(バイト単位)。 |
Source code in pydantic/type_adapter.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
|
json_schema ¶
json_schema(
*,
by_alias: bool = True,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema,
mode: JsonSchemaMode = "validation"
) -> dict[str, Any]
適用された型のJSONスキーマを生成します。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
by_alias |
bool
|
フィールド名にエイリアス名を使用するかどうか。 |
True
|
ref_template |
str
|
$ref文字列の生成に使用される書式文字列。 |
DEFAULT_REF_TEMPLATE
|
schema_generator |
type[GenerateJsonSchema]
|
スキーマの作成に使用するジェネレーター・クラス。 |
GenerateJsonSchema
|
mode |
JsonSchemaMode
|
スキーマの生成に使用するモード。 |
'validation'
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
ディクショナリとしてのモデルのJSONスキーマ。 |
Source code in pydantic/type_adapter.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
json_schemas
staticmethod
¶
json_schemas(
inputs: Iterable[
tuple[
JsonSchemaKeyT, JsonSchemaMode, TypeAdapter[Any]
]
],
/,
*,
by_alias: bool = True,
title: str | None = None,
description: str | None = None,
ref_template: str = DEFAULT_REF_TEMPLATE,
schema_generator: type[
GenerateJsonSchema
] = GenerateJsonSchema,
) -> tuple[
dict[
tuple[JsonSchemaKeyT, JsonSchemaMode],
JsonSchemaValue,
],
JsonSchemaValue,
]
複数のタイプアダプタからの定義を含むJSONスキーマを生成します。
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
Iterable[tuple[JsonSchemaKeyT, JsonSchemaMode, TypeAdapter[Any]]]
|
スキーマ生成への入力です。最初の2つの項目は、(最初の)出力マッピングのキーを形成します。 |
required |
by_alias |
bool
|
エイリアス名を使用するかどうか。 |
True
|
title |
str | None
|
スキーマのタイトル。 |
None
|
description |
str | None
|
スキーマの説明。 |
None
|
ref_template |
str
|
$ref文字列の生成に使用される書式文字列。 |
DEFAULT_REF_TEMPLATE
|
schema_generator |
type[GenerateJsonSchema]
|
スキーマの作成に使用するジェネレーター・クラス。 |
GenerateJsonSchema
|
Returns:
Name | Type | Description |
---|---|---|
次の条件を満たすタプル |
tuple[dict[tuple[JsonSchemaKeyT, JsonSchemaMode], JsonSchemaValue], JsonSchemaValue]
|
|
Source code in pydantic/type_adapter.py
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 |
|