andrew cooke wrote:
I was going to file a bug report for this, but then I wondered if it was
considered normal behaviour.  Am I wrong in thinking there should be a
better error message?

class NoHash:
...   def __hash__(self):
...     pass
...
frozenset([NoHash()])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required

I understand that there is a real problem here (in my code I had forgotten
the "return"); the issue is whether the stack trace should contain some
reference to __hash__ or similar.  Perhaps that is impossible for
containers implemented in C?

The error message comes from the internal call of hash().

>>> class C():
        def __hash__(s): return 'a'
>>> hash(C())
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    hash(C())
TypeError: an integer is required

This could be improved to
TypeError: hash must be an integer # or even
TypeError: hash must be integer, not <returned object.__class__>
(but someone would have to write a patch and most devs have other priorities.)

The same is true for unhashable types

>>> class C():
        __hash__ = None

>>> hash(C())
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    hash(C())
TypeError: unhashable type: 'C'

>>> set([[]])
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    set([[]])
TypeError: unhashable type: 'list'

Terry Jan Reedy


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to