#30651: Model.__eq__ never returns NotImplemented
-------------------------------------+-------------------------------------
     Reporter:  Elizabeth Uselton    |                    Owner:  Elizabeth
                                     |  Uselton
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  2.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Elizabeth Uselton:

Old description:

> Model.__eq__ never returns NotImplemented if it encounters an object it
> doesn't know how to compare against. Instead, if the object it is
> comparing to is not a Django Model, it automatically returns False.
>
> https://github.com/django/django/blob/master/django/db/models/base.py#L526
>
> According to the Python 3 data model reference, a __eq__ should return
> NotImplemented
> https://docs.python.org/3/reference/datamodel.html#object.__eq__
> If a.__eq__(b) returns NotImplemented, then b.__eq__(a) will be tried. If
> both return NotImplemented, then an is check is performed, and if that
> fails it returns False.
>
> This may seem like a relatively innocuous difference, but it can cause
> some nasty bugs. The most obvious is that for testing,
> <A Django Model> == mock.ANY returns False, since by not returning
> NotImplemented it never even looks at the overridden __eq__ on ANY.

New description:

 `Model.__eq__` never returns NotImplemented if it encounters an object it
 doesn't know how to compare against. Instead, if the object it is
 comparing to is not a Django Model, it automatically returns False.

 `https://github.com/django/django/blob/master/django/db/models/base.py#L526`

 According to the Python 3 data model reference, a `__eq__` should return
 NotImplemented
 `https://docs.python.org/3/reference/datamodel.html#object.__eq__`
 If `a.__eq__(b)` returns NotImplemented, then `b.__eq__(a)` will be tried.
 If both return NotImplemented, then an is check is performed, and if that
 fails it returns False.

 This may seem like a relatively innocuous difference, but it can cause
 some nasty bugs. The most obvious is that for testing,
 <A Django Model> == mock.ANY returns False, since by not returning
 NotImplemented it never even looks at the overridden `__eq__` on ANY.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30651#comment:1>
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/068.5a382ad1c469b1f87ad6e16c109ec155%40djangoproject.com.

Reply via email to