brython-dev / brython

Brython (Browser Python) is an implementation of Python 3 running in the browser
BSD 3-Clause "New" or "Revised" License
6.4k stars 512 forks source link

Brython doesn't raise an exception for __hash__ methods that return non-int values #2452

Closed schmave closed 6 months ago

schmave commented 6 months ago
class C:
    def __init__(self, x):
        self.x = x

    def __eq__(self, other):
        return self.x == other.x

    def __hash__(self):
        return 'strings are not allowed'

c = C(5)
s = set()
print(C(5) in s)

When this code is run in Python 3.12, it raises an exception:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    print(C(5) in s)
          ^^^^^^^^^
TypeError: __hash__ method should return an integer

In Brython, the code runs without error.

Thanks for taking a look!

PierreQuentel commented 6 months ago

Thanks Evan !