Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypyc/codegen/emitclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions mypyc/lib-rt/CPy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 53 additions & 1 deletion mypyc/test-data/run-dunders.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading