Skip to content

Commit ada26a9

Browse files
committed
Update dataclass documentation for new __eq__ behavior in 3.13 and add tests
1 parent 262f785 commit ada26a9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Doc/library/dataclasses.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,16 @@ Module contents
106106
generated.
107107

108108
.. versionchanged:: 3.13
109-
The generated ``__eq__`` method now compares each field individually (e.g., ``self.a == other.a and self.b == other.b``), rather than comparing tuples of fields as in previous versions.
109+
The generated ``__eq__`` method now compares each field individually
110+
(for example, ``self.a == other.a and self.b == other.b``), rather than
111+
comparing tuples of fields as in previous versions.
110112

111-
This method compares the class by comparing each field in order. Both instances in the comparison must
112-
be of the identical type.
113+
This method compares the class by comparing each field in order. Both
114+
instances in the comparison must be of the identical type.
113115

114-
In Python 3.12 and earlier, the comparison was performed by creating tuples of the fields and comparing them (e.g., ``(self.a, self.b) == (other.a, other.b)``).
116+
In Python 3.12 and earlier, the comparison was performed by creating
117+
tuples of the fields and comparing them (for example,
118+
``(self.a, self.b) == (other.a, other.b)``).
115119

116120
If the class already defines :meth:`!__eq__`, this parameter is
117121
ignored.

Lib/test/test_dataclasses/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,16 @@ class Foo:
26362636
f2 = Foo(AlwaysEqual(2), 2)
26372637
self.assertTrue(f1 == f2)
26382638

2639+
def test_eq_nan_field(self):
2640+
@dataclasses.dataclass
2641+
class D:
2642+
x: float
2643+
2644+
nan = float('nan')
2645+
d1 = D(nan)
2646+
d2 = D(nan)
2647+
self.assertFalse(d1 == d2)
2648+
26392649

26402650
class TestOrdering(unittest.TestCase):
26412651
def test_functools_total_ordering(self):

0 commit comments

Comments
 (0)