Changes between Initial Version and Version 1 of Ticket #30651
- Timestamp:
- Jul 21, 2019, 11:30:54 AM (5 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #30651 – Description
initial v1 1 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.1 `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. 2 2 3 https://github.com/django/django/blob/master/django/db/models/base.py#L526 3 `https://github.com/django/django/blob/master/django/db/models/base.py#L526` 4 4 5 According to the Python 3 data model reference, a __eq__should return NotImplemented6 https://docs.python.org/3/reference/datamodel.html#object.__eq__ 7 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.5 According to the Python 3 data model reference, a `__eq__` should return NotImplemented 6 `https://docs.python.org/3/reference/datamodel.html#object.__eq__` 7 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. 8 8 9 9 This may seem like a relatively innocuous difference, but it can cause some nasty bugs. The most obvious is that for testing, 10 <A Django Model> == mock.ANY returns False, since by not returning NotImplemented it never even looks at the overridden __eq__on ANY.10 <A Django Model> == mock.ANY returns False, since by not returning NotImplemented it never even looks at the overridden `__eq__` on ANY.