In the following code, I don't understand why I should need to add annotation for the queue variable, but not event.
import asyncio
class Foo:
def __init__(self) -> None:
self.event = asyncio.Event()
self.queue: asyncio.Queue = asyncio.Queue()
Without the type annotation, I get:
/code # mypy --version
mypy 0.521
/code # mypy test.py
test.py:8: error: Need type annotation for variable
asyncio.Queue is generic (you need to specify, for example, a queue of ints with asyncio.Queue[int]), but asyncio.Event is not. You can see this in the asyncio stubs in typeshed (github.com/python/typeshed).
asyncio.Queue[int] doesn't work, you must mean some other syntax.
>>> asyncio.Queue[int]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
It does not work at runtime, but it works in unevaluated type annotations. For example, self.queue: asyncio.Queue[int] = asyncio.Queue() should work.
Indeed it does. Black magic, is what it is, but whatev.
A quoted annotation 'Queue[int]' would work in other contexts as well.
The reason asyncio.Queue[int] is not evaluated in the example is a subtle corner of PEP 526. In particular, the section Runtime Effects of Type Annotations has the sentence "Annotations for local variables will not be evaluated" (plus an example). Even though the OP annotates a self attribute it still falls under this rule.
The other subtle part of the story here is that asyncio.Queue is only declared as a generic class in typeshed, which is where mypy gets its definitions, but not in the actual stdlib (yet). That is why when the OP tried asyncio.Queue[int] at the REPL it failed.
Finally, writing just asyncio.Queue in a type annotation is equivalent to asyncio.Queue[Any]. But maybe that's what you wanted anyway.
It works for local variables (in functions, methods), but not for global
variables.
import asyncio
def f():
... queue: asyncio.Queue[int] = asyncio.Queue()
...
f()
On Wed, 6 Jun 2018 at 10:08, Guilherme Salgado notifications@github.com
wrote:
@JelleZijlstra https://github.com/JelleZijlstra It doesn't seem to work
on 3.6.5 without the quotes. Any idea why?Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.import asyncio
queue: asyncio.Queue[int] = asyncio.Queue()
Traceback (most recent call last):
File "", line 1, in
TypeError: 'type' object is not subscriptable—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/python/mypy/issues/4052#issuecomment-394999988, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACGGaLUXfkhfE463OB53bLpnCxdMiVY2ks5t55wXgaJpZM4Psna8
.
--
Gustavo J. A. M. Carneiro
Gambit Research
"The universe is always one step beyond logic." -- Frank Herbert
Guido explains above why that is.
Most helpful comment
The reason
asyncio.Queue[int]is not evaluated in the example is a subtle corner of PEP 526. In particular, the section Runtime Effects of Type Annotations has the sentence "Annotations for local variables will not be evaluated" (plus an example). Even though the OP annotates aselfattribute it still falls under this rule.The other subtle part of the story here is that
asyncio.Queueis only declared as a generic class in typeshed, which is where mypy gets its definitions, but not in the actual stdlib (yet). That is why when the OP triedasyncio.Queue[int]at the REPL it failed.Finally, writing just
asyncio.Queuein a type annotation is equivalent toasyncio.Queue[Any]. But maybe that's what you wanted anyway.