[Python-checkins] cpython (merge 3.5 -> 3.6): merge

raymond.hettinger python-checkins at python.org
Tue Jan 17 01:44:21 EST 2017


https://hg.python.org/cpython/rev/3e4484ad182d
changeset:   106185:3e4484ad182d
branch:      3.6
parent:      106162:a9394f673391
parent:      106184:dfefbf6f6c73
user:        Raymond Hettinger <python at rcn.com>
date:        Mon Jan 16 22:43:43 2017 -0800
summary:
  merge

files:
  Doc/library/typing.rst  |   4 ++++
  Lib/test/test_typing.py |  11 +++++++++++
  Lib/typing.py           |  10 ++++++++++
  Misc/NEWS               |   2 ++
  4 files changed, 27 insertions(+), 0 deletions(-)


diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -563,6 +563,10 @@
    As a shorthand for this type, :class:`bytes` can be used to
    annotate arguments of any of the types mentioned above.
 
+.. class:: Deque(deque, MutableSequence[T])
+
+   A generic version of :class:`collections.deque`.
+
 .. class:: List(list, MutableSequence[T])
 
    Generic version of :class:`list`.
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -1572,6 +1572,9 @@
     def test_list(self):
         self.assertIsSubclass(list, typing.List)
 
+    def test_deque(self):
+        self.assertIsSubclass(collections.deque, typing.Deque)
+
     def test_set(self):
         self.assertIsSubclass(set, typing.Set)
         self.assertNotIsSubclass(frozenset, typing.Set)
@@ -1642,6 +1645,14 @@
         self.assertIsSubclass(MyDefDict, collections.defaultdict)
         self.assertNotIsSubclass(collections.defaultdict, MyDefDict)
 
+    def test_no_deque_instantiation(self):
+        with self.assertRaises(TypeError):
+            typing.Deque()
+        with self.assertRaises(TypeError):
+            typing.Deque[T]()
+        with self.assertRaises(TypeError):
+            typing.Deque[int]()
+
     def test_no_set_instantiation(self):
         with self.assertRaises(TypeError):
             typing.Set()
diff --git a/Lib/typing.py b/Lib/typing.py
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -59,6 +59,7 @@
     'SupportsRound',
 
     # Concrete collection types.
+    'Deque',
     'Dict',
     'DefaultDict',
     'List',
@@ -1771,6 +1772,15 @@
                             "use list() instead")
         return _generic_new(list, cls, *args, **kwds)
 
+class Deque(collections.deque, MutableSequence[T], extra=collections.deque):
+
+    __slots__ = ()
+
+    def __new__(cls, *args, **kwds):
+        if _geqv(cls, Deque):
+            raise TypeError("Type Deque cannot be instantiated; "
+                            "use deque() instead")
+        return _generic_new(collections.deque, cls, *args, **kwds)
 
 class Set(set, MutableSet[T], extra=set):
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -47,6 +47,8 @@
 - Issue #29219: Fixed infinite recursion in the repr of uninitialized
   ctypes.CDLL instances.
 
+- Issue #29011:  Fix an important omission by adding Deque to the typing module.
+
 - Issue #28969: Fixed race condition in C implementation of functools.lru_cache.
   KeyError could be raised when cached function with full cache was
   simultaneously called from differen threads with the same uncached arguments.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list