Skip to content

Commit

Permalink
pythongh-119972: typing: Fix specialization of generic with broken __…
Browse files Browse the repository at this point in the history
…eq__
  • Loading branch information
JelleZijlstra committed Jun 3, 2024
1 parent 059be67 commit 6bc470c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix specialization of :class:`typing.Generic` class with a broken ``__eq__``
method. Patch by Jelle Zijlstra.

0 comments on commit 6bc470c

Please sign in to comment.