From a36291d5b9a8e5aa465a9ac936bfb6de1508664e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 12 May 2025 06:49:13 -0700 Subject: [PATCH 1/2] gh-133925: Make typing._UnionGenericAlias hashable --- Lib/test/test_typing.py | 3 +++ Lib/typing.py | 3 +++ .../Library/2025-05-12-06-52-10.gh-issue-133925.elInBY.rst | 1 + 3 files changed, 7 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-05-12-06-52-10.gh-issue-133925.elInBY.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index bc03e6bf2d7962..89c7d261090944 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -10668,6 +10668,9 @@ def test_eq(self): with self.assertWarns(DeprecationWarning): self.assertNotEqual(int, typing._UnionGenericAlias) + def test_hashable(self): + self.assertIsInstance(hash(typing._UnionGenericAlias), int) + def load_tests(loader, tests, pattern): import doctest diff --git a/Lib/typing.py b/Lib/typing.py index 44f39e9672f46b..7192bfff8ad35f 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1649,6 +1649,9 @@ def __eq__(self, other): return True return NotImplemented + def __hash__(self): + return super().__hash__() + class _UnionGenericAlias(metaclass=_UnionGenericAliasMeta): """Compatibility hack. diff --git a/Misc/NEWS.d/next/Library/2025-05-12-06-52-10.gh-issue-133925.elInBY.rst b/Misc/NEWS.d/next/Library/2025-05-12-06-52-10.gh-issue-133925.elInBY.rst new file mode 100644 index 00000000000000..328e28abc3b42b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-12-06-52-10.gh-issue-133925.elInBY.rst @@ -0,0 +1 @@ +Make the private class ``typing._UnionGenericAlias`` hashable. From 2cb54766e93a1df73d427e297bd8e336973c416f Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 12 May 2025 07:32:18 -0700 Subject: [PATCH 2/2] hash should equal that of Union --- Lib/test/test_typing.py | 2 +- Lib/typing.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 89c7d261090944..6ef633e4545aef 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -10669,7 +10669,7 @@ def test_eq(self): self.assertNotEqual(int, typing._UnionGenericAlias) def test_hashable(self): - self.assertIsInstance(hash(typing._UnionGenericAlias), int) + self.assertEqual(hash(typing._UnionGenericAlias), hash(Union)) def load_tests(loader, tests, pattern): diff --git a/Lib/typing.py b/Lib/typing.py index 7192bfff8ad35f..3d64480e1431c1 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1650,7 +1650,7 @@ def __eq__(self, other): return NotImplemented def __hash__(self): - return super().__hash__() + return hash(Union) class _UnionGenericAlias(metaclass=_UnionGenericAliasMeta):