#32594: Signal.disconnect() returns None when passing sender as string
-------------------------------------+-------------------------------------
Reporter: Hugo | Owner: Hugo Cachitas
Cachitas |
Type: Bug | Status: assigned
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
According to the documentation on `Signal.disconnect(receiver=None,
sender=None, dispatch_uid=None)`
The method returns True if a receiver was disconnected and False if not.
I am having this issue where `disconnect()` returns `None` when I specify
the sender as a string model reference, i.e., passing
`sender="myapp.MyModel"` instead of `sender=MyModel`.
The sender may be lazily specified as a string as documented in
`django.db.models.signals.ModelSignal`.
To achieve this functionality, a `_lazy_method` is implemented where we
can find that the return value is not properly set.
{{{
#!python
if isinstance(sender, str):
apps = apps or Options.default_apps
apps.lazy_model_operation(partial_method, make_model_tuple(sender))
else:
return partial_method(sender)
}}}
I already have a failing test at
`tests/signals/tests.py:LazyModelRefTests` that could use your input.
{{{
#!python
@isolate_apps('signals', kwarg_name='apps')
def test_disconnect_return_value(self, apps):
"""
Signal.disconnect() return value should be consistent if we
use the Model or its string reference.
"""
class Created(models.Model):
pass
def receiver(**kwargs):
pass
sender = Created
signals.post_init.connect(receiver, sender=sender, apps=apps)
self.assertTrue(
signals.post_init.disconnect(receiver, sender=sender, apps=apps)
)
self.assertFalse(
signals.post_init.disconnect(receiver, sender=sender, apps=apps)
)
sender = 'signals.Created'
signals.post_init.connect(receiver, sender=sender, apps=apps)
self.assertTrue(
signals.post_init.disconnect(receiver, sender=sender, apps=apps)
)
self.assertFalse(
signals.post_init.disconnect(receiver, sender=sender, apps=apps)
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32594>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.d749c92767da5d9e1018254181b9fc12%40djangoproject.com.