Infer SendType
in a Python Generator (Coroutine)
#9576
-
I have a very simple use case. When yielding a either It seems that it's not possible for the type system to infer the send type based on the yielded type. But I don't know if there's anything I'm missing and I could be using. from typing import TYPE_CHECKING, Any
if TYPE_CHECKING:
from collections.abc import Generator
from resonate.context import Context
from resonate.record import Promise
from resonate.typing import Yieldable
def bar(ctx: Context, n: int) -> str: ...
def foo(ctx: Context, n: int) -> Generator[Yieldable, Any, str]:
p: Promise[str] = yield ctx.lfi(bar, n)
v: str = yield p
return v I currently rely on annotating variable type on assignment. At annotate the Coroutine as |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Could you provide a self-contained minimal code sample or tell me what library I need to install to repro your code? Your sample references the symbol The send type for a generator function is specified as the second type argument when specializing the |
Beta Was this translation helpful? Give feedback.
Pyright uses the specified SendType from the return type. If you are using multiple different send types in your generator (which is pretty atyipcal), you'd need to declare the type on the receiving variable like you've done in your example.