This commit is contained in:
Raymond Hettinger 2017-01-16 22:43:43 -08:00
commit e12c313f5e
4 changed files with 27 additions and 0 deletions

View file

@ -563,6 +563,10 @@ The module defines the following classes, functions and decorators:
As a shorthand for this type, :class:`bytes` can be used to As a shorthand for this type, :class:`bytes` can be used to
annotate arguments of any of the types mentioned above. 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]) .. class:: List(list, MutableSequence[T])
Generic version of :class:`list`. Generic version of :class:`list`.

View file

@ -1572,6 +1572,9 @@ class CollectionsAbcTests(BaseTestCase):
def test_list(self): def test_list(self):
self.assertIsSubclass(list, typing.List) self.assertIsSubclass(list, typing.List)
def test_deque(self):
self.assertIsSubclass(collections.deque, typing.Deque)
def test_set(self): def test_set(self):
self.assertIsSubclass(set, typing.Set) self.assertIsSubclass(set, typing.Set)
self.assertNotIsSubclass(frozenset, typing.Set) self.assertNotIsSubclass(frozenset, typing.Set)
@ -1642,6 +1645,14 @@ class CollectionsAbcTests(BaseTestCase):
self.assertIsSubclass(MyDefDict, collections.defaultdict) self.assertIsSubclass(MyDefDict, collections.defaultdict)
self.assertNotIsSubclass(collections.defaultdict, MyDefDict) 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): def test_no_set_instantiation(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
typing.Set() typing.Set()

View file

@ -59,6 +59,7 @@ __all__ = [
'SupportsRound', 'SupportsRound',
# Concrete collection types. # Concrete collection types.
'Deque',
'Dict', 'Dict',
'DefaultDict', 'DefaultDict',
'List', 'List',
@ -1771,6 +1772,15 @@ class List(list, MutableSequence[T], extra=list):
"use list() instead") "use list() instead")
return _generic_new(list, cls, *args, **kwds) 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): class Set(set, MutableSet[T], extra=set):

View file

@ -47,6 +47,8 @@ Library
- Issue #29219: Fixed infinite recursion in the repr of uninitialized - Issue #29219: Fixed infinite recursion in the repr of uninitialized
ctypes.CDLL instances. 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. - Issue #28969: Fixed race condition in C implementation of functools.lru_cache.
KeyError could be raised when cached function with full cache was KeyError could be raised when cached function with full cache was
simultaneously called from differen threads with the same uncached arguments. simultaneously called from differen threads with the same uncached arguments.