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

Commitcdfb201

Browse files
authored
gh-105237: Allow callingissubclass(X, typing.Protocol) again (#105239)
1 parent69d1245 commitcdfb201

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

‎Lib/test/test_typing.py‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2758,6 +2758,65 @@ def x(self): ...
27582758
withself.assertRaisesRegex(TypeError,only_classes_allowed):
27592759
issubclass(1,BadPG)
27602760

2761+
deftest_issubclass_and_isinstance_on_Protocol_itself(self):
2762+
classC:
2763+
defx(self):pass
2764+
2765+
self.assertNotIsSubclass(object,Protocol)
2766+
self.assertNotIsInstance(object(),Protocol)
2767+
2768+
self.assertNotIsSubclass(str,Protocol)
2769+
self.assertNotIsInstance('foo',Protocol)
2770+
2771+
self.assertNotIsSubclass(C,Protocol)
2772+
self.assertNotIsInstance(C(),Protocol)
2773+
2774+
only_classes_allowed=r"issubclass\(\) arg 1 must be a class"
2775+
2776+
withself.assertRaisesRegex(TypeError,only_classes_allowed):
2777+
issubclass(1,Protocol)
2778+
withself.assertRaisesRegex(TypeError,only_classes_allowed):
2779+
issubclass('foo',Protocol)
2780+
withself.assertRaisesRegex(TypeError,only_classes_allowed):
2781+
issubclass(C(),Protocol)
2782+
2783+
T=TypeVar('T')
2784+
2785+
@runtime_checkable
2786+
classEmptyProtocol(Protocol):pass
2787+
2788+
@runtime_checkable
2789+
classSupportsStartsWith(Protocol):
2790+
defstartswith(self,x:str)->bool: ...
2791+
2792+
@runtime_checkable
2793+
classSupportsX(Protocol[T]):
2794+
defx(self): ...
2795+
2796+
forprotoinEmptyProtocol,SupportsStartsWith,SupportsX:
2797+
withself.subTest(proto=proto.__name__):
2798+
self.assertIsSubclass(proto,Protocol)
2799+
2800+
# gh-105237 / PR #105239:
2801+
# check that the presence of Protocol subclasses
2802+
# where `issubclass(X, <subclass>)` evaluates to True
2803+
# doesn't influence the result of `issubclass(X, Protocol)`
2804+
2805+
self.assertIsSubclass(object,EmptyProtocol)
2806+
self.assertIsInstance(object(),EmptyProtocol)
2807+
self.assertNotIsSubclass(object,Protocol)
2808+
self.assertNotIsInstance(object(),Protocol)
2809+
2810+
self.assertIsSubclass(str,SupportsStartsWith)
2811+
self.assertIsInstance('foo',SupportsStartsWith)
2812+
self.assertNotIsSubclass(str,Protocol)
2813+
self.assertNotIsInstance('foo',Protocol)
2814+
2815+
self.assertIsSubclass(C,SupportsX)
2816+
self.assertIsInstance(C(),SupportsX)
2817+
self.assertNotIsSubclass(C,Protocol)
2818+
self.assertNotIsInstance(C(),Protocol)
2819+
27612820
deftest_protocols_issubclass_non_callable(self):
27622821
classC:
27632822
x=1

‎Lib/typing.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,8 @@ def __init__(cls, *args, **kwargs):
17821782
)
17831783

17841784
def__subclasscheck__(cls,other):
1785+
ifclsisProtocol:
1786+
returntype.__subclasscheck__(cls,other)
17851787
ifnotisinstance(other,type):
17861788
# Same error message as for issubclass(1, int).
17871789
raiseTypeError('issubclass() arg 1 must be a class')
@@ -1803,6 +1805,8 @@ def __subclasscheck__(cls, other):
18031805
def__instancecheck__(cls,instance):
18041806
# We need this method for situations where attributes are
18051807
# assigned in __init__.
1808+
ifclsisProtocol:
1809+
returntype.__instancecheck__(cls,instance)
18061810
ifnotgetattr(cls,"_is_protocol",False):
18071811
# i.e., it's a concrete subclass of a protocol
18081812
returnsuper().__instancecheck__(instance)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix longstanding bug where ``issubclass(object, typing.Protocol)`` would
2+
evaluate to ``True`` in some edge cases. Patch by Alex Waygood.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp