-
-
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
Fix union of generators having no return type #15168
Conversation
of generators
for more information, see https://pre-commit.ci
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
(the timing change is probably noise) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks for working on this! I think ikonst's observation is a good one, e.g. what if we have a Union of Iterator and Generator or something. I was playing around with this diff: diff --git a/mypy/checker.py b/mypy/checker.py
index c1c31538b..6f1705cf7 100644
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -952,8 +952,9 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
# AwaitableGenerator, Generator: tr is args[2].
return return_type.args[2]
else:
- # 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]`.
+ return NoneType()
def visit_func_def(self, defn: FuncDef) -> None:
if not self.recurse_into_functions:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
index cd0ff1100..34dcbdef0 100644
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -5177,17 +5177,7 @@ class ExpressionChecker(ExpressionVisitor[Type]):
# Determine the type of the entire yield from expression.
iter_type = get_proper_type(iter_type)
- if isinstance(iter_type, Instance) and iter_type.type.fullname == "typing.Generator":
- expr_type = self.chk.get_generator_return_type(iter_type, False)
- else:
- # Non-Generators don't return anything from `yield from` expressions.
- # However special-case Any (which might be produced by an error).
- actual_item_type = get_proper_type(actual_item_type)
- if isinstance(actual_item_type, AnyType):
- expr_type = AnyType(TypeOfAny.from_another_any, source_any=actual_item_type)
- else:
- # Treat `Iterator[X]` as a shorthand for `Generator[X, None, Any]`.
- expr_type = NoneType()
+ expr_type = self.chk.get_generator_return_type(iter_type, False)
if not allow_none_return and isinstance(get_proper_type(expr_type), NoneType):
self.chk.msg.does_not_return_value(None, e) It causes two other tests to fail, but I think the new behaviour is better than the old behaviour. What do you think? |
NoneType for the supertype case instead of AnyType
get_generator_return_type independently of iter_type
is a return type
Thanks for the feedback! I added your suggestions, I also think that this new behaviour is more natural than the previous one. I only had one test failing locally, where (I see that the second test that fails has to do with mypyc ( |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: graphql-core (https://github.com/graphql-python/graphql-core): typechecking got 1.05x slower (363.9s -> 382.2s)
(Performance measurements are based on a single noisy sample)
rich (https://github.com/Textualize/rich)
+ rich/segment.py:608: error: No return value expected [return-value]
|
Any luck with fixing the test? |
Fixes #15141
This adds a check if
iter_type
is a union type so thatexpr_type
is set to the generator return type instead of none.