Typing: Trouble instantiating asyncio.Queue in a type definition

Created on 26 Dec 2018  路  1Comment  路  Source: python/typing

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?

Most helpful comment

Please, read the docs.

Also, in Python 3.7+ you can use from __future__ import annotations to avoid most of these problems.

>All comments

Please, read the docs.

Also, in Python 3.7+ you can use from __future__ import annotations to avoid most of these problems.

Was this page helpful?
0 / 5 - 0 ratings