-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-128576: feat: Add decorator functools.lru_cache to method tkinter.Misc.winfo_rgb
#128577
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cdbc2d5
feat: Add decorator `functools.lru_cache` to method `tkinter.Misc.win…
Xiaokang2022 7eea471
📜🤖 Added by blurb_it.
blurb-it[bot] 004cd11
style: Improve the changelog
Xiaokang2022 27136d7
style: Sort the import
Xiaokang2022 acce9af
feat: Add magic methods `__eq__` and `__hash__` to class `tkinter.Misc`
Xiaokang2022 da6a174
fix: Fix the magic method `__eq__`
Xiaokang2022 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
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2025-01-07-05-25-57.gh-issue-128576.qzDkju.rst
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,2 @@ | ||
| Add decorator :func:`functools.lru_cache` to the :mod:`tkinter` method | ||
| :meth:`!winfo_rgb`. |
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.
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.
LRU-cached methods do not play well with
functools.lru_cacheif__hash__and__eq__are not implemented (see https://docs.python.org/3/faq/programming.html#faq-cache-method-calls) because it also caches the instance.Uh oh!
There was an error while loading. Please reload this page.
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.
The above is quoted from: https://docs.python.org/3.13/faq/programming.html#faq-cache-method-calls
But in reality, no matter how the attribute is updated, the return value of the method
winfo_rgbwill not change for a particular parameter.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.
The problem is not the return value. It's how the cache is built. The instance itself is cached by
lru_cache. I'm not sure we want to keep them alive. The method will know in advance all the existing instances of Misc that were created and that used this method. This is something we don't want (the cache is a full-fledged dictionary so a reference to those widgets will remain)Uh oh!
There was an error while loading. Please reload this page.
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.
Now, even if you add those magic methods, the instance will stil be cached. By the way, is it possible to change the color display of the current widget (e.g., directcolor to greyscale) and that this would affect the return value? if so, we cannot cache this method.
I am really not comfortable with caching tk widgets in general. Those objects should be released whenever they can. It would be an overkill to clear the cache when one object has been deleted as well =/
If LRU-caching is possible, instead of storing the instance itself in the cache, we could store the type and the hash of that instance using a class attribute. We could also remove the instance in
__del__so that the hash value can be reused by other objects.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.
After my testing, this doesn't affect the return value of the method, if my test method is not wrong.
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.
If the result is the same for any widget, perhaps the method should be a class method instead of an instance method. This may eliminate the need to cache instances.
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.
But is this always the same? according to the docs, this depends on the widget itself so I'm afraid we cannot do this.
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.
Although the documentation says that, I haven't found a way to corroborate what the documentation says, and if there is a way, hopefully it can be pointed out.
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.
cc @serhiy-storchaka