From 6bc470c3741e78566621bda4a758412a7db7111f Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Mon, 3 Jun 2024 04:14:46 -0700 Subject: [PATCH] gh-119972: typing: Fix specialization of generic with broken __eq__ --- Lib/test/test_typing.py | 12 ++++++++++++ Lib/typing.py | 2 +- .../2024-06-03-04-14-14.gh-issue-119972.nwtxx6.rst | 2 ++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-06-03-04-14-14.gh-issue-119972.nwtxx6.rst diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 4cf8d498fcc866..7273560d91faa7 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -4866,6 +4866,18 @@ class B(Generic[T]): self.assertEqual(A[T], A[T]) self.assertNotEqual(A[T], B[T]) + def test_buggy_eq(self): + class BrokenEq(abc.ABCMeta): + def __eq__(self, other): + raise TypeError("I'm broken") + + class G(Generic[T], metaclass=BrokenEq): + pass + + alias = G[int] + self.assertIs(get_origin(alias), G) + self.assertEqual(get_args(alias), (int,)) + def test_multiple_inheritance(self): class A(Generic[T, VT]): diff --git a/Lib/typing.py b/Lib/typing.py index 882dc4da58e914..0cce23b2236b36 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1077,7 +1077,7 @@ def _generic_class_getitem(cls, params): params = (params,) params = tuple(_type_convert(p) for p in params) - is_generic_or_protocol = cls in (Generic, Protocol) + is_generic_or_protocol = cls is Generic or cls is Protocol if is_generic_or_protocol: # Generic and Protocol can only be subscripted with unique type variables. diff --git a/Misc/NEWS.d/next/Library/2024-06-03-04-14-14.gh-issue-119972.nwtxx6.rst b/Misc/NEWS.d/next/Library/2024-06-03-04-14-14.gh-issue-119972.nwtxx6.rst new file mode 100644 index 00000000000000..30798ca525146a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-06-03-04-14-14.gh-issue-119972.nwtxx6.rst @@ -0,0 +1,2 @@ +Fix specialization of :class:`typing.Generic` class with a broken ``__eq__`` +method. Patch by Jelle Zijlstra.