diff --git a/mypyc/codegen/emitclass.py b/mypyc/codegen/emitclass.py index e037460b01677..f528de5f5bfff 100644 --- a/mypyc/codegen/emitclass.py +++ b/mypyc/codegen/emitclass.py @@ -387,6 +387,10 @@ def emit_line() -> None: richcompare_name = generate_richcompare_wrapper(cl, emitter) if richcompare_name: fields["tp_richcompare"] = richcompare_name + if "tp_hash" not in fields and not cl.has_method("__eq__") and not cl.inherits_python: + # CPython doesn't inherit tp_hash if tp_richcompare is defined, but only + # defining __eq__ should make the class unhashable (not __lt__, etc.) + fields["tp_hash"] = "CPy_DefaultHash" # If the class inherits from python, make space for a __dict__ struct_name = cl.struct_name(emitter.names) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index dfb7e43ed769b..424db8faa454d 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -780,6 +780,11 @@ static void CPy_LogGetAttr(const char *method, PyObject *obj, PyObject *attr) { PyObject_GetAttr((obj), (attr))) CPyTagged CPyObject_Hash(PyObject *o); + +// Default identity-based hash (same as object.__hash__), used as tp_hash +static inline Py_hash_t CPy_DefaultHash(PyObject *o) { + return PyBaseObject_Type.tp_hash(o); +} PyObject *CPyObject_GetAttr3(PyObject *v, PyObject *name, PyObject *defl); PyObject *CPyIter_Next(PyObject *iter); PyObject *CPyNumber_Power(PyObject *base, PyObject *index); diff --git a/mypyc/test-data/run-dunders.test b/mypyc/test-data/run-dunders.test index cf8c23345625d..ad9cd46bfd6c1 100644 --- a/mypyc/test-data/run-dunders.test +++ b/mypyc/test-data/run-dunders.test @@ -1403,7 +1403,9 @@ def test_final() -> None: assert (a < A(5)) is False assert (b < A(5)) is True -[case testDundersEq] +[case testDundersEqAndHash] +from testutil import assertRaises + class Eq: def __init__(self, x: int) -> None: self.x = x @@ -1425,6 +1427,56 @@ def test_equality_with_implicit_ne() -> None: assert ne(Eq(1), Eq(2)) assert not ne(Eq(1), Eq(1)) +class Lt: + def __init__(self, n: int) -> None: + self.n = n + + def __lt__(self, other: "Lt") -> bool: + return self.n < other.n + +class LtSub(Lt): + def __gt__(self, other: "Lt") -> bool: + return self.n > other.n + +class EqOnly: + def __eq__(self, other: object) -> bool: + return True + +class EqOnlySub(EqOnly): + def __lt__(self, other: "EqOnly") -> bool: + return True + +class EqHash: + def __eq__(self, other: object) -> bool: + return True + + def __hash__(self) -> int: + return 5 + +class EqHashSub(EqHash): + def __lt__(self, other: "EqHash") -> bool: + return True + +def test_ordering_comparisons_keep_class_hashable() -> None: + # Only defining __eq__ (without __hash__) makes a class unhashable + x = Lt(1) + assert hash(x) == hash(x) + assert x in {x} + y = LtSub(1) + assert y in {y} + assert Lt(1) < Lt(2) + assert LtSub(2) > Lt(1) + +def test_eq_makes_class_unhashable() -> None: + with assertRaises(TypeError, "unhashable type"): + hash(EqOnly()) + with assertRaises(TypeError, "unhashable type"): + hash(EqOnlySub()) + +def test_explicit_hash() -> None: + assert hash(EqHash()) == 5 + assert hash(EqHashSub()) == 5 + [case testDundersImplicitNeWithNarrowRhs] class Accepted: pass