New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve yield from inference for unions of generators #16717
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
|
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]
|
|
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? Intuitively Any makes more sense to me here.
This affects code like this, right?
x: Iterator[T]
y = yield from x|
|
||
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Fixes #15141, closes #15168
Tests written by omarsilva1