Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 8049052

Browse files
committed
Issue python#29011: Fix an important omission by adding Deque to the typing module.
1 parent a105dd3 commit 8049052

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

Doc/library/typing.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,10 @@ The module defines the following classes, functions and decorators:
557557
As a shorthand for this type, :class:`bytes` can be used to
558558
annotate arguments of any of the types mentioned above.
559559

560+
.. class:: Deque(deque, MutableSequence[T])
561+
562+
A generic version of :class:`collections.deque`.
563+
560564
.. class:: List(list, MutableSequence[T])
561565

562566
Generic version of :class:`list`.

Lib/test/test_typing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,9 @@ def test_bytestring(self):
15721572
def test_list(self):
15731573
self.assertIsSubclass(list, typing.List)
15741574

1575+
def test_deque(self):
1576+
self.assertIsSubclass(collections.deque, typing.Deque)
1577+
15751578
def test_set(self):
15761579
self.assertIsSubclass(set, typing.Set)
15771580
self.assertNotIsSubclass(frozenset, typing.Set)
@@ -1642,6 +1645,14 @@ class MyDefDict(typing.DefaultDict[str, int]):
16421645
self.assertIsSubclass(MyDefDict, collections.defaultdict)
16431646
self.assertNotIsSubclass(collections.defaultdict, MyDefDict)
16441647

1648+
def test_no_deque_instantiation(self):
1649+
with self.assertRaises(TypeError):
1650+
typing.Deque()
1651+
with self.assertRaises(TypeError):
1652+
typing.Deque[T]()
1653+
with self.assertRaises(TypeError):
1654+
typing.Deque[int]()
1655+
16451656
def test_no_set_instantiation(self):
16461657
with self.assertRaises(TypeError):
16471658
typing.Set()

Lib/typing.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
'SupportsRound',
6060

6161
# Concrete collection types.
62+
'Deque',
6263
'Dict',
6364
'DefaultDict',
6465
'List',
@@ -1771,6 +1772,15 @@ def __new__(cls, *args, **kwds):
17711772
"use list() instead")
17721773
return _generic_new(list, cls, *args, **kwds)
17731774

1775+
class Deque(collections.deque, MutableSequence[T], extra=collections.deque):
1776+
1777+
__slots__ = ()
1778+
1779+
def __new__(cls, *args, **kwds):
1780+
if _geqv(cls, Deque):
1781+
raise TypeError("Type Deque cannot be instantiated; "
1782+
"use deque() instead")
1783+
return _generic_new(collections.deque, cls, *args, **kwds)
17741784

17751785
class Set(set, MutableSet[T], extra=set):
17761786

Misc/NEWS

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22
Python News
33
+++++++++++
44

5+
What's New in Python 3.5.4?
6+
===========================
7+
8+
Core and Builtins
9+
-----------------
10+
11+
Library
12+
-------
13+
14+
- Issue #29011: Fix an important omission by adding Deque to the typing module.
15+
16+
517
What's New in Python 3.5.3?
618
===========================
719

@@ -528,17 +540,17 @@ Library
528540

529541
- Issue #27972: Prohibit Tasks to await on themselves.
530542

531-
- Issue #26923: Fix asyncio.Gather to refuse being cancelled once all
543+
- Issue #26923: Fix asyncio.Gather to refuse being cancelled once all
532544
children are done.
533545
Patch by Johannes Ebke.
534546

535-
- Issue #26796: Don't configure the number of workers for default
547+
- Issue #26796: Don't configure the number of workers for default
536548
threadpool executor.
537549
Initial patch by Hans Lawrenz.
538550

539551
- Issue #28600: Optimize loop.call_soon().
540552

541-
- Issue #28613: Fix get_event_loop() return the current loop if
553+
- Issue #28613: Fix get_event_loop() return the current loop if
542554
called from coroutines/callbacks.
543555

544556
- Issue #28639: Fix inspect.isawaitable to always return bool
@@ -553,7 +565,7 @@ Library
553565
- Issue #24142: Reading a corrupt config file left the parser in an
554566
invalid state. Original patch by Florian Höch.
555567

556-
- Issue #28990: Fix SSL hanging if connection is closed before handshake
568+
- Issue #28990: Fix SSL hanging if connection is closed before handshake
557569
completed.
558570
(Patch by HoHo-Ho)
559571

0 commit comments

Comments
 (0)