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

Commita2d94a0

Browse files
bpo-38908: Fix issue when non runtime_protocol failed to raise TypeError (GH-26067)
(cherry picked from commitc40486a)Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
1 parentbd5dfd6 commita2d94a0

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

‎Lib/test/test_typing.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,14 @@ class CustomProtocol(TestCase, Protocol):
14221422
classCustomContextManager(typing.ContextManager,Protocol):
14231423
pass
14241424

1425+
deftest_non_runtime_protocol_isinstance_check(self):
1426+
classP(Protocol):
1427+
x:int
1428+
1429+
withself.assertRaisesRegex(TypeError,"@runtime_checkable"):
1430+
isinstance(1,P)
1431+
1432+
14251433
classGenericTests(BaseTestCase):
14261434

14271435
deftest_basics(self):

‎Lib/typing.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,14 +1343,14 @@ def _no_init(self, *args, **kwargs):
13431343
raiseTypeError('Protocols cannot be instantiated')
13441344

13451345

1346-
def_allow_reckless_class_checks():
1346+
def_allow_reckless_class_checks(depth=3):
13471347
"""Allow instance and class checks for special stdlib modules.
13481348
13491349
The abc and functools modules indiscriminately call isinstance() and
13501350
issubclass() on the whole MRO of a user class, which may contain protocols.
13511351
"""
13521352
try:
1353-
returnsys._getframe(3).f_globals['__name__']in ['abc','functools']
1353+
returnsys._getframe(depth).f_globals['__name__']in ['abc','functools']
13541354
except (AttributeError,ValueError):# For platforms without _getframe().
13551355
returnTrue
13561356

@@ -1370,6 +1370,14 @@ class _ProtocolMeta(ABCMeta):
13701370
def__instancecheck__(cls,instance):
13711371
# We need this method for situations where attributes are
13721372
# assigned in __init__.
1373+
if (
1374+
getattr(cls,'_is_protocol',False)and
1375+
notgetattr(cls,'_is_runtime_protocol',False)and
1376+
not_allow_reckless_class_checks(depth=2)
1377+
):
1378+
raiseTypeError("Instance and class checks can only be used with"
1379+
" @runtime_checkable protocols")
1380+
13731381
if ((notgetattr(cls,'_is_protocol',False)or
13741382
_is_callable_members_only(cls))and
13751383
issubclass(instance.__class__,cls)):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix issue where:mod:`typing` protocols without the ``@runtime_checkable``
2+
decorator did not raise a ``TypeError`` when used with ``issubclass`` and
3+
``isinstance``. Now, subclassses of ``typing.Protocol`` will raise a
4+
``TypeError`` when used with with those checks.
5+
Patch provided by Yurii Karabas.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp