Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic version of OrderedDict to typing module #79522

Closed
itoijala mannequin opened this issue Nov 28, 2018 · 9 comments
Closed

Add generic version of OrderedDict to typing module #79522

itoijala mannequin opened this issue Nov 28, 2018 · 9 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@itoijala
Copy link
Mannequin

itoijala mannequin commented Nov 28, 2018

BPO 35341
Nosy @gvanrossum, @rhettinger, @ilevkivskyi, @miss-islington, @tirkarthi, @itoijala
PRs
  • bpo-35341: Add generic version of OrderedDict to typing #10850
  • [3.7] bpo-35341: Add generic version of OrderedDict to typing (GH-10850) #10851
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2018-12-02.16:18:57.270>
    created_at = <Date 2018-11-28.17:17:38.210>
    labels = ['3.7', '3.8', 'type-feature', 'library']
    title = 'Add generic version of OrderedDict to typing module'
    updated_at = <Date 2018-12-02.16:18:57.269>
    user = 'https://github.com/itoijala'

    bugs.python.org fields:

    activity = <Date 2018-12-02.16:18:57.269>
    actor = 'levkivskyi'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-12-02.16:18:57.270>
    closer = 'levkivskyi'
    components = ['Library (Lib)']
    creation = <Date 2018-11-28.17:17:38.210>
    creator = 'itoijala'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35341
    keywords = ['patch']
    message_count = 9.0
    messages = ['330616', '330661', '330672', '330673', '330861', '330873', '330875', '330887', '330889']
    nosy_count = 6.0
    nosy_names = ['gvanrossum', 'rhettinger', 'levkivskyi', 'miss-islington', 'xtreak', 'itoijala']
    pr_nums = ['10850', '10851']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue35341'
    versions = ['Python 3.7', 'Python 3.8']

    @itoijala
    Copy link
    Mannequin Author

    itoijala mannequin commented Nov 28, 2018

    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:
        pass
    typing.get_type_hints(f)

    gives:

    Traceback (most recent call last):
      File "foo.py", line 9, in <module>
        typing.get_type_hints(f)
      File "/usr/lib/python3.7/typing.py", line 1001, in get_type_hints
        value = _eval_type(value, globalns, localns)
      File "/usr/lib/python3.7/typing.py", line 260, in _eval_type
        return t._evaluate(globalns, localns)
      File "/usr/lib/python3.7/typing.py", 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/typing.py 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).

    @itoijala itoijala mannequin added 3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Nov 28, 2018
    @gvanrossum
    Copy link
    Member

    Given that this is in collections, I don't object. Ivan, what do you say?

    @ilevkivskyi
    Copy link
    Member

    Yes, since we already have DefaultDict, Counter, and ChainMap from collections, we can also add OrderedDict.

    @ilevkivskyi
    Copy link
    Member

    Also typing is technically still provisional, we can backport this to previous versions (at least to 3.7).

    @rhettinger
    Copy link
    Contributor

    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.

    @itoijala
    Copy link
    Mannequin Author

    itoijala mannequin commented Dec 2, 2018

    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?

    @tirkarthi
    Copy link
    Member

    Shall I open a new issue for this?

    @itoijala Please see https://bugs.python.org/issue33462. It's on master.

    @ilevkivskyi
    Copy link
    Member

    New changeset 68b56d0 by Ivan Levkivskyi (Ismo Toijala) in branch 'master':
    bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
    68b56d0

    @miss-islington
    Copy link
    Contributor

    New changeset 6cb0486 by Miss Islington (bot) in branch '3.7':
    bpo-35341: Add generic version of OrderedDict to typing (GH-10850)
    6cb0486

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants