[Django] #26083: __eq__, __hash__ and Python 3

20 views
Skip to first unread message

Django

unread,
Jan 14, 2016, 9:39:01 AM1/14/16
to django-...@googlegroups.com
#26083: __eq__, __hash__ and Python 3
--------------------------------------+--------------------
Reporter: atombrella | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
I ran python -Werror runtests.py for DRF, and noticed quite a lot of
warnings about __eq__ and __hash__, and then checked Django's repository.

DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in
3.x

--
Ticket URL: <https://code.djangoproject.com/ticket/26083>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 14, 2016, 9:50:23 AM1/14/16
to django-...@googlegroups.com
#26083: __eq__, __hash__ and Python 3
-------------------------------------+-------------------------------------
Reporter: atombrella | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Core (Other) | Version: master
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
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Old description:

> I ran python -Werror runtests.py for DRF, and noticed quite a lot of
> warnings about __eq__ and __hash__, and then checked Django's repository.
>
> DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in
> 3.x

New description:

I ran `python -Werror runtests.py` for DRF, and noticed quite a lot of

warnings about `__eq__` and `__hash__`, and then checked Django's
repository.

`DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in
3.x`

--

Comment:

Can you point to where the errors are coming from? I'm not aware of any
deprecation warnings when running Django's test suite.

--
Ticket URL: <https://code.djangoproject.com/ticket/26083#comment:1>

Django

unread,
Jan 14, 2016, 10:13:50 AM1/14/16
to django-...@googlegroups.com
#26083: __eq__, __hash__ and Python 3
-------------------------------------+-------------------------------------
Reporter: atombrella | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Core (Other) | Version: master
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
-------------------------------------+-------------------------------------

Comment (by atombrella):

/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/db/__init__.py:29: DeprecationWarning: Overriding __eq__


blocks inheritance of __hash__ in 3.x

class DefaultConnectionProxy(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/core/validators.py:31: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class RegexValidator(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/core/validators.py:151: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class EmailValidator(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/core/validators.py:289: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class BaseValidator(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/core/validators.py:352: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class DecimalValidator(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/core/checks/messages.py:15: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class CheckMessage(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/core/cache/__init__.py:90: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class DefaultCacheProxy(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/db/models/sql/datastructures.py:28: DeprecationWarning:


Overriding __eq__ blocks inheritance of __hash__ in 3.x

class Join(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/template/context.py:32: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class BaseContext(object):
/home/mads/djangorestframework/local/lib/python2.7/site-
packages/django/template/base.py:138: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class Origin(object):

--
Ticket URL: <https://code.djangoproject.com/ticket/26083#comment:2>

Django

unread,
Jan 14, 2016, 7:54:13 PM1/14/16
to django-...@googlegroups.com
#26083: Python 2 deprecation warnings for classes that define __eq__() but not
__hash__()
--------------------------------------+------------------------------------

Reporter: atombrella | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


Comment:

From what I read [http://stackoverflow.com/questions/15471333/how-to-
eliminate-a-python3-deprecation-warning-for-the-equality-operator on
stackoverflow], the lack of `__hash__()` is only a problem if these
objects are used in something like a `set()`, which seems unlikely in
these cases which is why we haven't had any report of problems, I suppose.

If I could at least reproduce the warnings I would find some value in
fixing them, but I can't.

--
Ticket URL: <https://code.djangoproject.com/ticket/26083#comment:3>

Django

unread,
Jul 6, 2016, 1:16:47 AM7/6/16
to django-...@googlegroups.com
#26083: Python 2 deprecation warnings for classes that define __eq__() but not
__hash__()
--------------------------------------+------------------------------------
Reporter: atombrella | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Description changed by benf-wspdigital:

Old description:

> I ran `python -Werror runtests.py` for DRF, and noticed quite a lot of

> warnings about `__eq__` and `__hash__`, and then checked Django's


> repository.
>
> `DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in

> 3.x`

New description:

Many classes within Django define the `__eq__` method, which in Python 3
will have disruptive implications for inheritance of `__hash__`. See the
[https://docs.python.org/3.4/reference/datamodel.html#object.__hash__
Python 3 documentation for `__hash__`]:

If a class does not define an __eq__() method it should not define a
__hash__() operation either; if it defines __eq__() but not __hash__(),
its instances will not be usable as items in hashable collections. […]

and continues on to describe in detail the implications of defining
`__eq__` and/or `__hash__`, and exactly why one would implement one and/or
the other on a class.

The warning emitted by `python2 -3` for this is:

DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__
in 3.x

--

--
Ticket URL: <https://code.djangoproject.com/ticket/26083#comment:4>

Django

unread,
Jul 6, 2016, 2:02:46 AM7/6/16
to django-...@googlegroups.com
#26083: Python 2 deprecation warnings for classes that define __eq__() but not
__hash__()
--------------------------------------+------------------------------------
Reporter: atombrella | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Core (Other) | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by benf-wspdigital):

The original reporter discovered these warnings by running the test suite
for the ‘django-rest-framework‘ library.

I have followed the instructions for [http://www.django-rest-
framework.org/topics/contributing/#testing running the test suite for
django-rest-framework], and got the same warnings originally reported in
the Django code base:

{{{
$ python2 -3 -Wall ./runtests.py -q --fast
[…]
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/core/checks/messages.py:15: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class CheckMessage(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/core/cache/__init__.py:90: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class DefaultCacheProxy(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/db/__init__.py:29: DeprecationWarning: Overriding __eq__


blocks inheritance of __hash__ in 3.x

class DefaultConnectionProxy(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/core/validators.py:31: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class RegexValidator(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/core/validators.py:158: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class EmailValidator(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/core/validators.py:296: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class BaseValidator(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/core/validators.py:359: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class DecimalValidator(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/db/models/sql/datastructures.py:28: DeprecationWarning:


Overriding __eq__ blocks inheritance of __hash__ in 3.x

class Join(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/template/context.py:32: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class BaseContext(object):
/Users/benfinney/.virtualenvs/test-install/lib/python2.7/site-
packages/django/template/base.py:138: DeprecationWarning: Overriding


__eq__ blocks inheritance of __hash__ in 3.x

class Origin(object):
[…]
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/26083#comment:5>

Django

unread,
Jan 27, 2017, 12:57:57 PM1/27/17
to django-...@googlegroups.com
#26083: Python 2 deprecation warnings for classes that define __eq__() but not
__hash__()
--------------------------------------+------------------------------------
Reporter: Mads Jensen | Owner: nobody
Type: Cleanup/optimization | Status: closed

Component: Core (Other) | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


Comment:

At this point Django doesn't support Python 2 in master so silencing the
warnings is no longer a reason to address this ticket. Absent a use case
where the lack of `__hash__()` causes a problem for usage of these
classes, I think it's fine to close this ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/26083#comment:6>

Reply all
Reply to author
Forward
0 new messages