Skip to content

Track extra fields set after init in model_fields_set#12817

Merged
Viicos merged 2 commits intopydantic:mainfrom
navalprakhar:fix/11134-model-fields-set
Mar 5, 2026
Merged

Track extra fields set after init in model_fields_set#12817
Viicos merged 2 commits intopydantic:mainfrom
navalprakhar:fix/11134-model-fields-set

Conversation

@navalprakhar
Copy link
Copy Markdown
Contributor

@navalprakhar navalprakhar commented Feb 19, 2026

Change Summary

Fix _setattr_handler to update __pydantic_fields_set__ when setting extra fields after init.

Related issue number

fix #11134

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 Feb 19, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Feb 19, 2026

Merging this PR will improve performance by 9.58%

⚡ 2 improved benchmarks
✅ 210 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_list_of_ints_core_json 775.9 µs 727.1 µs +6.72%
test_many_models_core_model 2.7 ms 2.5 ms +9.58%

Comparing navalprakhar:fix/11134-model-fields-set (6ac5ac6) with main (fc68422)

Open in CodSpeed

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 19, 2026

Coverage report

Click to see where and how coverage changed

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

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

@navalprakhar
Copy link
Copy Markdown
Contributor Author

please review

Comment thread pydantic/main.py Outdated
@Viicos Viicos added relnotes-change Used for changes to existing functionality which don't have a better categorization. and removed relnotes-fix Used for bugfixes. labels Mar 5, 2026
@Viicos Viicos changed the title Fix model_fields_set not tracking extra fields set after init Track extra fields set after init in model_fields_set Mar 5, 2026
@Viicos Viicos added awaiting author revision awaiting changes from the PR author and removed ready for review labels Mar 5, 2026
navalprakhar and others added 2 commits March 5, 2026 07:10
When setting an extra field after model initialization, the field was
added to model_extra but not to model_fields_set. This was inconsistent
with regular fields which are tracked in model_fields_set when set
after init.

Updated both the inline extra field assignment and the extra_known
setattr handler to also update __pydantic_fields_set__.

Closes pydantic#11134
@navalprakhar navalprakhar force-pushed the fix/11134-model-fields-set branch from 49ff7bc to 6ac5ac6 Compare March 5, 2026 12:11
@navalprakhar navalprakhar requested a review from Viicos March 5, 2026 12:12
@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 5, 2026
@Viicos Viicos merged commit 0b7b855 into pydantic:main Mar 5, 2026
81 checks passed
@navalprakhar navalprakhar deleted the fix/11134-model-fields-set branch March 5, 2026 12:52
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-change Used for changes to existing functionality which don't have a better categorization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

model_fields_set includes extra fields only at initialization

2 participants