Open axelv opened 1 year ago
Thanks for the proposal! I understand the issue (and wonder how much easier things like that would be should we have optional chaining available in python) but I'm afraid with the changes we would also have to have a different BaseModel (that will automatically exclude default_factory
generated data on the resource serialization). Which in its turn will make things more complicated.
Optional typing for an array can be annoying because it forces you to check for None each time you want to iterate over a array. A better solution might be (which I implemented in this pull request):
Field(default=...)
for required elementsField(default_factory=list)
for optional array elementsField(default=None)
for optional (non-array) elementsThis makes it more convenient to iterate over array elements:
becomes
Note that for required elements the default=... will validate that a non-empty list is passed when parsing. More info on this https://docs.pydantic.dev/latest/usage/models/#required-optional-fields