Closed
Description
Bug report
Bug description:
Also reported upstream to Django here.
The following code works on Python 3.12 and I believe #85160 is the cause.
When traversing a relationship and then calling a singledispatchmethod
method registered on a model, the reference to self seems to get cached and locked to the first created model.
class Test(models.Model):
...
class Test2(models.Model):
relationship = models.ForeignKey("Test", on_delete=models.CASCADE)
@singledispatchmethod
def bug(self, x):
print(">", id(self))
@bug.register
def _(self, x: int):
print(">", id(self))
and then it's pretty easy to trigger:
for t in test.test2_set.all():
print("id:", id(t)) # always changes
t.bug(4) # only ever prints a single id
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
- gh-127750: Improve caching in singledispatchmethod #127751
- gh-127750: Fix singledispatchmethod caching #127839
- gh-127750: Fix singledispatchmethod caching (v2) #128648
- gh-127750: Fix and optimize functools.singledispatchmethod() #130008
- [3.13] gh-127750: Fix functools.singledispatchmethod() #130029
- gh-127750: Improve repr of functools.singledispatchmethod #130220
- gh-127750: Restore inspect and pydoc support of singledispatchmethod #130309
- [3.13] gh-127750: Backport some tests for singledispatchmethod (GH-130309) #130340
- [3.12] gh-127750: Backport some tests for singledispatchmethod (GH-130309) (GH-130340) #130341