[Python-checkins] bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
Ivan Levkivskyi
webhook-mailer at python.org
Sun Dec 2 10:53:18 EST 2018
https://github.com/python/cpython/commit/68b56d02ef20479b87c65e523cf3dec1b7b77d40
commit: 68b56d02ef20479b87c65e523cf3dec1b7b77d40
branch: master
author: Ismo Toijala <ismo.toijala at gmail.com>
committer: Ivan Levkivskyi <levkivskyi at gmail.com>
date: 2018-12-02T15:53:14Z
summary:
bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
files:
A Misc/NEWS.d/next/Library/2018-12-02-13-50-52.bpo-35341.32E8T_.rst
M Doc/library/typing.rst
M Lib/test/test_typing.py
M Lib/typing.py
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 4469e88b2248..47ae4213f3c0 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -689,6 +689,12 @@ The module defines the following classes, functions and decorators:
.. versionadded:: 3.5.2
+.. class:: OrderedDict(collections.OrderedDict, MutableMapping[KT, VT])
+
+ A generic version of :class:`collections.OrderedDict`.
+
+ .. versionadded:: 3.7.2
+
.. class:: Counter(collections.Counter, Dict[T, int])
A generic version of :class:`collections.Counter`.
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 6d8cc5319fb1..0d66ebbd1845 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -2075,6 +2075,22 @@ class MyDefDict(typing.DefaultDict[str, int]):
self.assertIsSubclass(MyDefDict, collections.defaultdict)
self.assertNotIsSubclass(collections.defaultdict, MyDefDict)
+ def test_ordereddict_instantiation(self):
+ self.assertIs(type(typing.OrderedDict()), collections.OrderedDict)
+ self.assertIs(type(typing.OrderedDict[KT, VT]()), collections.OrderedDict)
+ self.assertIs(type(typing.OrderedDict[str, int]()), collections.OrderedDict)
+
+ def test_ordereddict_subclass(self):
+
+ class MyOrdDict(typing.OrderedDict[str, int]):
+ pass
+
+ od = MyOrdDict()
+ self.assertIsInstance(od, MyOrdDict)
+
+ self.assertIsSubclass(MyOrdDict, collections.OrderedDict)
+ self.assertNotIsSubclass(collections.OrderedDict, MyOrdDict)
+
@skipUnless(sys.version_info >= (3, 3), 'ChainMap was added in 3.3')
def test_chainmap_instantiation(self):
self.assertIs(type(typing.ChainMap()), collections.ChainMap)
diff --git a/Lib/typing.py b/Lib/typing.py
index 4f9e04506fb5..3243e2af1119 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -1241,6 +1241,7 @@ def _alias(origin, params, inst=True):
AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co)
Dict = _alias(dict, (KT, VT), inst=False)
DefaultDict = _alias(collections.defaultdict, (KT, VT))
+OrderedDict = _alias(collections.OrderedDict, (KT, VT))
Counter = _alias(collections.Counter, T)
ChainMap = _alias(collections.ChainMap, (KT, VT))
Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co))
diff --git a/Misc/NEWS.d/next/Library/2018-12-02-13-50-52.bpo-35341.32E8T_.rst b/Misc/NEWS.d/next/Library/2018-12-02-13-50-52.bpo-35341.32E8T_.rst
new file mode 100644
index 000000000000..43aa9956c1f7
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-12-02-13-50-52.bpo-35341.32E8T_.rst
@@ -0,0 +1 @@
+Add generic version of ``collections.OrderedDict`` to the ``typing`` module. Patch by Ismo Toijala.
More information about the Python-checkins
mailing list