Construct the final `FieldInfo` instance, by merging the possibly existing `FieldInfo` instances from the metadata. With the following example: ```python {test="skip" lint="skip"} class Model(BaseModel): f: Annotated[int, Gt(1), Field(description='desc', lt=2)]
(cls, metadata: list[Any], **attr_overrides: Any)
| 478 | |
| 479 | @classmethod |
| 480 | def _construct(cls, metadata: list[Any], **attr_overrides: Any) -> Self: |
| 481 | """Construct the final `FieldInfo` instance, by merging the possibly existing `FieldInfo` instances from the metadata. |
| 482 | |
| 483 | With the following example: |
| 484 | |
| 485 | ```python {test="skip" lint="skip"} |
| 486 | class Model(BaseModel): |
| 487 | f: Annotated[int, Gt(1), Field(description='desc', lt=2)] |
| 488 | ``` |
| 489 | |
| 490 | `metadata` refers to the metadata elements of the `Annotated` form. This metadata is iterated over from left to right: |
| 491 | |
| 492 | - If the element is a `Field()` function (which is itself a `FieldInfo` instance), the field attributes (such as |
| 493 | `description`) are saved to be set on the final `FieldInfo` instance. |
| 494 | On the other hand, some kwargs (such as `lt`) are stored as `metadata` (see `FieldInfo.__init__()`, calling |
| 495 | `FieldInfo._collect_metadata()`). In this case, the final metadata list is extended with the one from this instance. |
| 496 | - Else, the element is considered as a single metadata object, and is appended to the final metadata list. |
| 497 | |
| 498 | Args: |
| 499 | metadata: The list of metadata elements to merge together. If the `FieldInfo` instance to be constructed is for |
| 500 | a field with an assigned `Field()`, this `Field()` assignment should be added as the last element of the |
| 501 | provided metadata. |
| 502 | **attr_overrides: Extra attributes that should be set on the final merged `FieldInfo` instance. |
| 503 | |
| 504 | Returns: |
| 505 | The final merged `FieldInfo` instance. |
| 506 | """ |
| 507 | merged_metadata: list[Any] = [] |
| 508 | merged_kwargs: dict[str, Any] = {} |
| 509 | |
| 510 | for meta in metadata: |
| 511 | if isinstance(meta, FieldInfo): |
| 512 | merged_metadata.extend(meta.metadata) |
| 513 | |
| 514 | new_js_extra: JsonDict | None = None |
| 515 | current_js_extra = meta.json_schema_extra |
| 516 | if current_js_extra is not None and 'json_schema_extra' in merged_kwargs: |
| 517 | # We need to merge `json_schema_extra`'s: |
| 518 | existing_js_extra = merged_kwargs['json_schema_extra'] |
| 519 | if isinstance(existing_js_extra, dict): |
| 520 | if isinstance(current_js_extra, dict): |
| 521 | new_js_extra = { |
| 522 | **existing_js_extra, |
| 523 | **current_js_extra, |
| 524 | } |
| 525 | elif callable(current_js_extra): |
| 526 | warn( |
| 527 | 'Composing `dict` and `callable` type `json_schema_extra` is not supported. ' |
| 528 | 'The `callable` type is being ignored. ' |
| 529 | "If you'd like support for this behavior, please open an issue on pydantic.", |
| 530 | UserWarning, |
| 531 | ) |
| 532 | elif callable(existing_js_extra) and isinstance(current_js_extra, dict): |
| 533 | warn( |
| 534 | 'Composing `dict` and `callable` type `json_schema_extra` is not supported. ' |
| 535 | 'The `callable` type is being ignored. ' |
| 536 | "If you'd like support for this behavior, please open an issue on pydantic.", |
| 537 | UserWarning, |
no test coverage detected