kaxil commented on code in PR #63716:
URL: https://github.com/apache/airflow/pull/63716#discussion_r2941429171
##########
airflow-core/src/airflow/api_fastapi/core_api/base.py:
##########
@@ -54,12 +54,12 @@ def make_partial_model(model: type[PydanticBaseModel]) ->
type[PydanticBaseModel
"""Create a version of a Pydantic model where all fields are Optional with
default=None."""
field_overrides: dict = {}
for field_name, field_info in model.model_fields.items():
- new_info = deepcopy(field_info)
ann = field_info.annotation
origin = get_origin(ann)
if not (origin is Union and type(None) in get_args(ann)):
ann = ann | None # type: ignore[operator, assignment]
- new_info.default = None
+ new_info = FieldInfo(default=None)
Review Comment:
Thanks for the context Pierre! I dug into this and found the root cause.
`deepcopy` itself works fine on 2.12.0, but the issue is that after
`deepcopy` + setting `.default = None`, pydantic doesn't know the default was
explicitly set because `_attributes_set` (an internal dict tracking which
attributes were user-provided) doesn't include `"default"`. So `create_model`
still treats those fields as required.
The fix that works on both 2.12.0 and 2.12.5 is to use `FieldInfo._copy()`
(pydantic's own copy method, exists since at least 2.12.0) and then register
the default in `_attributes_set`:
```python
new_info = field_info._copy()
# ... annotation handling ...
new_info.default = None
new_info._attributes_set["default"] = None
```
This preserves all field attributes (aliases, title, description, etc.) and
correctly marks the default as explicitly set. Verified on both 2.12.0 and
2.12.5.
With this fix we could revert the pydantic bump from #63570 back to
`>=2.11.0` (or `>=2.12.0` if needed for other reasons).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]