From 4e7a12c089fadb48ea8d598406e38411f46c39c7 Mon Sep 17 00:00:00 2001 From: nirajan khatiwada Date: Fri, 25 Sep 2026 15:30:33 +0545 Subject: [PATCH] gh-94107: Make _pydecimal.Context hashable to match _decimal.Context --- Lib/_pydecimal.py | 3 --- Lib/test/test_decimal.py | 12 ++++++++++++ .../2026-09-25-15-30-00.gh-issue-94107.cT4xW9.rst | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-09-25-15-30-00.gh-issue-94107.cT4xW9.rst diff --git a/Lib/_pydecimal.py b/Lib/_pydecimal.py index 57d6143daea43a..8f7f3faba5ef62 100644 --- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -4060,9 +4060,6 @@ def _regard_flags(self, *flags): for flag in flags: self._ignored_flags.remove(flag) - # We inherit object.__hash__, so we must deny this explicitly - __hash__ = None - def Etiny(self): """Returns Etiny (= Emin - prec + 1)""" return int(self.Emin - self.prec + 1) diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 7524822632bae7..e1739f317d0a58 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -3658,6 +3658,18 @@ def test_same_quantum(self): self.assertRaises(TypeError, c.same_quantum, '1', 2) self.assertRaises(TypeError, c.same_quantum, 1, '2') + def test_context_hash(self): + Context = self.decimal.Context + c1 = Context() + c2 = Context() + self.assertIsInstance(hash(c1), int) + self.assertIsInstance(hash(c2), int) + self.assertEqual(hash(c1), c1.__hash__()) + self.assertNotEqual(hash(c1), hash(c2)) + d = {c1: 'first', c2: 'second'} + self.assertEqual(d[c1], 'first') + self.assertEqual(d[c2], 'second') + def test_scaleb(self): Decimal = self.decimal.Decimal Context = self.decimal.Context diff --git a/Misc/NEWS.d/next/Library/2026-09-25-15-30-00.gh-issue-94107.cT4xW9.rst b/Misc/NEWS.d/next/Library/2026-09-25-15-30-00.gh-issue-94107.cT4xW9.rst new file mode 100644 index 00000000000000..5eadc16700deba --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-25-15-30-00.gh-issue-94107.cT4xW9.rst @@ -0,0 +1 @@ +Make :class:`!_pydecimal.Context` hashable by inheriting :meth:`object.__hash__`, matching the behavior of :class:`!_decimal.Context` per :pep:`399`.