Skip to content

Support exclude_if in computed fields#12748

Merged
Viicos merged 7 commits intopydantic:mainfrom
andresliszt:support-exclude-if-in-computed-fields
Mar 17, 2026
Merged

Support exclude_if in computed fields#12748
Viicos merged 7 commits intopydantic:mainfrom
andresliszt:support-exclude-if-in-computed-fields

Conversation

@andresliszt
Copy link
Copy Markdown
Contributor

@andresliszt andresliszt commented Jan 28, 2026

Change Summary

This PR aims to support serialization_exclude_if in computed fields. This PR adds the ability to exclude the computed field both by passing a direct argument to the decorator, exclude_if (following the same naming convention we use for regular fields), and also if it receives an Annotated as in the example described in the linked issue.

from pydantic import BaseModel, Field
from typing import Annotated

IntExcludeZero = Annotated[int, Field(exclude_if=lambda v: v == 0)]

# From annotated
class Model(BaseModel):
    @computed_field
    def a_computed_field(self) -> IntExcludeZero:
        return 0

obj = Model()
print(obj.model_dump())  # {}


# From decorator arg
class Model(BaseModel):
    @computed_field(exclude_if=exclude_if=lambda v: v == 0)
    def a_computed_field(self) -> int:
        return 0
obj = Model()
print(obj.model_dump())  # {}

# This is invalid, repeated exclude_if, raises PydanticUserError
class Model(BaseModel):
    @computed_field(exclude_if=exclude_if=lambda v: v == 0)
    def a_computed_field(self) -> IntExcludeZero:
        return 0

Related issue number

Fixes #12690

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: @Viicos

@github-actions github-actions Bot added the relnotes-fix Used for bugfixes. label Jan 28, 2026
Comment thread pydantic/_internal/_generate_schema.py Outdated
raise PydanticUserError(
'exclude_if set in the computed_field decorator and in the annotated type',
code='invalid-annotated-type',
)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

is this status code correct?

Comment thread pydantic/_internal/_generate_schema.py Outdated
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 28, 2026

Merging this PR will not alter performance

✅ 212 untouched benchmarks


Comparing andresliszt:support-exclude-if-in-computed-fields (0567845) with main (772448e)

Open in CodSpeed

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 28, 2026

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  fields.py
  pydantic/_internal
  _generate_schema.py
  pydantic-core/python/pydantic_core
  core_schema.py
Project Total  

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

@andresliszt
Copy link
Copy Markdown
Contributor Author

please review

Copy link
Copy Markdown
Collaborator

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks, implementation seems mostly great to me, couple of small thoughts...

Comment thread pydantic/_internal/_generate_schema.py Outdated
Comment thread pydantic/_internal/_generate_schema.py Outdated
Comment thread pydantic/_internal/_generate_schema.py Outdated
Comment thread pydantic-core/tests/serializers/test_model.py
Comment thread tests/test_computed_fields.py
@Viicos Viicos added awaiting author revision awaiting changes from the PR author and removed ready for review labels Feb 7, 2026
@andresliszt andresliszt force-pushed the support-exclude-if-in-computed-fields branch from 9d9012e to 88c732a Compare February 8, 2026 15:38
@andresliszt andresliszt force-pushed the support-exclude-if-in-computed-fields branch from 88c732a to 1cf1573 Compare March 7, 2026 14:25
@Viicos Viicos changed the title Support exclude if in computed fields Support exclude_if in computed fields Mar 17, 2026
@Viicos Viicos added relnotes-feature and removed relnotes-fix Used for bugfixes. labels Mar 17, 2026
Copy link
Copy Markdown
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.

Thanks @andresliszt, that's a nice one!

@Viicos Viicos added needs-blogpost-entry This PR needs to be documented in the release notes blog post and removed awaiting author revision awaiting changes from the PR author labels Mar 17, 2026
@Viicos Viicos merged commit 2d5779c into pydantic:main Mar 17, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-blogpost-entry This PR needs to be documented in the release notes blog post relnotes-feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support exclude_if for computed fields

3 participants