Open CaptainException opened 3 months ago
This might be fixed now, the logic to set default = None if optional has been moved before the logic to set it to PydanticUndefined. I can't upgrade to the newest version because of a different bug it introduced but it might be good to test this again.
EDIT: Looking this over, I'm actually pretty positive it should be fixed, try it now on the latest release.
Versions:
Bug Description:
Using
fields_optional
on a Model which has a field where adefault
parameter is set causesTypeError: cannot specify both default and default_factory
The full error trace (minus the hot reload stuff) is:
My Model is defined like this:
And the Schema like this:
Manually specifying all optional fields minus the field using
default
also works:but this defeats the purpose of the
__all__
shortcut.This is a similar bug as #1019 and #1080. The PR #1100 is a fix for this but I don't know if that has any side effects, or if there is a better solution.
When using PATCH on a resource the id is always the only required field, so maybe we could have a
fields_optional_exclude
option similar toexclude
so that we don't have to manually specify all fields, which can be tedious on big Models. An exclude option is shorter and more readable.But this obviously does not fix the issue for people who are using fields with a default value that can be optional in their Schema.