-
Notifications
You must be signed in to change notification settings - Fork 268
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
Add support for typing.Never
#1579
base: main
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #1579 will not alter performanceComparing Summary
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1579 +/- ##
==========================================
- Coverage 90.21% 89.64% -0.57%
==========================================
Files 106 114 +8
Lines 16339 17980 +1641
Branches 36 40 +4
==========================================
+ Hits 14740 16118 +1378
- Misses 1592 1842 +250
- Partials 7 20 +13
... and 51 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
please review |
@pydantic/oss would love your input on this design. I would have assumed a simpler design where any type containing a |
Change Summary
Add support for
typing.Never
.Validation
The validator implementation might look a bit weird. This is because of
RootModel
. Even though it doesn't really make sense, I assume that the following should work:and because
RootModel
passesPydanticUndefined
to the validator when no data is provided, the validator needs to work withPydanticUndefined
. If we don't expectRootModel[Never]()
to ever work, we can simplify the implementation and simply reject all values, but there might be another issue: users might wonder how the following works whileRootModel
does not:Also, simply rejecting everything in the validator means that the validated model instance will not contain any value for the
Never
fields. When users try to access those fields, they will get an error, but the error is going to be something likeThis kind of makes sense because indeed the
Never
fields never hold any value, but at the same time they are defined in the model, so it kind of makes sense as well to say that the instances do have those attributes. We should provide some better error messages, but before that gets handled in pydantic, I think in core we can returnPydanticUndefined
as a placeholder.Serialisation
Serialising the never fields themselves will trigger an error. For instance,
On the other hand, serialising models containing never fields should work and the never fields should be omitted:
These are covered by the new test cases included in this PR.
Manual integration test
Tested locally with the counterpart implementation for pydantic and it worked.
Related issue number
Part of pydantic/pydantic#9731
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @sydney-runkle