Skip to content
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

feat: add support to return default if input is None #1501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Meetesh-Saini
Copy link

Change Summary

Added support to return default value when passing None as input.
Previously, passing None explicitly to a required field with a default value would raise a ValidationError

Related issue number

fix pydantic/pydantic#8972

Checklist

  • Unit tests for the changes exist
  • Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

please review

Copy link

codecov bot commented Oct 24, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 4 lines in your changes missing coverage. Please review.

Project coverage is 89.08%. Comparing base (ab503cb) to head (e5cf2ac).
Report is 207 commits behind head on main.

Files with missing lines Patch % Lines
src/validators/model_fields.rs 88.23% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1501      +/-   ##
==========================================
- Coverage   90.21%   89.08%   -1.13%     
==========================================
  Files         106      112       +6     
  Lines       16339    17890    +1551     
  Branches       36       40       +4     
==========================================
+ Hits        14740    15938    +1198     
- Misses       1592     1932     +340     
- Partials        7       20      +13     
Files with missing lines Coverage Δ
python/pydantic_core/core_schema.py 94.85% <100.00%> (+0.08%) ⬆️
src/validators/model_fields.rs 94.01% <88.23%> (-2.46%) ⬇️

... and 49 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b21b0f...e5cf2ac. Read the comment docs.

Copy link

codspeed-hq bot commented Oct 24, 2024

CodSpeed Performance Report

Merging #1501 will not alter performance

Comparing Meetesh-Saini:dev-none-default (e5cf2ac) with main (9b21b0f)

Summary

✅ 155 untouched benchmarks

@davidhewitt
Copy link
Contributor

I wonder if there is a simpler API here. Rather than going for config, given a BeforeValidator works here, can we do something simpler like:

class Foo(BaseModel):
    bar: Annotated[list[int], NoneAsDefault] = [0,1,2]
    baz: Annotated[list[str], NoneAsDefault] = ["a", "b", "c"]

... we might be able to do this purely in the pydantic side.

cc @sydney-runkle for opinions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Return default value if the input is None
2 participants