-
-
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
Don't flag intentionally empty generators unreachable #15722
Changes from 3 commits
05cfabd
29c3463
7871b8d
5e33f47
ebc1cfa
a64254c
6d20704
82e8c8c
2a95a6d
a090d56
91a97c8
7c2a83b
df177cd
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 |
---|---|---|
|
@@ -132,6 +132,7 @@ | |
Var, | ||
WhileStmt, | ||
WithStmt, | ||
YieldExpr, | ||
is_final_node, | ||
) | ||
from mypy.options import Options | ||
|
@@ -1063,6 +1064,20 @@ def enter_attribute_inference_context(self) -> Iterator[None]: | |
yield None | ||
self.inferred_attribute_types = old_types | ||
|
||
def _is_empty_generator(self, func: FuncItem) -> bool: | ||
""" | ||
Checks whether a function's body is 'return; yield' (the yield being added only | ||
to promote the function into a generator). | ||
""" | ||
return ( | ||
len(body := func.body.body) == 2 | ||
and isinstance(ret_stmt := body[0], ReturnStmt) | ||
and (ret_stmt.expr is None or is_literal_none(ret_stmt.expr)) | ||
and isinstance(expr_stmt := body[1], ExpressionStmt) | ||
and isinstance(yield_expr := expr_stmt.expr, YieldExpr) | ||
and (yield_expr.expr is None or is_literal_none(yield_expr.expr)) | ||
) | ||
|
||
def check_func_def( | ||
self, defn: FuncItem, typ: CallableType, name: str | None, allow_empty: bool = False | ||
) -> None: | ||
|
@@ -1240,7 +1255,7 @@ def check_func_def( | |
# have no good way of doing this. | ||
# | ||
# TODO: Find a way of working around this limitation | ||
if len(expanded) >= 2: | ||
if len(expanded) >= 2 or self._is_empty_generator(item): | ||
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. Maybe update the multiline comment immediately above this line? We're not just suppressing reachability warnings for TypeVars with value restrictions anymore 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. Yeah, I didn't want to muddy the blame for that comment 🤣 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. |
||
self.binder.suppress_unreachable_warnings() | ||
self.accept(item.body) | ||
unreachable = self.binder.is_unreachable() | ||
|
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.
Or even better: move the function into the global scope?
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.
e31efd3