Replies: 2 comments
-
Yes, I think that's sound, but it also feels like a pretty rare special case. I'd entertain adding such a feature if there's signal from enough pyright users that it would be useful (e.g. in the form of upvotes on this discussion). Obviously, you can include an explicit return type annotation if you want to specify the |
Beta Was this translation helpful? Give feedback.
-
FYI, what i was doing is like from collections.abc import Callable, Generator
from dataclasses import dataclass
from functools import wraps
from typing import Any, Never, Self, TypeGuard, final
@final
@dataclass
class Just[T]:
value: T
def is_empty(self):
return False
def __pos__(self) -> Generator[Self, Never, T]:
yield self
return self.value
@final
@dataclass
class Nothing:
def is_empty(self):
return True
def __pos__(self) -> Generator[Self, Never, Never]:
yield self
raise TypeError()
type Maybe[T] = Just[T] | Nothing
def is_empty(e: Maybe[Any]) -> TypeGuard[Nothing]:
return e.is_empty()
def do[**P, T](f: Callable[P, Generator[Maybe[Any], Never, T]]) -> Callable[P, Maybe[T]]:
@wraps(f)
def wrapper(*args: P.args, **kwargs: P.kwargs):
g = f(*args, **kwargs)
try:
return next(filter(is_empty, g))
except StopIteration as e:
r: T = e.value
return Just(r)
return wrapper
#####
def f() -> Maybe[int]: ...
def g() -> Maybe[str]: ...
@do
def h(): # error: Return type, "Generator[Just[int] | Nothing | Just[str], Unknown, tuple[int, str]]", is partially unknown (reportUnknownParameterType)
x = yield from +f()
y = yield from +g()
reveal_type(x) # int
reveal_type(y) # str
return x, y
reveal_type(h) # () -> Maybe[tuple[int, str]] |
Beta Was this translation helpful? Give feedback.
-
I've read followings and have a question:
#5181
#6468
microsoft/pylance-release#4275
#5716
Q. is it possible to infer 2nd type argument of return Generator type when following conditions are met:
yield from
, noyield
Generator
ex)
Beta Was this translation helpful? Give feedback.
All reactions