Opened 8 years ago

Closed 7 years ago

#26083 closed Cleanup/optimization (wontfix)

Python 2 deprecation warnings for classes that define __eq__() but not __hash__()

Reported by: Mads Jensen Owned by: nobody
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ben Finney)

Many classes within Django define the __eq__ method, which in Python 3 will have disruptive implications for inheritance of __hash__. See the 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

Change History (6)

comment:1 by Tim Graham, 8 years ago

Description: modified (diff)

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

comment:2 by Mads Jensen, 8 years ago

/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):
Last edited 8 years ago by Tim Graham (previous) (diff)

comment:3 by Tim Graham, 8 years ago

Summary: __eq__, __hash__ and Python 3Python 2 deprecation warnings for classes that define __eq__() but not __hash__()
Triage Stage: UnreviewedAccepted

From what I read 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.

comment:4 by Ben Finney, 8 years ago

Description: modified (diff)

comment:5 by Ben Finney, 8 years ago

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

I have followed the instructions for 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
[…]
…/django/core/checks/messages.py:15: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class CheckMessage(object):
…/django/core/cache/__init__.py:90: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class DefaultCacheProxy(object):
…/django/db/__init__.py:29: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class DefaultConnectionProxy(object):
…/django/core/validators.py:31: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class RegexValidator(object):
…/django/core/validators.py:158: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class EmailValidator(object):
…/django/core/validators.py:296: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class BaseValidator(object):
…/django/core/validators.py:359: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class DecimalValidator(object):
…/django/db/models/sql/datastructures.py:28: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class Join(object):
…/django/template/context.py:32: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class BaseContext(object):
…/django/template/base.py:138: DeprecationWarning: Overriding __eq__ blocks inheritance of __hash__ in 3.x
  class Origin(object):
[…]
Last edited 8 years ago by Ben Finney (previous) (diff)

comment:6 by Tim Graham, 7 years ago

Resolution: wontfix
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top