When specifying the type normally, i.e.
import asyncio
a_var: asyncio.Queue[int] = asyncio.Queue()
Everything works as expected, mypy passes it fine, etc.
However, if I want to do
import asyncio
MyQueue = asyncio.Queue[int]
def myfunc(queue: MyQueue):
...
or
import asyncio
def myfunc(queue: asyncio.Queue[int]):
...
I get
def myfunc(queue: asyncio.Queue[int])
TypeError: 'type' object is not subscriptable
For the second, quoting 'asyncio.Queue[int]' works fine, but the the first mypy gives me
Invalid type "module.MyQueue"
What/is there a good way to achieve this?
Please, read the docs.
Also, in Python 3.7+ you can use from __future__ import annotations to avoid most of these problems.
Most helpful comment
Please, read the docs.
Also, in Python 3.7+ you can use
from __future__ import annotationsto avoid most of these problems.