Issue
In TypeScript, you would do something like
async function getString(word: string): Promise<string> {
return word;
}
How can I do the same in Python? I tried the following:
async def get_string(word: str) -> Coroutine[str]:
return word
And got this traceback:
TypeError: Too few parameters for typing.Coroutine; actual 1, expected 3
So Coroutine
expects 3 types. But why? And what should they be in this case?
This is also specified in the docs, but I still don't understand
Solution
The example in the docs shows what the three types are:
from typing import List, Coroutine c = None # type: Coroutine[List[str], str, int] ... x = c.send('hi') # type: List[str] async def bar() -> None: x = await c # type: int
- what you'd get back if you sent a value;
- what value you can send; and
- what you'd get it you awaited it.
It also links to the Generator definition with more examples, and a slightly clearer definition:
Generator[YieldType, SendType, ReturnType]
In your case I'd guess [None, None, str]
, as you only care about the awaitable value.
Answered By - jonrsharpe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.