Improve yield from inference for unions of generators#16717
Improve yield from inference for unions of generators#16717hauntsaninja merged 7 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
New error in rich is a true positive |
| # Supertype of Generator (Iterator, Iterable, object): tr is any. | ||
| return AnyType(TypeOfAny.special_form) | ||
| # We have a supertype of Generator (Iterator, Iterable, object) | ||
| # Treat `Iterator[X]` as a shorthand for `Generator[X, Any, None]`. |
There was a problem hiding this comment.
Why? Intuitively Any makes more sense to me here.
This affects code like this, right?
x: Iterator[T]
y = yield from xThere was a problem hiding this comment.
This allows us to produce errors for cases like python/typeshed#11222 / testReturnInIterator
|
|
||
| class T: pass | ||
|
|
||
| def foo(arg: Union[Generator[int, None, T], Iterable[str]]) -> Generator[Union[int, str], None, Optional[T]]: |
There was a problem hiding this comment.
This is unsound and should arguably error. For example, I could pass a Generator[str, None, str] and get a different type out.
Possibly we should allow it for pragmatic reasons, but then again I'm not sure how often people use yield from with values typed as just Iterable.
There was a problem hiding this comment.
Oh this is a great point. I've added a test, but I think this is pre-existing, Iterable[str] acts like Generator[str, Any, Any] in argument positions. mypy handles generator returns kind of weirdly, like it thinks the type of the yield from expr is the return type
mypy's existing behaviour is to error with Function does not return a value (it only ever returns None) [func-returns-value] on the return statement, which doesn't make sense to me
|
Diff from mypy_primer, showing the effect of this PR on open source code: rich (https://github.com/Textualize/rich)
+ rich/segment.py:607: error: No return value expected [return-value]
|
Fixes #15141, closes #15168