-
-
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
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,7 @@ def f() -> Generator[int, None, None]: | |
from typing import Iterator | ||
def f() -> Iterator[int]: | ||
yield 1 | ||
return "foo" | ||
return "foo" # E: No return value expected | ||
[out] | ||
|
||
|
||
|
@@ -2231,6 +2231,32 @@ class B: pass | |
def foo(x: int) -> Union[Generator[A, None, None], Generator[B, None, None]]: | ||
yield x # E: Incompatible types in "yield" (actual type "int", expected type "Union[A, B]") | ||
|
||
[case testYieldFromUnionOfGenerators] | ||
from typing import Generator, Union | ||
|
||
class T: pass | ||
|
||
def foo(arg: Union[Generator[int, None, T], Generator[str, None, T]]) -> Generator[Union[int, str], None, T]: | ||
return (yield from arg) | ||
|
||
[case testYieldFromInvalidUnionReturn] | ||
from typing import Generator, Union | ||
|
||
class T: pass | ||
class R: pass | ||
|
||
def foo(arg: Union[T, R]) -> Generator[Union[int, str], None, T]: | ||
return (yield from arg) # E: "yield from" can't be applied to "Union[T, R]" | ||
|
||
[case testYieldFromUnionOfGeneratorWithIterableStr] | ||
from typing import Generator, Union, Iterable, Optional | ||
|
||
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 commentThe 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 Possibly we should allow it for pragmatic reasons, but then again I'm not sure how often people use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, mypy's existing behaviour is to error with |
||
return (yield from arg) | ||
[builtins fixtures/tuple.pyi] | ||
|
||
[case testNoCrashOnStarRightHandSide] | ||
x = *(1, 2, 3) # E: can't use starred expression here | ||
[builtins fixtures/tuple.pyi] | ||
|
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?
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