Skip to content

Commit

Permalink
bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
Browse files Browse the repository at this point in the history
(cherry picked from commit 68b56d0)

Co-authored-by: Ismo Toijala <ismo.toijala@gmail.com>
  • Loading branch information
miss-islington and itoijala committed Dec 2, 2018
1 parent 265b419 commit 6cb0486
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Doc/library/typing.rst
Expand Up @@ -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`.
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_typing.py
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Lib/typing.py
Expand Up @@ -1242,6 +1242,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))
Expand Down
@@ -0,0 +1 @@
Add generic version of ``collections.OrderedDict`` to the ``typing`` module. Patch by Ismo Toijala.

0 comments on commit 6cb0486

Please sign in to comment.