Skip to content

Conversation

anvilpete
Copy link
Contributor

@anvilpete anvilpete commented Sep 7, 2025

Change Summary

This PR depends on the following PR in pydantic-core: pydantic/pydantic-core#1722.

This change adds an extra parameter to the validation functions, overriding any other model configuration.

SomeModel(BaseModel):
    my_field: int
    model_config = ConfigDict(extra="allow")

data = SomeModel.model_validate(
    {"myfield": 1, "extra field": 2},
    extra="forbid"  # override model config and forbid extra fields just this time
)

Related issue number

feature #9278

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

Copy link
Contributor

github-actions bot commented Oct 1, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  config.py
  main.py
  type_adapter.py
  pydantic/plugin
  __init__.py
Project Total  

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

Copy link

codspeed-hq bot commented Oct 1, 2025

CodSpeed Performance Report

Merging #12233 will improve performances by 23.71%

Comparing anvilpete:validate-extra (da9ba3f) with main (af271da)

Summary

⚡ 1 improvement
✅ 45 untouched

Benchmarks breakdown

Benchmark BASE HEAD Change
test_failed_rebuild 267.8 µs 216.5 µs +23.71%

@anvilpete anvilpete marked this pull request as ready for review October 1, 2025 18:26
@anvilpete
Copy link
Contributor Author

Please review. This PR requires the recent pydantic-core 2.40.0 release, so I've included the version bump and test fixes.

Copy link
Contributor

@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, this looks good to me. The pydantic-core version got bumped separately, so if you rebase on main the diff should be possible to clean up a fair bit.

@Viicos Viicos removed the relnotes-fix Used for bugfixes. label Oct 5, 2025
@anvilpete
Copy link
Contributor Author

Thanks for your quick review, ready to merge now I think. Resolves #9278.

@davidhewitt davidhewitt linked an issue Oct 5, 2025 that may be closed by this pull request
13 tasks
@davidhewitt davidhewitt merged commit e49d758 into pydantic:main Oct 5, 2025
63 checks passed
@Viicos Viicos added needs-blogpost-entry This PR needs to be documented in the release notes blog post and removed ready for review labels Oct 6, 2025
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.

Configure behavior for extra fields at runtime / validation time

4 participants