Skip to content

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

Closed

Conversation

andresliszt
Copy link
Contributor

@andresliszt andresliszt commented Nov 24, 2024

Change Summary

Adds support for pydantic side exclude_if. This PR points to the pydantic-core version in the open PR added in the related issue.

Related issue number

pydantic/pydantic-core#1535

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @sydney-runkle

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Nov 24, 2024
@andresliszt andresliszt force-pushed the support/exclude-if-at-field branch from fd789eb to 537d0dd Compare November 24, 2024 17:03
@andresliszt
Copy link
Contributor Author

please review

Copy link
Contributor Author

@andresliszt andresliszt Nov 24, 2024

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?

Copy link

codspeed-hq bot commented Nov 24, 2024

CodSpeed Performance Report

Merging #10961 will not alter performance

Comparing andresliszt:support/exclude-if-at-field (03c6313) with main (15f693a)

Summary

✅ 45 untouched benchmarks

@@ -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
Copy link
Contributor Author

@andresliszt andresliszt Nov 24, 2024

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?

Copy link
Member

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 👍

Copy link
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
Project Total  

This report was generated by python-coverage-comment-action

@sydney-runkle sydney-runkle added relnotes-feature and removed relnotes-fix Used for bugfixes. labels Jan 8, 2025
@emosenkis
Copy link

It would be great if the callback could also receive a serialization context (my use case is excluding fields based on the context)

@sydney-runkle
Copy link
Contributor

We'll be revisiting this before the v2.11 release 👍

@joshorr
Copy link

joshorr commented Mar 31, 2025

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! 😃

@andresliszt
Copy link
Contributor Author

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

@lokhman
Copy link

lokhman commented Apr 30, 2025

@andresliszt @sydney-runkle We (users) need it very much :) Let's push it forward!

@Viicos
Copy link
Member

Viicos commented May 19, 2025

I'll get back on this shortly!

Copy link
Member

@Viicos Viicos left a 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
Copy link
Member

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
Copy link
Member

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,
Copy link
Member

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

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Jul 11, 2025
@Viicos Viicos mentioned this pull request Jul 11, 2025
@andresliszt
Copy link
Contributor Author

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.

Hello ! I commit to have it This week, best regards

@Viicos
Copy link
Member

Viicos commented Aug 7, 2025

Hi @andresliszt, I co-authored a new PR with a couple more changes and documentation, which we'll merge soon. Thanks for the contribution!

@Viicos Viicos closed this Aug 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author revision awaiting changes from the PR author relnotes-feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants