Skip to content

Commit 4e7a12c

Browse files
gh-94107: Make _pydecimal.Context hashable to match _decimal.Context
1 parent b954645 commit 4e7a12c

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

‎Lib/_pydecimal.py‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4060,9 +4060,6 @@ def _regard_flags(self, *flags):
40604060
for flag in flags:
40614061
self._ignored_flags.remove(flag)
40624062

4063-
# We inherit object.__hash__, so we must deny this explicitly
4064-
__hash__ = None
4065-
40664063
def Etiny(self):
40674064
"""Returns Etiny (= Emin - prec + 1)"""
40684065
return int(self.Emin - self.prec + 1)

‎Lib/test/test_decimal.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,6 +3658,18 @@ def test_same_quantum(self):
36583658
self.assertRaises(TypeError, c.same_quantum, '1', 2)
36593659
self.assertRaises(TypeError, c.same_quantum, 1, '2')
36603660

3661+
def test_context_hash(self):
3662+
Context = self.decimal.Context
3663+
c1 = Context()
3664+
c2 = Context()
3665+
self.assertIsInstance(hash(c1), int)
3666+
self.assertIsInstance(hash(c2), int)
3667+
self.assertEqual(hash(c1), c1.__hash__())
3668+
self.assertNotEqual(hash(c1), hash(c2))
3669+
d = {c1: 'first', c2: 'second'}
3670+
self.assertEqual(d[c1], 'first')
3671+
self.assertEqual(d[c2], 'second')
3672+
36613673
def test_scaleb(self):
36623674
Decimal = self.decimal.Decimal
36633675
Context = self.decimal.Context
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :class:`!_pydecimal.Context` hashable by inheriting :meth:`object.__hash__`, matching the behavior of :class:`!_decimal.Context` per :pep:`399`.

0 commit comments

Comments
 (0)