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 exclude as field parameter #2231
Add exclude as field parameter #2231
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2231 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 25 25
Lines 5109 5120 +11
Branches 1050 1045 -5
=========================================
+ Hits 5109 5120 +11
Continue to review full report at Codecov.
|
No changes here, just the change description ATM. |
f6a2772
to
c0d2727
Compare
c0d2727
to
61efbaa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to see it's finally getting implemented, thank you!
af2ea07
to
652bcaf
Compare
Implementation wise, this is just about the way I'd like it. A few key points here:
In summary: reviews are very welcome |
I am probably missing something, but why |
nope, not missing anything. Typo on my part, sorry. I updated the comment. |
a214adc
to
58b30d3
Compare
58b30d3
to
890a5f5
Compare
9025a3b
to
6ee95b0
Compare
54a9302
to
a22cfba
Compare
- Add "exclude" / "include" as a field parameter so that it can be configured using model config (or fields) instead of purely at `.dict` / `.json` export time. - Unify merging logic of advanced include/exclude fields - Add tests for merging logic and field/config exclude/include params - Closes pydantic#660
* Increase test coverage * Remove (now) redundant type checks in Model._iter: New exclusion/inclusion algorithms guarantee that no sets are passed further down.
Running benchmarks this vs. master is at: this: pydantic best=33.225μs/iter avg=33.940μs/iter stdev=1.120μs/iter version=1.7.3 master: pydantic best=32.901μs/iter avg=33.276μs/iter stdev=0.242μs/iter version=1.7.3
* Fix/simplify type annotations * Allow both ``True`` and ``Ellipsis`` to be used to indicate full field exclusion * Reenable hypothesis plugin (removed by mistake) * Update advanced include/include docs to use ``True`` instead of ``...``
a22cfba
to
af7d2e5
Compare
This way, the model field object does not need to concern itself with dealing with field into specific fields. (Same was done for alias in a previous commit).
af7d2e5
to
d733814
Compare
@samuelcolvin when can we expect this functionality to be released? |
@daviskirk Do you think it could be possible to also set a callable as a value? class User(BaseModel):
name: str
friends: list[str] | None = Field(None, exclude=lambda x: x is None)
assert User(name="Alone", friends=None).json() == '{"name": "Alone"}'
assert User(name="Popular", friends=["Bob", "Alice"]).json() == '{"name": "Popular", "friends": ["Bob", "Alice"]}' WDYT? |
I think that would be possible... I do think that it would open up a pretty large field of possibilities for users to "use"/"misuse" that I'm not sure I can predict right now. Given all the larger changes and v2 coming along I'm not sure if we want to commit to such a broad feature without lot's of users actually really wanting it. For example, I can behavior the behavior of "exclude" (as implemented here already with being able to drill down into nested fields) being popular enough that we might want to expand it to other fields like Not that I'm against this... I think it would be a pretty fancy feature, just trying to look out for you and samuel who might be stuck supporting it ;) |
You're absolutely right. I just shared a recent need and got away easily with custom |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise this looks great.
This is awesome. Thank you so much @daviskirk 🎉 |
…all fields Small regression in pydantic#2231. The shallow copy done with `Config.copy_on_model_validation = True` (default behaviour) was using excluded / included fields when it should just copy everything closes pydantic#3195
…all fields Small regression in pydantic#2231. The shallow copy done with `Config.copy_on_model_validation = True` (default behaviour) was using excluded / included fields when it should just copy everything closes pydantic#3195
Any release predictions on this? |
Change Summary
.dict
/.json
export time.Related issue number
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)