-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Improve __setattr__
performance of Pydantic models by caching setter functions
#10868
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3a285fd
fast setattr
MarkusSintonen 6f0ab2d
Code review fixes.
MarkusSintonen 668d4dd
Add more tests, fix priv field corner case, code review fixes
MarkusSintonen 7dfbe50
Predefine simple attr setters without captured closure.
MarkusSintonen 404b8b7
Code review changes
MarkusSintonen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from functools import cached_property | ||
|
||
import pytest | ||
|
||
from pydantic import BaseModel, ConfigDict, ValidationError | ||
|
||
|
||
class InnerValidateAssignment(BaseModel): | ||
model_config = ConfigDict(validate_assignment=True) | ||
inner_field1: str | ||
inner_field2: int | ||
|
||
|
||
class Model(BaseModel): | ||
field1: str | ||
field2: int | ||
field3: float | ||
inner1: InnerValidateAssignment | ||
inner2: InnerValidateAssignment | ||
|
||
_private_field1: str | ||
_private_field2: int | ||
_private_field3: float | ||
|
||
@cached_property | ||
def prop_cached1(self) -> str: | ||
return self.field1 + self._private_field1 | ||
|
||
@cached_property | ||
def prop_cached2(self) -> int: | ||
return self.field2 + self._private_field2 | ||
|
||
@cached_property | ||
def prop_cached3(self) -> float: | ||
return self.field3 + self._private_field3 | ||
|
||
|
||
def test_setattr(benchmark): | ||
def set_attrs(m): | ||
m.field1 = 'test1' | ||
m.field2 = 43 | ||
m.field3 = 4.0 | ||
m.inner1.inner_field1 = 'test inner1' | ||
m.inner1.inner_field2 = 421 | ||
m.inner2.inner_field1 = 'test inner2' | ||
m.inner2.inner_field2 = 422 | ||
m._private_field1 = 'test2' | ||
m._private_field2 = 44 | ||
m._private_field3 = 5.1 | ||
m.prop_cached1 = 'cache override' | ||
m.prop_cached2 = 10 | ||
m.prop_cached3 = 10.1 | ||
|
||
inner = {'inner_field1': 'test inner', 'inner_field2': 420} | ||
model = Model(field1='test', field2=42, field3=3.14, inner1=inner, inner2=inner) | ||
benchmark(set_attrs, model) | ||
|
||
model.field2 = 'bad' # check benchmark setup | ||
with pytest.raises(ValidationError): | ||
model.inner1.field2 = 'bad' | ||
|
||
|
||
def test_getattr(benchmark): | ||
def get_attrs(m): | ||
_ = m.field1 | ||
_ = m.field2 | ||
_ = m.field3 | ||
_ = m.inner1.inner_field1 | ||
_ = m.inner1.inner_field2 | ||
_ = m.inner2.inner_field1 | ||
_ = m.inner2.inner_field2 | ||
_ = m._private_field1 | ||
_ = m._private_field2 | ||
_ = m._private_field3 | ||
_ = m.prop_cached1 | ||
_ = m.prop_cached2 | ||
_ = m.prop_cached3 | ||
|
||
inner = {'inner_field1': 'test inner', 'inner_field2': 420} | ||
model = Model(field1='test1', field2=42, field3=3.14, inner1=inner, inner2=inner) | ||
model._private_field1 = 'test2' | ||
model._private_field2 = 43 | ||
model._private_field3 = 4.14 | ||
benchmark(get_attrs, model) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.