Skip to content

Make deque subscriptable? #3413

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

Closed
JukkaL opened this issue May 23, 2017 · 9 comments
Closed

Make deque subscriptable? #3413

JukkaL opened this issue May 23, 2017 · 9 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented May 23, 2017

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.]

@gvanrossum
Copy link
Member

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

@ilevkivskyi
Copy link
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.

@gvanrossum
Copy link
Member

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

@ilevkivskyi
Copy link
Member

ilevkivskyi commented May 23, 2017

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.

@JukkaL
Copy link
Collaborator Author

JukkaL commented May 23, 2017

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.

@ilevkivskyi ilevkivskyi self-assigned this May 30, 2017
@matthchr
Copy link

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.

@Michael0x2a
Copy link
Collaborator

Michael0x2a commented Feb 12, 2019

@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[...].

@ilevkivskyi
Copy link
Member

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

@ilevkivskyi ilevkivskyi removed their assignment Dec 9, 2019
@AlexWaygood
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@JukkaL @Michael0x2a @gvanrossum @matthchr @ilevkivskyi @AlexWaygood and others