Opened 8 years ago

Last modified 7 years ago

#26083 closed Cleanup/optimization

Python 2 deprecation warnings for classes that define __eq__() but not __hash__() — at Version 4

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 (4)

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)
Note: See TracTickets for help on using tickets.
Back to Top