Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-119972: typing: Fix specialization of generic with broken __eq__#119973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
JelleZijlstra wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromJelleZijlstra:brokenq
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
gh-119972: typing: Fix specialization of generic with broken __eq__
  • Loading branch information
@JelleZijlstra
JelleZijlstra committedJun 3, 2024
commitb805800ffb458731c203df7f01c271ef9a9468f9
12 changes: 12 additions & 0 deletionsLib/test/test_typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5191,6 +5191,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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Doesn't need to beABCMeta to trigger this; justtype is sufficient:

Suggested change
classBrokenEq(abc.ABCMeta):
classBrokenEq(type):

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
4 changes: 2 additions & 2 deletionsLib/typing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1209,7 +1209,7 @@ def _generic_class_getitem(cls, args):
args = (args,)

args = tuple(_type_convert(p) for p in args)
is_generic_or_protocol = clsin (Generic,Protocol)
is_generic_or_protocol = clsisGeneric or cls isProtocol

if is_generic_or_protocol:
# Generic and Protocol can only be subscripted with unique type variables.
Expand DownExpand Up@@ -1416,7 +1416,7 @@ def __init__(self, origin, args, *, inst=True, name=None):
args = (args,)
self.__args__ = tuple(... if a is _TypingEllipsis else
a for a in args)
enforce_default_ordering = originin (Generic,Protocol)
enforce_default_ordering = originisGeneric or origin isProtocol
self.__parameters__ = _collect_type_parameters(
args,
enforce_default_ordering=enforce_default_ordering,
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Fix specialization of :class:`typing.Generic` class with a broken ``__eq__``
method. Patch by Jelle Zijlstra.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp