KEMBAR78
Improve yield from inference for unions of generators by hauntsaninja · Pull Request #16717 · python/mypy · GitHub
Skip to content

Conversation

hauntsaninja
Copy link
Collaborator

@hauntsaninja hauntsaninja commented Dec 30, 2023

Fixes #15141, closes #15168

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@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

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Mar 7, 2024

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


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.

Copy link
Collaborator Author

@hauntsaninja hauntsaninja Mar 7, 2024

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

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2024

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 hauntsaninja merged commit 394d17b into python:master Mar 21, 2024
@hauntsaninja hauntsaninja deleted the union-gen branch March 21, 2024 02:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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