-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-40651: Bugfix of the LRU
implementation based on OrderedDict
in the collections document
#20139
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
Conversation
…in the `collections` module documents. Signed-off-by: Dongfang Qu <[email protected]>
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.
Hi @qudongfang, thanks for your contribution !
@@ -1161,6 +1161,8 @@ variants of :func:`functools.lru_cache`:: | |||
return value | |||
|
|||
def __setitem__(self, key, value): | |||
if key in self: | |||
self.move_to_end(key) | |||
super().__setitem__(key, value) | |||
if len(self) > self.maxsize: |
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.
I think this part could be replaced by self.popitem(last=False)
now.
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.
Yes, you are right.
It is cleaner to remove the oldest entry by self.popitem(last=False)
.
At the same time, It is working correctly.
Do you suggest I update it in this pull request?
Not sure about the conventions here.
Normally we are only supposed to change the code directly related to the issue.
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.
I don't know, I think popitem()
was added in Python3 so this block of code needed to be compatible with Python 2 until this year.
Let's wait for @rhettinger review, I can change it in another PR if he thinks it's needed.
Thanks @qudongfang for the PR, and @rhettinger for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
…thonGH-20139) (cherry picked from commit bb8635c) Co-authored-by: qudongfang <[email protected]>
bpo-40651: Bugfix of the
LRU
implementation based onOrderedDict
in thecollections
module documents.It didn't consider the calls to
__setitem__
method as usage.https://bugs.python.org/issue40651