Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
692a0dc9
Commit
692a0dc9
authored
Sep 12, 2019
by
Divij Rajkumar
Committed by
Ivan Levkivskyi
Sep 12, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-38008: Move builtin protocol whitelist to mapping instead of list (GH-15647)
Fixes
https://bugs.python.org/issue38008
parent
ea683dec
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
5 deletions
+20
-5
Lib/test/test_typing.py
Lib/test/test_typing.py
+8
-0
Lib/typing.py
Lib/typing.py
+9
-5
Misc/NEWS.d/next/Library/2019-09-12-10-47-34.bpo-38008.sH74Iy.rst
...S.d/next/Library/2019-09-12-10-47-34.bpo-38008.sH74Iy.rst
+3
-0
No files found.
Lib/test/test_typing.py
View file @
692a0dc9
...
...
@@ -1381,6 +1381,14 @@ class ProtocolTests(BaseTestCase):
self
.
assertIsSubclass
(
B
,
Custom
)
self
.
assertNotIsSubclass
(
A
,
Custom
)
def
test_builtin_protocol_whitelist
(
self
):
with
self
.
assertRaises
(
TypeError
):
class
CustomProtocol
(
TestCase
,
Protocol
):
pass
class
CustomContextManager
(
typing
.
ContextManager
,
Protocol
):
pass
class
GenericTests
(
BaseTestCase
):
def
test_basics
(
self
):
...
...
Lib/typing.py
View file @
692a0dc9
...
...
@@ -989,10 +989,13 @@ def _allow_reckless_class_cheks():
return
True
_PROTO_WHITELIST
=
[
'Callable'
,
'Awaitable'
,
'Iterable'
,
'Iterator'
,
'AsyncIterable'
,
'AsyncIterator'
,
'Hashable'
,
'Sized'
,
'Container'
,
'Collection'
,
'Reversible'
,
'ContextManager'
,
'AsyncContextManager'
]
_PROTO_WHITELIST
=
{
'collections.abc'
:
[
'Callable'
,
'Awaitable'
,
'Iterable'
,
'Iterator'
,
'AsyncIterable'
,
'Hashable'
,
'Sized'
,
'Container'
,
'Collection'
,
'Reversible'
,
],
'contextlib'
:
[
'AbstractContextManager'
,
'AbstractAsyncContextManager'
],
}
class
_ProtocolMeta
(
ABCMeta
):
...
...
@@ -1105,7 +1108,8 @@ class Protocol(Generic, metaclass=_ProtocolMeta):
# ... otherwise check consistency of bases, and prohibit instantiation.
for
base
in
cls
.
__bases__
:
if
not
(
base
in
(
object
,
Generic
)
or
base
.
__module__
==
'collections.abc'
and
base
.
__name__
in
_PROTO_WHITELIST
or
base
.
__module__
in
_PROTO_WHITELIST
and
base
.
__name__
in
_PROTO_WHITELIST
[
base
.
__module__
]
or
issubclass
(
base
,
Generic
)
and
base
.
_is_protocol
):
raise
TypeError
(
'Protocols can only inherit from other'
' protocols, got %r'
%
base
)
...
...
Misc/NEWS.d/next/Library/2019-09-12-10-47-34.bpo-38008.sH74Iy.rst
0 → 100644
View file @
692a0dc9
Fix parent class check in protocols to correctly identify the module that
provides a builtin protocol, instead of assuming they all come from the
:mod:`collections.abc` module
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment