MCPcopy Index your code
hub / github.com/python/cpython / test_hash_field_rules

Method test_hash_field_rules

Lib/test/test_dataclasses/__init__.py:610–634  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

608 a: Any = unhashable
609
610 def test_hash_field_rules(self):
611 # Test all 6 cases of:
612 # hash=True/False/None
613 # compare=True/False
614 for (hash_, compare, result ) in [
615 (True, False, 'field' ),
616 (True, True, 'field' ),
617 (False, False, 'absent'),
618 (False, True, 'absent'),
619 (None, False, 'absent'),
620 (None, True, 'field' ),
621 ]:
622 with self.subTest(hash=hash_, compare=compare):
623 @dataclass(unsafe_hash=True)
624 class C:
625 x: int = field(compare=compare, hash=hash_, default=5)
626
627 if result == 'field':
628 # __hash__ contains the field.
629 self.assertEqual(hash(C(5)), hash((5,)))
630 elif result == 'absent':
631 # The field is not present in the hash.
632 self.assertEqual(hash(C(5)), hash(()))
633 else:
634 assert False, f'unknown result {result!r}'
635
636 def test_init_false_no_default(self):
637 # If init=False and no default value, then the field won't be

Callers

nothing calls this directly

Calls 3

subTestMethod · 0.95
assertEqualMethod · 0.95
CClass · 0.70

Tested by

no test coverage detected