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
Implement Optional required #1031
Implement Optional required #1031
Conversation
…), make it persist
Needed for when _type_analysis is re-run in Generics
Codecov Report
@@ Coverage Diff @@
## master #1031 +/- ##
==========================================
- Coverage 100% 99.96% -0.04%
==========================================
Files 20 20
Lines 3316 3332 +16
Branches 653 659 +6
==========================================
+ Hits 3316 3331 +15
- Misses 0 1 +1
Continue to review full report at Codecov.
|
This is ready for review @samuelcolvin , @dmontagu . I didn't think the optional nullable was worth extra docs, but let me know if you think this should include some. |
Thanks for the code review! Done |
Sure! |
The mypy plugin may need to be changed to reflect this (either it needs to be, or it was a bug before |
Thanks @dmontagu ! I haven't been able to check out the mypy plug-in (although I look forward to using it) Should we do the update here or in a separate PR? |
I looked at the mypy plugin code and it actually currently treats It would be nice to add tests for this, and I wrote some, but after some reflection I think it will be easiest to just put it in a separate PR (really need to make it easier to write unit tests for the mypy plugin...). I'll take responsibility for writing additional mypy plugin tests, and I'll wait until after this is merged though and we make the desired behavior explicit. I think it would be nice to add some tests here for (Currently the plugin does not treat |
Thanks for the review @dmontagu ! I added tests for I guess that will have to be updated if the current behavior is changed. Thanks for taking care of the mypy plug-in! |
Thanks for the review, I applied all the requested changes. |
thanks so much, I'll release v1.2 now. |
Thanks for fixing the tests I missed And thanks for merging this! |
* Implement Optional required, when creating a ModelField(required=True), make it persist * Add test for nullable required * Improve formatting of Undefined custom object * Refactor field infer/creation with Undefined to make it idempotent Needed for when _type_analysis is re-run in Generics * Add PR changes * Increment/update tests with code review * Update/refactor Undefined implementation with code review * Fix BoolUndefined as string type for mypy, not runtime * Add docs about required Optional * Add explicit tests for Any * Apply code review requested changes * move tests out of test_validators.py
Change Summary
Make
ModelField(required=True)
keep the required flag.This allows solving tiangolo/fastapi#646 without hacks, and solves #990, allowing required nullables with:
A benefit is that external libraries using Pydantic like FastAPI or others (and more to come) can create a
ModelField
object, set therequired
argument (ModelField(required=True)
) and expect it to be preserved without having to re-set it after creation withfield.required = True
.Related issue number
#990 #1028
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)