Skip to content

Commit

Permalink
gh-116647: Fix recursive child in dataclasses (#116790)
Browse files Browse the repository at this point in the history
  • Loading branch information
et-repositories committed Mar 19, 2024
1 parent 3cac2af commit 7593574
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Lib/dataclasses.py
Expand Up @@ -1075,7 +1075,9 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
cmp_fields = (field for field in field_list if field.compare)
terms = [f'self.{field.name}==other.{field.name}' for field in cmp_fields]
field_comparisons = ' and '.join(terms) or 'True'
body = [f'if other.__class__ is self.__class__:',
body = [f'if self is other:',
f' return True',
f'if other.__class__ is self.__class__:',
f' return {field_comparisons}',
f'return NotImplemented']
func = _create_fn('__eq__',
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_dataclasses/__init__.py
Expand Up @@ -2471,6 +2471,15 @@ def __repr__(self):


class TestEq(unittest.TestCase):
def test_recursive_eq(self):
# Test a class with recursive child
@dataclass
class C:
recursive: object = ...
c = C()
c.recursive = c
self.assertEqual(c, c)

def test_no_eq(self):
# Test a class with no __eq__ and eq=False.
@dataclass(eq=False)
Expand Down
@@ -0,0 +1 @@
Fix recursive child in dataclasses

0 comments on commit 7593574

Please sign in to comment.