Skip to content

Make deque subscriptable? #3413

Closed
Closed

Description

@JukkaL

Mypy doesn't let you write deque[int] etc., but the recommended replacement (typing.Deque) is not present in Python 3.6.0 typing:

from collections import deque

def f(x: 'deque[int]') -> None: pass  # deque not subscriptable, use typing.Deque

Maybe we should allow the above example since typing.Deque may be unavailable at runtime. Alternatively, we could modify the message to recommend using something like this:

MYPY = False
if MYPY:
    from typing import Deque
....

However, the above is pretty unintuitive. TYPE_CHECKING is not present in earlier versions of typing so it's not always an option, but we could perhaps recommend it.

[Zulip hit this issue recently.]

Activity

  1. gvanrossum commented on May 23, 2017

    @gvanrossum
    Member

    Ironically, we used to have deque[int] until @ilevkivskyi disallowed that in #2869...

  2. ilevkivskyi commented on May 23, 2017

    @ilevkivskyi
    Member

    The simplest solution is to just allow deque[int] (I prohibited a bunch of similar things like list[int] in the same PR, but those are probably OK). Alternatively, we can show this error depending on the Python version that is being used (sys.version_info) or the Python version from mypy options.

  3. gvanrossum commented on May 23, 2017

    @gvanrossum
    Member

    OK, let's allow that. Are there any others?

  4. ilevkivskyi commented on May 23, 2017

    @ilevkivskyi
    Member

    Here is the full list of other things prohibited by #2869 for which replacements are proposed:

    list[int]
    dict[int, str]
    set[int]
    tuple[int]
    frozenset[int]
    collections.defaultdict[int, str]
    collections.Counter[str]
    collections.ChainMap[str, str]

    All corresponding typing types are in 3.6.0.

  5. JukkaL commented on May 23, 2017

    @JukkaL
    CollaboratorAuthor

    All corresponding typing types are in 3.6.0.

    What about 3.5.0? Maybe we should allow indexing all types that don't have corresponding typing aliases in 3.5.0 or 3.6.0 (or 3.5.x, x>0). We should continue rejecting list[x] and such because the corresponding aliases have been around from the early days.

  6. self-assigned this
    on May 30, 2017
  7. matthchr commented on Feb 12, 2019

    @matthchr

    Wondering if there's been any movement on this? We've got exactly this situation where we want to subscript Deque to annotate the specific type of the Deque, but unfortunately we're targeting both Python 3.5 and 3.6, and as far as I can tell based on your comments above there's no workaround for 3.5.

  8. Michael0x2a commented on Feb 12, 2019

    @Michael0x2a
    Collaborator

    @matthchr -- I believe we solved this via the typing_extensions module, which contains backports of these kinds of types for older versions of Python 3.5 and 3.6.

    So in your case, you'd want to pip install typing-extensions then use typing_extensions.Deque[...] instead of typing.Deque[...].

  9. ilevkivskyi commented on Apr 1, 2019

    @ilevkivskyi
    Member

    See #6593 for another (opposite, i.e. false negative) aspect of this issue.

  10. removed their assignment
    on Dec 9, 2019
  11. AlexWaygood commented on Mar 28, 2022

    @AlexWaygood
    Member

    typing.Deque is available on Python 3.6, and Python 3.5 is no longer supported by mypy. I can't find any other aliases where this is an issue (especially given the typing_extensions backports), so I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions