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

Commit08756db

Browse files
authored
gh-105280: Ensureisinstance([], collections.abc.Mapping) always evaluates toFalse (#105281)
1 parent058b960 commit08756db

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

‎Lib/test/test_typing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importcollections.abc
44
fromcollectionsimportdefaultdict
55
fromfunctoolsimportlru_cache,wraps
6+
importgc
67
importinspect
78
importitertools
89
importpickle
@@ -2758,6 +2759,19 @@ def x(self): ...
27582759
withself.assertRaisesRegex(TypeError,only_classes_allowed):
27592760
issubclass(1,BadPG)
27602761

2762+
deftest_isinstance_checks_not_at_whim_of_gc(self):
2763+
self.addCleanup(gc.enable)
2764+
gc.disable()
2765+
2766+
withself.assertRaisesRegex(
2767+
TypeError,
2768+
"Protocols can only inherit from other protocols"
2769+
):
2770+
classFoo(collections.abc.Mapping,Protocol):
2771+
pass
2772+
2773+
self.assertNotIsInstance([],collections.abc.Mapping)
2774+
27612775
deftest_issubclass_and_isinstance_on_Protocol_itself(self):
27622776
classC:
27632777
defx(self):pass

‎Lib/typing.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,25 @@ def _pickle_pskwargs(pskwargs):
17711771
class_ProtocolMeta(ABCMeta):
17721772
# This metaclass is somewhat unfortunate,
17731773
# but is necessary for several reasons...
1774+
def__new__(mcls,name,bases,namespace,/,**kwargs):
1775+
ifname=="Protocol"andbases== (Generic,):
1776+
pass
1777+
elifProtocolinbases:
1778+
forbaseinbases:
1779+
ifnot (
1780+
basein {object,Generic}
1781+
orbase.__name__in_PROTO_ALLOWLIST.get(base.__module__, [])
1782+
or (
1783+
issubclass(base,Generic)
1784+
andgetattr(base,"_is_protocol",False)
1785+
)
1786+
):
1787+
raiseTypeError(
1788+
f"Protocols can only inherit from other protocols, "
1789+
f"got{base!r}"
1790+
)
1791+
returnsuper().__new__(mcls,name,bases,namespace,**kwargs)
1792+
17741793
def__init__(cls,*args,**kwargs):
17751794
super().__init__(*args,**kwargs)
17761795
ifgetattr(cls,"_is_protocol",False):
@@ -1906,14 +1925,7 @@ def _proto_hook(other):
19061925
ifnotcls._is_protocol:
19071926
return
19081927

1909-
# ... otherwise check consistency of bases, and prohibit instantiation.
1910-
forbaseincls.__bases__:
1911-
ifnot (basein (object,Generic)or
1912-
base.__module__in_PROTO_ALLOWLISTand
1913-
base.__name__in_PROTO_ALLOWLIST[base.__module__]or
1914-
issubclass(base,Generic)andgetattr(base,'_is_protocol',False)):
1915-
raiseTypeError('Protocols can only inherit from other'
1916-
' protocols, got %r'%base)
1928+
# ... otherwise prohibit instantiation.
19171929
ifcls.__init__isProtocol.__init__:
19181930
cls.__init__=_no_init_or_replace_init
19191931

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix bug where ``isinstance([], collections.abc.Mapping)`` could evaluate to
2+
``True`` if garbage collection happened at the wrong time. The bug was
3+
caused by changes to the implementation of:class:`typing.Protocol` in
4+
Python 3.12.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp