Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

hauntsaninja
Copy link
Collaborator

Fixes #15141, closes #15168

Tests written by omarsilva1

This comment has been minimized.

Copy link
Contributor

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]

@hauntsaninja
Copy link
Collaborator Author

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]`.
Copy link
Member

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]]:
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Starting from version 1.0.0, a union of Generators is (sometimes) treated as if it has no ReturnType
2 participants