-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
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.
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
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 allows us to produce errors for cases like python/typeshed#11222 / testReturnInIterator
test-data/unit/check-statements.test
Outdated
|
||
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
.
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.
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