This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add generic version of OrderedDict to typing module
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, itoijala, levkivskyi, miss-islington, rhettinger, xtreak
Priority: normal Keywords: patch

Created on 2018-11-28 17:17 by itoijala, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 10850 merged itoijala, 2018-12-02 10:13
PR 10851 merged miss-islington, 2018-12-02 15:53
Messages (9)
msg330616 - (view) Author: Ismo Toijala (itoijala) * Date: 2018-11-28 17:17
The other collections from the collections module (namedtuple, deque, ChainMap, Counter, defaultdict) have generic versions in the typing module for use in type annotations.

The problem is currently the following:

from __future__ import annotations
import typing
from collections import OrderedDict
# Understood by mypy
def f(d: OrderedDict[str, str]) -> None:


Traceback (most recent call last):
  File "", line 9, in <module>
  File "/usr/lib/python3.7/", line 1001, in get_type_hints
    value = _eval_type(value, globalns, localns)
  File "/usr/lib/python3.7/", line 260, in _eval_type
    return t._evaluate(globalns, localns)
  File "/usr/lib/python3.7/", line 464, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: 'type' object is not subscriptable

To fix this, a line like the following could be added to Lib/ near line 1250:

OrderedDict = _alias(collections.OrderedDict, (KT, VT))

There might be a reasoning for why this has not been done yet, but I have not been able to find any.

If this is acceptable, I could prepare a PR.
I suppose there is no hope of a backport to 3.7, since this is a new feature (though very minor).
msg330661 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2018-11-29 04:02
Given that this is in collections, I don't object. Ivan, what do you say?
msg330672 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-11-29 09:53
Yes, since we already have `DefaultDict`, `Counter`, and `ChainMap` from collections, we can also add `OrderedDict`.
msg330673 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-11-29 09:55
Also typing is technically still provisional, we can backport this to previous versions (at least to 3.7).
msg330861 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-12-01 21:03
Now that regular dicts are ordered, my expectation is that OrderedDict() is going to mostly fall into disuse (much like UserDict, UserList, UserString).

That said, it would be nice if all of the collections classes had generic counterparts in the typing module.
msg330873 - (view) Author: Ismo Toijala (itoijala) * Date: 2018-12-02 09:55
My reason for using OrderedDict was that the normal dict is not reversible. I needed to get the last element added, so I ended up doing something like next(reversed(ordered_dict)).

Perhaps this would be something to add to the normal dict (for 3.8?). Then I could migrate away from OrderedDict.

Shall I open a new issue for this?
msg330875 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-12-02 10:30
> Shall I open a new issue for this?

@itoijala Please see It's on master.
msg330887 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2018-12-02 15:53
New changeset 68b56d02ef20479b87c65e523cf3dec1b7b77d40 by Ivan Levkivskyi (Ismo Toijala) in branch 'master':
bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
msg330889 - (view) Author: miss-islington (miss-islington) Date: 2018-12-02 16:14
New changeset 6cb0486ce861903448bd6ba1095685b6cd48e3bd by Miss Islington (bot) in branch '3.7':
bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
Date User Action Args
2022-04-11 14:59:08adminsetgithub: 79522
2018-12-02 16:18:57levkivskyisetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-12-02 16:14:46miss-islingtonsetnosy: + miss-islington
messages: + msg330889
2018-12-02 15:53:34miss-islingtonsetpull_requests: + pull_request10086
2018-12-02 15:53:17levkivskyisetmessages: + msg330887
2018-12-02 10:30:26xtreaksetnosy: + xtreak
messages: + msg330875
2018-12-02 10:13:00itoijalasetkeywords: + patch
stage: patch review
pull_requests: + pull_request10085
2018-12-02 09:55:03itoijalasetmessages: + msg330873
2018-12-01 21:03:31rhettingersetnosy: + rhettinger
messages: + msg330861
2018-11-29 09:55:52levkivskyisetmessages: + msg330673
2018-11-29 09:53:25levkivskyisetmessages: + msg330672
2018-11-29 04:02:32gvanrossumsetmessages: + msg330661
2018-11-29 03:36:26xtreaksetnosy: + gvanrossum, levkivskyi
2018-11-28 17:17:38itoijalacreate