Repository navigation
Simplified and improved django.utils.functional.lazy(). - #16954
Merged
Merged
Conversation
Member
|
@ngnpope Thanks 👍 I've moved one of the commits to the #16957 with a regression test, as it seems to be a bugfix related to the ticket-34445. |
Member
|
I moved and reorganized the first six commit and "total ordering" removal in #16958. Please rebase when it's merged. |
Member
Merged 🎉 |
Member
Author
|
Rebased. |
felixxm
reviewed
Jun 8, 2023
felixxm
reviewed
Jun 8, 2023
Member
Author
|
Ok. I've rejigged everything, polished up the commit messages, etc. Hopefully this is in a better state now. |
felixxm
reviewed
Jun 9, 2023
ngnpope
force-pushed
the
simplify-lazy
branch
2 times, most recently
from
June 11, 2023 19:40
8b40efc to
2dcfe69
Compare
This order reads more naturally and puts methods into three groups: 1. The methods required to support the implementation of __proxy__, e.g. __deepcopy__ doesn't come from `object` and __reduce__ is overridden to support behavior required explicitly for pickling of lazy objects. 2. Methods that are specifically overridden from `object` which we don't want to inherit from the provided resultclasses. These will be skipped later when we add methods from the resultclasses. 3. Additional methods - that is _add__, __radd__, and __mod__ - don't come from `object`, but typically from `str` and `int` which are the most common use cases. Co-authored-by: Nick Pope <[email protected]>
Previously, the proxy class was prepared lazily: lazy_identity = lazy(identity, int) lazy_identity(10) # prepared here lazy_identity(10) This has a slight advantage that if the lazy doesn't end up getting used, the preparation work is skipped, however that's not very likely. Besides this laziness, it is also inconsistent in that the methods which are wrapped directly (__str__ etc.) are prepared already when __proxy__ is defined, and there is a weird half-initialized state. This change it so that everything is prepared already on the first line of the example above.
We can avoid the function call and assignment overhead which could be significant when instantiating many lazy objects. It's still easy enough to read too.
This allows for formatting of lazy objects which have a custom formatter defined by overriding the default implementation from `object`.
felixxm
approved these changes
Jun 12, 2023
Member
Author
Member
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Supersedes PR #11314.
All comments from there should be addressed.
Aside from removing much cruft left over from the Python 2 days related to
strvsbytesand the simplicity and consistency of eagerly preparing the proxy, there is some improvement in performance too in certain cases.Before
After