#35984: functools.singledispatchmethod on Django models don't work
-----------------------+-----------------------------------------
Reporter: vodik | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------+-----------------------------------------
I'm on Django 4.2.17 and Python 3.13.1
The following code works on Python 3.12 and I believe
https://github.com/python/cpython/issues/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.
{{{#!python
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:
{{{#!python
for t in test.test2_set.all():
print("id:", id(t)) # always changes
t.bug(4) # only ever prints a single id
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/35984>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.