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: support default_factory
with BaseModel.construct
#1755
feat: support default_factory
with BaseModel.construct
#1755
Conversation
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.
To me this seems fine however I have a couple of questions:
I see that the only other place where __field_defaults__
is used is in BaseModel._iter()
when excluding default values. Would it make sense to remove it altogether and rely on __fields__
for that?
Apart from that, was __field_defaults__
used as an optimisation to keep the number of deepcopy
s down? If yes and if the impact is measurable, would it be better to use get_default
only for fields with default factory?
Hello @zoopp EDIT: I added a basic commit to remove it. We can discuss whether or not we want to go even further but I'm afraid it would take us away from the issue |
518cf61
to
b5554f7
Compare
Sure, I was only inquiring about it out of curiosity. |
I'm wondering if there anything I can do expedite a potential merge of this pull request. Is there any specific feedback I can provide to help with it? |
@zoopp As Samuel is busy working, I would just fork this repo and merge the PRs you need if you can't wait an extra month |
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 looks like it could be fine, but questions:
- could this be considered a breaking change, after all people use
__fields__
a lot, they probably use__field_defaults__
too? at least I think we need a note in the change description - this is currently conflicting with master, could you fix
- does this have a measurable effect on performance.
@samuelcolvin I'm not sure As for the performance question it slows down things for sure when no with
|
thanks so much. I think this is definitely better than what went before. |
Change Summary
Some changes have been made on
default_factory
to avoid calling it for nothing and avoid some side effects in #1504 and #1712. And this line changed the default value that is used inBaseModel.construct
toNone
.Instead of changing
__field_defaults__
, which would be a mess, let's just useModelField.get_default
inconstruct
.The feature is hence scoped to
BaseModel.construct
and has the exact same behaviour asBaseModel.__init__
Related issue number
closes #1732
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)