You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mypy doesn't let you write deque[int] etc., but the recommended replacement (typing.Deque) is not present in Python 3.6.0 typing:
fromcollectionsimportdequedeff(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=FalseifMYPY:
fromtypingimportDeque
....
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.
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.
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[...].
Reacted by Brandon Klein, Steve Dignam and Changsheng Wu
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.
Activity
gvanrossum commentedon May 23, 2017
Ironically, we used to have deque[int] until @ilevkivskyi disallowed that in #2869...
ilevkivskyi commentedon May 23, 2017
The simplest solution is to just allow
deque[int](I prohibited a bunch of similar things likelist[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 commentedon May 23, 2017
OK, let's allow that. Are there any others?
ilevkivskyi commentedon May 23, 2017
Here is the full list of other things prohibited by #2869 for which replacements are proposed:
All corresponding typing types are in 3.6.0.
JukkaL commentedon May 23, 2017
What about 3.5.0? Maybe we should allow indexing all types that don't have corresponding
typingaliases in 3.5.0 or 3.6.0 (or 3.5.x, x>0). We should continue rejectinglist[x]and such because the corresponding aliases have been around from the early days.matthchr commentedon Feb 12, 2019
Wondering if there's been any movement on this? We've got exactly this situation where we want to subscript
Dequeto annotate the specific type of theDeque, 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 commentedon 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-extensionsthen usetyping_extensions.Deque[...]instead oftyping.Deque[...].ilevkivskyi commentedon Apr 1, 2019
See #6593 for another (opposite, i.e. false negative) aspect of this issue.
AlexWaygood commentedon Mar 28, 2022
typing.Dequeis 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 thetyping_extensionsbackports), so I'm closing this issue.