Merge `FieldInfo` instances keeping only explicitly set attributes. Later `FieldInfo` instances override earlier ones. Returns: FieldInfo: A merged FieldInfo instance.
(*field_infos: FieldInfo, **overrides: Any)
| 575 | category=None, |
| 576 | ) |
| 577 | def merge_field_infos(*field_infos: FieldInfo, **overrides: Any) -> FieldInfo: |
| 578 | """Merge `FieldInfo` instances keeping only explicitly set attributes. |
| 579 | |
| 580 | Later `FieldInfo` instances override earlier ones. |
| 581 | |
| 582 | Returns: |
| 583 | FieldInfo: A merged FieldInfo instance. |
| 584 | """ |
| 585 | if len(field_infos) == 1: |
| 586 | # No merging necessary, but we still need to make a copy and apply the overrides |
| 587 | field_info = field_infos[0]._copy() |
| 588 | field_info._attributes_set.update(overrides) |
| 589 | |
| 590 | default_override = overrides.pop('default', PydanticUndefined) |
| 591 | if default_override is Ellipsis: |
| 592 | default_override = PydanticUndefined |
| 593 | if default_override is not PydanticUndefined: |
| 594 | field_info.default = default_override |
| 595 | |
| 596 | for k, v in overrides.items(): |
| 597 | setattr(field_info, k, v) |
| 598 | return field_info # type: ignore |
| 599 | |
| 600 | merged_field_info_kwargs: dict[str, Any] = {} |
| 601 | metadata = {} |
| 602 | for field_info in field_infos: |
| 603 | attributes_set = field_info._attributes_set.copy() |
| 604 | |
| 605 | try: |
| 606 | json_schema_extra = attributes_set.pop('json_schema_extra') |
| 607 | existing_json_schema_extra = merged_field_info_kwargs.get('json_schema_extra') |
| 608 | |
| 609 | if existing_json_schema_extra is None: |
| 610 | merged_field_info_kwargs['json_schema_extra'] = json_schema_extra |
| 611 | if isinstance(existing_json_schema_extra, dict): |
| 612 | if isinstance(json_schema_extra, dict): |
| 613 | merged_field_info_kwargs['json_schema_extra'] = { |
| 614 | **existing_json_schema_extra, |
| 615 | **json_schema_extra, |
| 616 | } |
| 617 | if callable(json_schema_extra): |
| 618 | warn( |
| 619 | 'Composing `dict` and `callable` type `json_schema_extra` is not supported.' |
| 620 | 'The `callable` type is being ignored.' |
| 621 | "If you'd like support for this behavior, please open an issue on pydantic.", |
| 622 | PydanticJsonSchemaWarning, |
| 623 | ) |
| 624 | elif callable(json_schema_extra): |
| 625 | # if ever there's a case of a callable, we'll just keep the last json schema extra spec |
| 626 | merged_field_info_kwargs['json_schema_extra'] = json_schema_extra |
| 627 | except KeyError: |
| 628 | pass |
| 629 | |
| 630 | # later FieldInfo instances override everything except json_schema_extra from earlier FieldInfo instances |
| 631 | merged_field_info_kwargs.update(attributes_set) |
| 632 | |
| 633 | for x in field_info.metadata: |
| 634 | if not isinstance(x, FieldInfo): |