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(field): add dump_alias and load_alias options #1695
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1695 +/- ##
===========================================
Coverage 100.00% 100.00%
===========================================
Files 25 21 -4
Lines 5109 3921 -1188
Branches 1050 785 -265
===========================================
- Hits 5109 3921 -1188
Continue to review full report at Codecov.
|
e3d8542
to
98e91c7
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.
This is looking good.
Questions:
- are we satisfied it'll work correctly with inheritance? The inheritance landscape with aliases is already mind numbingly complicated, will this just slot in or confuse matters more?
- How do we deal with skipping/excluding from
dict()
etc.? I guess the simplest solution might be an objectExclude
which can only be used ondump_alias
.
@samuelcolvin This is the whole question and honestly I need to think it over. This is why it's just a draft. I feel like we still need some feedback on the desired API. The best would be to have a bunch of tests with nested models and load/dump + exclude fields to have the expected behaviour for each one and see if there are no edge cases or something |
Just saw this while working on #660 ... not sure if I'm out of lying trying to create a pull requests for that or if the discussion here has changed the view on it. That said, there are a few arguments for separating the two:
would we want to allow the fields to be "included" by the TestModel(a=3).dict(include={"a"}) # {"a_dump": 3} There are cases I know where this might be the desired behavior. There are also arguments against the second point: I can just as well think of cases where this might be confusing because the author of the class wants the field to be hidden permanently and not being randomly resurrected by a overriding using dict or json. Especially confusing would might be a case like this: TestModel(a=3).dict() # {"b": "foo", "c": "bar"}
# user decides they don't want to see "b"
TestModel(a=3).dict(exclude={"b"}) # {"a_dump": 3, "c": "bar"} ... what?? why did a_dump show up, it wasn't there before. @samuelcolvin @PrettyWood Were there any thoughts on "dump_alias" vs. "exclude" in class config / field or am I just confusing a bunch of issues here? |
What if we didn't allow excluding via That way we get clear separation of concerns and a simple interface. |
I'm really in favor of this. I'll be back on this PR in the next few days. Thanks a lot @daviskirk for the suggestion. Sorry I missed your message before |
#2231 now has a working candidate (see: #2231 (comment) in particular, in case that influences anything here) |
98e91c7
to
874e27b
Compare
@samuelcolvin I went back on this. PS: fastapi tests fail because they use @tiangolo I would like your input on this matter if possible. I reckon differentiating serialization and deseralization will be useful for fastapi and this is a start |
Closing this. Although we'll definitely support |
Change Summary
Related issue number
closes #624
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)