-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
gh-127750: Fix singledispatchmethod caching (v2) #128648
base: main
Are you sure you want to change the base?
gh-127750: Fix singledispatchmethod caching (v2) #128648
Conversation
import weakref # see comment in singledispatch function | ||
self._method_cache = weakref.WeakKeyDictionary() | ||
def __set_name__(self, obj, name): | ||
self.attrname = name |
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.
Check cached_property.__set_name__
, it has some more stuff in it - might be needed here as well.
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.
Hmm. The additions there prevent something like this:
@dataclass(frozen=True)
class A:
value: int
@singledispatchmethod
def dispatch(self, x):
return id(self)
renamed_dispatch = dispatch # allowed? if so, how should it behave
The corresponding test for the cached_property
for this is
cpython/Lib/test/test_functools.py
Line 3315 in 34e840f
def test_reuse_different_names(self): |
But on current main renaming is allowed for the singledispatchmethod.
I am not sure here what the desired behavior is (and why)
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 this implementation is desirable, maybe later someone who knows more about this can comment.
Version based on idea from @dg-pb in #127839. This version
__hash__
/__eq__
Regression in Django with singledispatchmethod on models #127750There is still a cache (stored on the object instances). Quick benchmark (windows, non-pgo):
(note that the alternative to this PR is not to keep main, but to revert #107148)