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 a config field to make all model fields optional #3121
Conversation
This is done to match TypedDict interface, see https://www.python.org/dev/peps/pep-0589/#totality
The PR is ready, please review |
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.
LGTM otherwise! Not the first time people asked for this.
And it can properly be used afterwards by FastAPI for update payloads where only a subset of your model is often sent
I'll still wait for @samuelcolvin opinion for new config params
Please update |
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Ok, the doc is updated, please review |
@christophelec thanks for this! I actually just submitted a PR for the exact same thing accidentally 😅. I'm wondering if you'd be open to checking mine out and possibly adding the helper class method? You can find the PR here: Basically, it just allows you to declare your full model, and then call |
Actually I take that back - this is similar to my PR but not quite the same. This PR would still allow |
if not cls.total: | ||
field.type_ = Optional[field.type_] | ||
field.required = False |
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.
if not cls.total: | |
field.type_ = Optional[field.type_] | |
field.required = False | |
if not cls.total: | |
field.required = False |
@lsapan is right. We shouldn't change the type and accept None
.
Making it non required is enough
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.
I think it would be great to
- add a schema example to show there are no required keys
- add a test with "not nullable" keys like @lsapan said
- maybe a helper to generate it. If so doc as well
Sorry about the delay @lsapan, I checked your PR and found some key difference, that might explain why I found it necessary to change the type to Optional and allow null as you mention : class Foo(BaseModel):
a: str = Field(...)
b: str = Field(...)
c: Optional[str] = Field()
Bar = create_partial_model(Foo)
b = Bar(a='x', c=None)
assert b.dict() == b.dict(exclude_unset=True) == {'a': 'x', 'c': None} While in mine : b = Bar(a='x', c=None)
assert b.dict() == {'a': 'x', 'b': None, 'c': None}
assert b.dict(exclude_unset=True) == {'a': 'x', 'c': None} I understand why you made this decision : it allows not to muddy the type by making it Optional, and overall fits better the use case of a patch for FastAPI (as I expect forgetting to use However, I think the fact that the field does not appear at all, whether What do you think @PrettyWood @lsapan ? To me if we're ok with having the field not appear whether Otherwise, I found an issue with my PR about the handling of |
@christophelec no problem at all! So for what it's worth, that's actually a slightly different discussion than the The problem with making all the fields optional is that (from your example above), you could do:
According to your model, |
Change Summary
This PR adds a Config to turn all fields of a model Optional with a None default.
The underlying use is to create a copy of an existing model, but where fields are Optional, to allow for an update of only the fields provided in FastAPI.
An example is provided in the issue below.
Related issue number
Closes #3120
It seems it would also help with #2272 (comment)
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)