Closed B4rtware closed 3 years ago
Wrap your type in a string.
tasks: List["asyncio.Task[None]"] = []
Other option add
from __future__ import annotations
to the top of your file. That will treat all type annotations as strings.
Pyright should warn you if you attempt to subscript a type that will generate a runtime exception. I didn't realize that asyncio.Task
didn't support subscripting prior to Python 3.9. I've updated the code so it knows about this case.
This is not a bug report, but a question on how to write a correctly typed piece of code.
Description: I would like to type hint my list of tasks. Unfortunately, I am not able to define the type so that pyright does not throw an error. The given code example throws:
Type of "tasks" is partially unknown
. (I also know that all of my courotines inside that List will returnNone
.)I have tried to use asyncio.Task[None] which is also proposed via pyright by using
and hovering over the variable
a
-> Task[None]. So this code snippet:does not throw an error by pyright but I get a runtime error:
I have also tried to use the
typing.Awaitable
this gives no errors but hides the Task functions because it does not exposes the Future or Task interface.And I guess the TypeError above occurs because of the same reason we can't use the built in type list for type hinting before 3.9 and without future.
I am using pyright 1.1.164 and strict mode.
Do you know how I can type hint it correctly?