Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
andresliszt wants to merge3 commits intopydantic:main
base:main
Choose a base branch
Loading
fromandresliszt:support/exclude-if-at-field

Conversation

andresliszt
Copy link
Contributor

@andreslisztandresliszt commentedNov 24, 2024
edited by pydantic-hookybot
Loading

Change Summary

Adds support for pydantic sideexclude_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

  • 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

andersk, PaulCharnay-abelio, thibaut-lo, and jkugler reacted with thumbs up emoji
@github-actionsgithub-actionsbot added the relnotes-fixUsed for bugfixes. labelNov 24, 2024
@andreslisztandreslisztforce-pushed thesupport/exclude-if-at-field branch fromfd789eb to537d0ddCompareNovember 24, 2024 17:03
@andresliszt
Copy link
ContributorAuthor

please review

pydantic-hooky[bot], heckad, and PaulCharnay-abelio reacted with thumbs up emoji

Copy link
ContributorAuthor

@andreslisztandreslisztNov 24, 2024
edited
Loading

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-hqCodSpeed HQ
Copy link

codspeed-hqbot commentedNov 24, 2024
edited
Loading

CodSpeed Performance Report

Merging#10961 willnot alter performance

Comparingandresliszt:support/exclude-if-at-field (03c6313) withmain (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
ContributorAuthor

@andreslisztandreslisztNov 24, 2024
edited
Loading

Choose a reason for hiding this comment

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

Had to addpydantic-core, I think this has to be here for installingpydantic-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 onmain iirc, so should matter anymore 👍

@github-actionsGitHub Actions
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 bypython-coverage-comment-action

@sydney-runklesydney-runkle added relnotes-feature and removed relnotes-fixUsed for bugfixes. labelsJan 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 thecontext)

@sydney-runkle
Copy link
Contributor

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

andresliszt and thibaut-lo reacted with hooray emoji

@joshorr
Copy link

joshorr commentedMar 31, 2025
edited
Loading

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

thibaut-lo and daragok reacted with thumbs up emoji

@andresliszt
Copy link
ContributorAuthor

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

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

annesylvie-runna and daragok reacted with thumbs up emoji

@Viicos
Copy link
Member

I'll get back on this shortly!

jkugler, joshorr, and daragok reacted with hooray emojiandresliszt, jkugler, and daragok reacted with rocket emoji

Copy link
Member

@ViicosViicos left a comment
edited
Loading

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 onmodel_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:Callablethatdetermineswhethertoexcludeafieldduringserializationbasedonitsvalue.
exclude_if:Acallablethatdetermineswhethertoexcludeafieldduringserializationbasedonitsvalue.

@@ -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 fordefault_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 onmain 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:Callablethatdetermineswhethertoexcludeafieldduringserializationbasedonitsvalue.
exclude_if:Acallablethatdetermineswhethertoexcludeafieldduringserializationbasedonitsvalue.

@@ -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 mypydantic-core PR review

@pydantic-hookypydantic-hookybot added awaiting author revisionawaiting changes from the PR author and removed ready for review labelsJul 11, 2025
@ViicosViicos mentioned this pull requestJul 11, 2025
@andresliszt
Copy link
ContributorAuthor

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 onmodel_dump() etc), but some relatively simple conflicts will need to be resolved.

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

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ViicosViicosViicos requested changes

Requested changes must be addressed to merge this pull request.

Labels
awaiting author revisionawaiting changes from the PR authorrelnotes-feature
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@andresliszt@emosenkis@sydney-runkle@joshorr@lokhman@Viicos

[8]ページ先頭

©2009-2025 Movatter.jp