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.]
Ironically, we used to have deque[int] until @ilevkivskyi disallowed that in https://github.com/python/mypy/pull/2869...
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.
OK, let's allow that. Are there any others?
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.
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.
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.
@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[...].
See https://github.com/python/mypy/issues/6593 for another (opposite, i.e. false negative) aspect of this issue.
Most helpful comment
@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-extensionsthen usetyping_extensions.Deque[...]instead oftyping.Deque[...].