-
Notifications
You must be signed in to change notification settings - Fork 302
Add NoneRequired
error on optional fields
#1791
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
base: main
Are you sure you want to change the base?
Add NoneRequired
error on optional fields
#1791
Conversation
- 1 new 'none-required' error-line per x_i -> 100 new lines - 2 new error-line for sub-branch So 102 new lines in total.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
NoneRequired
error on optional fields
CodSpeed Performance ReportMerging #1791 will not alter performanceComparing Summary
|
please review |
Thanks for the contribution. Surely this is going to be a breaking change for lots of people? I think if we were to accept this, it would need to be behind a config flag, and opt in. |
Thanks for the quick reply!
I did not think of that ... If their program relies on the specific errors emitted / the number of errors, then yes it's a breaking change.
Happy to work on that if you think this makes sense / had value. |
As per the versioning policy https://docs.pydantic.dev/latest/version-policy/#pydantic-v2 I think adding additional error detail is permitted without considering it breaking. I'd be quite up for having this. Consider e.g. a three-member union: from pydantic import BaseModel
class User(BaseModel):
age: int | float | None
user = User(age='abc')
It feels like we should have an |
I'd also be up to have this included without consider it breaking. Alternatively this could make it for V3, but I'm not sure introducing an opt-in mechanism under V2 is worth it (I don't users will ever enable it, it's a welcomed improvement but not a necessity for end users). |
Change Summary
This changes the nullable validator. When the inner validation fails, it now adds a
NoneRequired
error-line.The first commit updates the nullable validator, while follow-up commits update test data. These test data become more verbose especially with recursive models. This is similar to what happens with union types when the validation fails, you get lots of error-lines.
I am not sure this is the 'correct' thing to do with optional types, but it's the simplest way I found to address pydantic#8852 and is inspired by the proposed solution there.
Note: maybe documentation could be updated following such a change. I decided to wait for feedback before proceeding further!
Example
The goal was to have this behavior on optional fields:
With this change, the errors one gets from an optional type - i.e. a type where
None
is an accepted value - look like the errors one gets when working with union types.For example, if we go
int | bool
instead ofint | None
:Both errors - with the union type and with the optional type - have the exact same shape now.
Related issue number
ValidationEror
should be provided when anOptional
type hint is used pydantic#8852Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @Viicos