-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Support exclude_if at field level #10961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support exclude_if at field level #10961
Conversation
fd789eb
to
537d0dd
Compare
please review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So committing (temporary) this file is necessary when building from an open PR on pydantic-core, right?
CodSpeed Performance ReportMerging #10961 will not alter performanceComparing Summary
|
@@ -11,7 +11,7 @@ git fetch --tags | |||
|
|||
pip install -r requirements.txt | |||
# Install the version of pydantic from the current branch, not the released version used by fastapi | |||
pip uninstall -y pydantic | |||
pip uninstall -y pydantic pydantic-core # TODO: pydantic-core is temporal here | |||
cd .. && pip install . && cd fastapi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to add pydantic-core
, I think this has to be here for installing pydantic-core
from branch to work --- Do I keep it here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is removed on main
iirc, so should matter anymore 👍
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
It would be great if the callback could also receive a serialization context (my use case is excluding fields based on the |
We'll be revisiting this before the v2.11 release 👍 |
Curious what the status of this is. I am assuming it did not make it out in v2.11; do you know when it might come out? I've been looking forward to this for a while! 😃 |
Curious too! @sydney-runkle Let me know if you guys still need this, to update the PR if necessary |
@andresliszt @sydney-runkle We (users) need it very much :) Let's push it forward! |
I'll get back on this shortly! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @andresliszt sorry for the delay. Are you willing to continue working on this? Left some comments, it is mostly in good shape (lets keep it at the field level for now, we can later improve this to be on model_dump()
etc), but some relatively simple conflicts will need to be resolved.
@@ -950,6 +961,7 @@ def Field( # noqa: C901 | |||
description: Human-readable description. | |||
examples: Example values for this field. | |||
exclude: Whether to exclude the field from the model serialization. | |||
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. | |
exclude_if: A callable that determines whether to exclude a field during serialization based on its value. |
@@ -784,6 +791,7 @@ def Field( | |||
description: str | None = _Unset, | |||
examples: list[Any] | None = _Unset, | |||
exclude: bool | None = _Unset, | |||
exclude_if: Callable[[Any], bool] | None = _Unset, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this overload (default
set) and the one for default_factory
set, we can reuse the type variable to get proper type checking:
exclude_if: Callable[[_T], bool] = _Unset
@@ -11,7 +11,7 @@ git fetch --tags | |||
|
|||
pip install -r requirements.txt | |||
# Install the version of pydantic from the current branch, not the released version used by fastapi | |||
pip uninstall -y pydantic | |||
pip uninstall -y pydantic pydantic-core # TODO: pydantic-core is temporal here | |||
cd .. && pip install . && cd fastapi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is removed on main
iirc, so should matter anymore 👍
@@ -116,6 +117,7 @@ class FieldInfo(_repr.Representation): | |||
description: The description of the field. | |||
examples: List of examples of the field. | |||
exclude: Whether to exclude the field from the model serialization. | |||
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exclude_if: Callable that determines whether to exclude a field during serialization based on its value. | |
exclude_if: A callable that determines whether to exclude a field during serialization based on its value. |
@@ -1057,6 +1057,7 @@ def _generate_td_field_schema( | |||
return core_schema.typed_dict_field( | |||
common_field['schema'], | |||
required=False if not field_info.is_required() else required, | |||
exclude_if=field_info.exclude_if, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will need to be updated according to my pydantic-core
PR review
Hello ! I commit to have it This week, best regards |
Hi @andresliszt, I co-authored a new PR with a couple more changes and documentation, which we'll merge soon. Thanks for the contribution! |
Change Summary
Adds support for pydantic side
exclude_if
. This PR points to thepydantic
-core version in the open PR added in the related issue.Related issue number
pydantic/pydantic-core#1535
Checklist
Selected Reviewer: @sydney-runkle