diff --git a/mypyc/doc/list_operations.rst b/mypyc/doc/list_operations.rst index bb4681266cab..f18ee5c2bf0a 100644 --- a/mypyc/doc/list_operations.rst +++ b/mypyc/doc/list_operations.rst @@ -32,6 +32,7 @@ Operators * ``lst[n]`` (get item by integer index) * ``lst[n:m]``, ``lst[n:]``, ``lst[:m]``, ``lst[:]`` (slicing) +* ``lst1 == lst2``, ``lst1 != lst2`` * ``lst1 + lst2``, ``lst += iter`` * ``lst * n``, ``n * lst``, ``lst *= n`` * ``obj in lst`` diff --git a/mypyc/doc/tuple_operations.rst b/mypyc/doc/tuple_operations.rst index 32a610a79507..67b7db721365 100644 --- a/mypyc/doc/tuple_operations.rst +++ b/mypyc/doc/tuple_operations.rst @@ -21,6 +21,7 @@ Operators * ``tup[n]`` (integer index) * ``tup[n:m]``, ``tup[n:]``, ``tup[:m]`` (slicing) +* ``tup1 == tup2``, ``tup1 != tup2`` (variable-length tuples) * ``tup1 + tup2`` * ``tup * n``, ``n * tup`` diff --git a/mypyc/irbuild/ll_builder.py b/mypyc/irbuild/ll_builder.py index 5fa291a6ae8e..8c99d9fa3b97 100644 --- a/mypyc/irbuild/ll_builder.py +++ b/mypyc/irbuild/ll_builder.py @@ -187,7 +187,14 @@ uint64_overflow, uint64_to_int_op, ) -from mypyc.primitives.list_ops import list_build_op, list_extend_op, list_items, new_list_op +from mypyc.primitives.list_ops import ( + list_build_op, + list_equal_op, + list_extend_op, + list_items, + list_not_equal_op, + new_list_op, +) from mypyc.primitives.misc_ops import ( bool_op, buf_init_item, @@ -222,6 +229,8 @@ new_tuple_op, new_tuple_with_length_op, sequence_tuple_op, + tuple_equal_op, + tuple_not_equal_op, ) from mypyc.rt_expandtype import expand_rtype from mypyc.rt_subtype import is_runtime_subtype @@ -2020,6 +2029,27 @@ def compare_bytes(self, lhs: Value, rhs: Value, op: str, line: int) -> Value: op_type = ComparisonOp.EQ if op == "==" else ComparisonOp.NEQ return self.add(ComparisonOp(compare_result, Integer(1, c_int_rprimitive), op_type, line)) + def compare_sequences( + self, + equal_op: CFunctionDescription, + not_equal_op: CFunctionDescription, + lhs: Value, + rhs: Value, + op: str, + line: int, + ) -> Value: + """Compare two lists or two variable-length tuples for (in)equality. + + The ops return 1 if the comparison is true, 0 if it's false and -1 on error. + Use a separate op for != instead of negating ==, since a subclass may + override __ne__ (or inherit it while overriding only __eq__). + """ + compare_op = equal_op if op == "==" else not_equal_op + compare_result = self.call_c(compare_op, [lhs, rhs], line) + return self.add( + ComparisonOp(compare_result, Integer(1, c_int_rprimitive), ComparisonOp.EQ, line) + ) + def compare_tuples(self, lhs: Value, rhs: Value, op: str, line: int = -1) -> Value: """Compare two tuples item by item""" # type cast to pass mypy check @@ -3156,6 +3186,14 @@ def translate_eq_cmp(self, lreg: Value, rreg: Value, expr_op: str, line: int) -> return self.compare_strings(lreg, rreg, expr_op, line) if is_bytes_rprimitive(ltype) and is_bytes_rprimitive(rtype): return self.compare_bytes(lreg, rreg, expr_op, line) + if is_list_rprimitive(ltype) and is_list_rprimitive(rtype): + return self.compare_sequences( + list_equal_op, list_not_equal_op, lreg, rreg, expr_op, line + ) + if is_tuple_rprimitive(ltype) and is_tuple_rprimitive(rtype): + return self.compare_sequences( + tuple_equal_op, tuple_not_equal_op, lreg, rreg, expr_op, line + ) lopt = optional_value_type(ltype) ropt = optional_value_type(rtype) diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 5151bb7cd745..3b7057f2eb56 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1039,6 +1039,7 @@ CPyTagged CPyList_Count(PyObject *obj, PyObject *value); int CPyList_Insert(PyObject *list, CPyTagged index, PyObject *value); PyObject *CPyList_Extend(PyObject *o1, PyObject *o2); int CPyList_Remove(PyObject *list, PyObject *obj); +int CPyList_Compare(PyObject *left, PyObject *right, int op); CPyTagged CPyList_Index(PyObject *list, PyObject *obj); PyObject *CPySequence_Sort(PyObject *seq); PyObject *CPySequence_Concat(PyObject *seq, PyObject *other); @@ -1186,6 +1187,7 @@ bool CPySet_Remove(PyObject *set, PyObject *key); PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end); PyObject *CPySequenceTuple_GetItem_(PyObject *tuple, CPyTagged index); +int CPyTuple_Compare(PyObject *left, PyObject *right, int op); static inline PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index) { diff --git a/mypyc/lib-rt/list_ops.c b/mypyc/lib-rt/list_ops.c index 86caa34a3f5a..69205e811d70 100644 --- a/mypyc/lib-rt/list_ops.c +++ b/mypyc/lib-rt/list_ops.c @@ -468,6 +468,61 @@ CPyTagged CPyList_Index(PyObject *list, PyObject *obj) { return retval; } +// Compare two lists for equality. Return 1 if equal, 0 if not equal, -1 on error. +// +// The caller must hold a critical section on both lists. +static int _CPyList_Equal(PyObject *left, PyObject *right) { + if (Py_SIZE(left) != Py_SIZE(right)) { + return 0; + } + Py_ssize_t i; + for (i = 0; i < Py_SIZE(left) && i < Py_SIZE(right); i++) { + PyObject *left_item = PyList_GET_ITEM(left, i); + PyObject *right_item = PyList_GET_ITEM(right, i); + if (left_item == right_item) { + continue; + } + Py_INCREF(left_item); + Py_INCREF(right_item); + int cmp = PyObject_RichCompareBool(left_item, right_item, Py_EQ); + Py_DECREF(left_item); + Py_DECREF(right_item); + if (cmp <= 0) { + return cmp; + } + } + // An item comparison may have changed the lengths + return Py_SIZE(left) == Py_SIZE(right); +} + +// Compare two lists using == or != (op is Py_EQ or Py_NE). Return 1 if the result is +// true, 0 if it's false, and -1 on error. +int CPyList_Compare(PyObject *left, PyObject *right, int op) { + if (likely(PyList_CheckExact(left) && PyList_CheckExact(right))) { + int result; + if (left == right) { + // All items are identical, so they compare equal + result = 1; + } else { + Py_BEGIN_CRITICAL_SECTION2(left, right); + result = _CPyList_Equal(left, right); + Py_END_CRITICAL_SECTION2(); + if (result < 0) { + return -1; + } + } + return op == Py_EQ ? result : !result; + } + // Subclasses may override __eq__ and __ne__ + PyObject *res = PyObject_RichCompare(left, right, op); + if (res == NULL) { + return -1; + } + int result = PyObject_IsTrue(res); + Py_DECREF(res); + return result; +} + PyObject *CPySequence_Sort(PyObject *seq) { PyObject *newlist = PySequence_List(seq); if (newlist == NULL) diff --git a/mypyc/lib-rt/tuple_ops.c b/mypyc/lib-rt/tuple_ops.c index a65614ac201b..a544b49eadb6 100644 --- a/mypyc/lib-rt/tuple_ops.c +++ b/mypyc/lib-rt/tuple_ops.c @@ -89,3 +89,48 @@ PyObject *CPySequenceTuple_SetSize(PyObject *tuple, Py_ssize_t size) { } return CPyTuple_MoveItems(tuple, size, size); } + +// Compare two exact tuples for equality. Return 1 if equal, 0 if not equal, -1 on error. +static int _CPyTuple_Equal(PyObject *left, PyObject *right) { + if (left == right) { + // All items are identical, so they compare equal + return 1; + } + Py_ssize_t left_size = PyTuple_GET_SIZE(left); + Py_ssize_t right_size = PyTuple_GET_SIZE(right); + // Like CPython, compare items even if the lengths differ, since item + // comparisons can have side effects or raise exceptions + Py_ssize_t i; + for (i = 0; i < left_size && i < right_size; i++) { + PyObject *left_item = PyTuple_GET_ITEM(left, i); + PyObject *right_item = PyTuple_GET_ITEM(right, i); + if (left_item == right_item) { + continue; + } + int cmp = PyObject_RichCompareBool(left_item, right_item, Py_EQ); + if (cmp <= 0) { + return cmp; + } + } + return left_size == right_size; +} + +// Compare two tuples using == or != (op is Py_EQ or Py_NE). Return 1 if the result is +// true, 0 if it's false, and -1 on error. +int CPyTuple_Compare(PyObject *left, PyObject *right, int op) { + if (likely(PyTuple_CheckExact(left) && PyTuple_CheckExact(right))) { + int result = _CPyTuple_Equal(left, right); + if (result < 0) { + return -1; + } + return op == Py_EQ ? result : !result; + } + // Subclasses may override __eq__ and __ne__ + PyObject *res = PyObject_RichCompare(left, right, op); + if (res == NULL) { + return -1; + } + int result = PyObject_IsTrue(res); + Py_DECREF(res); + return result; +} diff --git a/mypyc/primitives/list_ops.py b/mypyc/primitives/list_ops.py index 32d655a69816..e6e4728b7b8c 100644 --- a/mypyc/primitives/list_ops.py +++ b/mypyc/primitives/list_ops.py @@ -309,6 +309,24 @@ error_kind=ERR_NEG_INT, ) +# list == list (return -1/0/1) +list_equal_op = custom_op( + arg_types=[list_rprimitive, list_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyList_Compare", + error_kind=ERR_NEG_INT, + extra_int_constants=[(2, c_int_rprimitive)], # Py_EQ +) + +# list != list (return -1/0/1) +list_not_equal_op = custom_op( + arg_types=[list_rprimitive, list_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyList_Compare", + error_kind=ERR_NEG_INT, + extra_int_constants=[(3, c_int_rprimitive)], # Py_NE +) + # list.remove(obj) method_op( name="remove", diff --git a/mypyc/primitives/tuple_ops.py b/mypyc/primitives/tuple_ops.py index 59183c61bdf0..2fa615a4b4d8 100644 --- a/mypyc/primitives/tuple_ops.py +++ b/mypyc/primitives/tuple_ops.py @@ -9,6 +9,7 @@ from mypyc.ir.ops import ERR_MAGIC, ERR_NEVER from mypyc.ir.rtypes import ( bit_rprimitive, + c_int_rprimitive, c_pyssize_t_rprimitive, c_size_t_rprimitive, int_rprimitive, @@ -17,7 +18,14 @@ tuple_rprimitive, void_rtype, ) -from mypyc.primitives.registry import binary_op, custom_op, function_op, load_address_op, method_op +from mypyc.primitives.registry import ( + ERR_NEG_INT, + binary_op, + custom_op, + function_op, + load_address_op, + method_op, +) # Get the 'builtins.tuple' type object. load_address_op(name="builtins.tuple", type=object_rprimitive, src="PyTuple_Type") @@ -122,6 +130,24 @@ error_kind=ERR_NEVER, ) +# tuple == tuple (return -1/0/1) +tuple_equal_op = custom_op( + arg_types=[tuple_rprimitive, tuple_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyTuple_Compare", + error_kind=ERR_NEG_INT, + extra_int_constants=[(2, c_int_rprimitive)], # Py_EQ +) + +# tuple != tuple (return -1/0/1) +tuple_not_equal_op = custom_op( + arg_types=[tuple_rprimitive, tuple_rprimitive], + return_type=c_int_rprimitive, + c_function_name="CPyTuple_Compare", + error_kind=ERR_NEG_INT, + extra_int_constants=[(3, c_int_rprimitive)], # Py_NE +) + # tuple + tuple binary_op( name="+", diff --git a/mypyc/test-data/irbuild-lists.test b/mypyc/test-data/irbuild-lists.test index 47e11f4f544b..b8ecf597c872 100644 --- a/mypyc/test-data/irbuild-lists.test +++ b/mypyc/test-data/irbuild-lists.test @@ -361,6 +361,32 @@ L0: r3 = truncate r1: i32 to builtins.bool return r3 +[case testListEquality] +from typing import List +def eq(x: List[int], y: List[int]) -> bool: + return x == y +def neq(x: List[int], y: List[str]) -> bool: + return x != y +[out] +def eq(x, y): + x, y :: list + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyList_Compare(x, y, 2) + r1 = r0 >= 0 :: signed + r2 = r0 == 1 + return r2 +def neq(x, y): + x, y :: list + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyList_Compare(x, y, 3) + r1 = r0 >= 0 :: signed + r2 = r0 == 1 + return r2 + [case testListInsert] from typing import List def f(x: List[int], y: int) -> None: diff --git a/mypyc/test-data/irbuild-tuple.test b/mypyc/test-data/irbuild-tuple.test index 1adbcd2fed74..4cb7444c6e2b 100644 --- a/mypyc/test-data/irbuild-tuple.test +++ b/mypyc/test-data/irbuild-tuple.test @@ -1158,6 +1158,32 @@ L0: b = r0 return 1 +[case testVariableLengthTupleEquality] +from typing import Tuple +def eq(x: Tuple[int, ...], y: Tuple[int, ...]) -> bool: + return x == y +def neq(x: Tuple[int, ...], y: Tuple[str, ...]) -> bool: + return x != y +[out] +def eq(x, y): + x, y :: tuple + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyTuple_Compare(x, y, 2) + r1 = r0 >= 0 :: signed + r2 = r0 == 1 + return r2 +def neq(x, y): + x, y :: tuple + r0 :: i32 + r1, r2 :: bit +L0: + r0 = CPyTuple_Compare(x, y, 3) + r1 = r0 >= 0 :: signed + r2 = r0 == 1 + return r2 + [case testTupleFloatElementComparison] def f(x: tuple[float], y: tuple[float]) -> bool: return x == y diff --git a/mypyc/test-data/run-lists.test b/mypyc/test-data/run-lists.test index 4c025c652ed6..a4dc37ab0906 100644 --- a/mypyc/test-data/run-lists.test +++ b/mypyc/test-data/run-lists.test @@ -985,3 +985,159 @@ class GoodList(list): return GoodList(list(self) * n + [4]) def copy(self) -> Any: return GoodList(list(self) + [5]) + +[case testListEquality] +from typing import Any, List +from testutil import assertRaises +from leqsubclass import ListSubclass, ListEqOverride, CaseInsensitiveList, ListNeOverride + +def eq(x: List[Any], y: List[Any]) -> bool: + return x == y + +def ne(x: List[Any], y: List[Any]) -> bool: + return x != y + +class AlwaysEqual: + def __eq__(self, other: object) -> bool: + return True + +class NeverEqual: + def __eq__(self, other: object) -> bool: + return False + +class RaisesOnEq: + def __eq__(self, other: object) -> bool: + raise ValueError("eq") + +class FalsyEq: + def __eq__(self, other: object) -> Any: + return 0 + +class TruthyEq: + def __eq__(self, other: object) -> Any: + return "yes" + +def test_basic() -> None: + nested = [[1, 2], [3]] + values: List[List[Any]] = [ + [], + [1], + [1, 2], + [1, 2, 3], + [2, 1], + [1, 2, 4], + ["a", "b"], + [1.0, 2.0], + [True, 2], + [[1, 2], [3]], + nested, + [[1, 2], [4]], + [None], + [(1, 2)], + [(1, 2), (3,)], + ] + for x in values: + for y in values: + a: Any = x + b: Any = y + assert eq(x, y) == (a == b), (x, y) + assert ne(x, y) == (a != b), (x, y) + assert eq(x, list(y)) == (a == b), (x, y) + assert ne(x, list(y)) == (a != b), (x, y) + +def test_identity() -> None: + nan: Any = float("nan") + x = [nan] + assert eq(x, x) + assert not ne(x, x) + assert eq(x, [nan]) + assert not eq(x, [float("nan")]) + assert ne(x, [float("nan")]) + y = [NeverEqual()] + assert eq(y, y) + assert not eq(y, [NeverEqual()]) + +def test_custom_eq() -> None: + assert eq([AlwaysEqual()], [1]) + assert eq([1], [AlwaysEqual()]) + assert not eq([AlwaysEqual()], [1, 2]) + assert ne([NeverEqual()], [NeverEqual()]) + assert not eq([FalsyEq()], [1]) + assert ne([FalsyEq()], [1]) + assert eq([TruthyEq()], [1]) + assert not ne([TruthyEq()], [1]) + +def test_error() -> None: + with assertRaises(ValueError, "eq"): + eq([RaisesOnEq()], [1]) + with assertRaises(ValueError, "eq"): + ne([1, RaisesOnEq()], [1, 2]) + # Lengths differ, so items aren't compared + assert not eq([RaisesOnEq()], [1, 2]) + # Items are compared in order, and the first mismatch stops the comparison + assert not eq([1, RaisesOnEq()], [2, 3]) + +class ClearOnEq: + def __init__(self, target: List[Any]) -> None: + self.target = target + + def __eq__(self, other: object) -> bool: + self.target.clear() + return True + +def test_mutation_during_comparison() -> None: + a: List[Any] = [] + a.extend([ClearOnEq(a), 1, 2]) + assert not eq(a, [0, 1, 2]) + assert a == [] + a.append(ClearOnEq(a)) + assert not eq(a, [0]) + b: List[Any] = [0, 1] + b.append(ClearOnEq(b)) + assert not eq([0, 1, 2], b) + assert b == [] + +def test_subclass() -> None: + assert eq(ListSubclass([1, 2]), [1, 2]) + assert eq([1, 2], ListSubclass([1, 2])) + assert ne(ListSubclass([1, 2]), [1]) + assert not eq(ListEqOverride([1]), [1]) + assert not eq([1], ListEqOverride([1])) + assert ne(ListEqOverride([1]), [1]) + assert ne([1], ListEqOverride([1])) + x = ListEqOverride([]) + assert not eq(x, x) + assert ne(x, x) + # Only __eq__ is overridden, so != uses list.__ne__ + ci = CaseInsensitiveList(["A"]) + assert eq(ci, ["a"]) + assert ne(ci, ["a"]) + assert eq(["a"], ci) + assert ne(["a"], ci) + assert not ne(ci, ["A"]) + y = ListNeOverride([1]) + assert eq(y, [1]) + assert ne(y, [1]) + assert ne([1], y) + +[file leqsubclass.py] +from typing import Any + +class ListSubclass(list[Any]): + pass + +class ListEqOverride(list[Any]): + def __eq__(self, other: object) -> bool: + return False + + def __ne__(self, other: object) -> bool: + return True + +class CaseInsensitiveList(list[str]): + def __eq__(self, other: object) -> bool: + assert isinstance(other, list) + return [s.lower() for s in self] == [s.lower() for s in other] + +class ListNeOverride(list[Any]): + def __ne__(self, other: object) -> bool: + return True diff --git a/mypyc/test-data/run-tuples.test b/mypyc/test-data/run-tuples.test index d9bcd577abe6..d31b67eb62ba 100644 --- a/mypyc/test-data/run-tuples.test +++ b/mypyc/test-data/run-tuples.test @@ -586,3 +586,168 @@ def test_unhashable_item() -> None: hash((a, 1)) with assertRaises(TypeError, "unhashable type: 'list'"): hash((1, (2, a))) + +[case testVariableLengthTupleEquality] +from typing import Any, Tuple +from testutil import assertRaises +from teqsubclass import ( + TupleSubclass, TupleEqOverride, TupleEqOnlyOverride, TupleNeOverride, NT +) + +def eq(x: Tuple[Any, ...], y: Tuple[Any, ...]) -> bool: + return x == y + +def ne(x: Tuple[Any, ...], y: Tuple[Any, ...]) -> bool: + return x != y + +class AlwaysEqual: + def __eq__(self, other: object) -> bool: + return True + +class NeverEqual: + def __eq__(self, other: object) -> bool: + return False + +class RaisesOnEq: + def __eq__(self, other: object) -> bool: + raise ValueError("eq") + +class FalsyEq: + def __eq__(self, other: object) -> Any: + return 0 + +class TruthyEq: + def __eq__(self, other: object) -> Any: + return "yes" + +def test_basic() -> None: + nested = ((1, 2), (3,)) + values: list[Tuple[Any, ...]] = [ + (), + (1,), + (1, 2), + (1, 2, 3), + (2, 1), + (1, 2, 4), + ("a", "b"), + (1.0, 2.0), + (True, 2), + ((1, 2), (3,)), + nested, + ((1, 2), (4,)), + (None,), + ([1, 2],), + ([1, 2], [3]), + ] + for x in values: + for y in values: + a: Any = x + b: Any = y + assert eq(x, y) == (a == b), (x, y) + assert ne(x, y) == (a != b), (x, y) + assert eq(x, tuple(list(y))) == (a == b), (x, y) + assert ne(x, tuple(list(y))) == (a != b), (x, y) + +def test_identity() -> None: + nan: Any = float("nan") + x = (nan,) + assert eq(x, x) + assert not ne(x, x) + assert eq(x, (nan,)) + assert not eq(x, (float("nan"),)) + assert ne(x, (float("nan"),)) + y = (NeverEqual(),) + assert eq(y, y) + assert not eq(y, (NeverEqual(),)) + +def test_custom_eq() -> None: + assert eq((AlwaysEqual(),), (1,)) + assert eq((1,), (AlwaysEqual(),)) + assert not eq((AlwaysEqual(),), (1, 2)) + assert ne((NeverEqual(),), (NeverEqual(),)) + assert not eq((FalsyEq(),), (1,)) + assert ne((FalsyEq(),), (1,)) + assert eq((TruthyEq(),), (1,)) + assert not ne((TruthyEq(),), (1,)) + +def test_error() -> None: + with assertRaises(ValueError, "eq"): + eq((RaisesOnEq(),), (1,)) + with assertRaises(ValueError, "eq"): + ne((1, RaisesOnEq()), (1, 2)) + # Items are compared even if the lengths differ + with assertRaises(ValueError, "eq"): + eq((RaisesOnEq(),), (1, 2)) + with assertRaises(ValueError, "eq"): + ne((1, 2), (RaisesOnEq(),)) + # Items are compared in order, and the first mismatch stops the comparison + assert not eq((1, RaisesOnEq()), (2, 3)) + assert not eq((1, RaisesOnEq()), (2,)) + +class CountingEq: + def __init__(self) -> None: + self.count = 0 + + def __eq__(self, other: object) -> bool: + self.count += 1 + return True + +def test_different_lengths() -> None: + c = CountingEq() + assert not eq((c,), (1, 2)) + assert ne((1, 2, 3), (c,)) + assert c.count == 2 + a: Any = (c,) + b: Any = (1, 2) + assert a != b + assert c.count == 3 + +def test_subclass() -> None: + assert eq(TupleSubclass((1, 2)), (1, 2)) + assert eq((1, 2), TupleSubclass((1, 2))) + assert ne(TupleSubclass((1, 2)), (1,)) + assert eq(NT(1, 2), (1, 2)) + assert ne(NT(1, 2), (2, 1)) + assert not eq(TupleEqOverride((1,)), (1,)) + assert not eq((1,), TupleEqOverride((1,))) + assert ne(TupleEqOverride((1,)), (1,)) + assert ne((1,), TupleEqOverride((1,))) + x = TupleEqOverride(()) + assert not eq(x, x) + assert ne(x, x) + # Only __eq__ is overridden, so != uses tuple.__ne__ + y = TupleEqOnlyOverride((1,)) + assert eq(y, (2,)) + assert ne(y, (2,)) + assert eq((2,), y) + assert ne((2,), y) + assert not ne(y, (1,)) + z = TupleNeOverride((1,)) + assert eq(z, (1,)) + assert ne(z, (1,)) + assert ne((1,), z) + +[file teqsubclass.py] +from typing import Any, NamedTuple + +class TupleSubclass(tuple[Any, ...]): + pass + +class TupleEqOverride(tuple[Any, ...]): + def __eq__(self, other: object) -> bool: + return False + + def __ne__(self, other: object) -> bool: + return True + +class TupleEqOnlyOverride(tuple[Any, ...]): + def __eq__(self, other: object) -> bool: + return True + +class TupleNeOverride(tuple[Any, ...]): + def __ne__(self, other: object) -> bool: + return True + +class NT(NamedTuple): + x: int + y: int