-
-
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
Merged
hauntsaninja
merged 13 commits into
python:master
from
ikonst:07-19-intentionally_empty_generator_marked_as_unreachable
Jul 26, 2023
Merged
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
05cfabd
Don't flag intentionally empty generators unreachable
ikonst 29c3463
add test
ikonst 7871b8d
support 'return None' and 'yield None'
ikonst 5e33f47
AlexWaygood feedback
ikonst ebc1cfa
less walrus
ikonst a64254c
make the TODO-remove part second
ikonst 6d20704
empty generator *functions*
ikonst 82e8c8c
empty generator *function*
ikonst 2a95a6d
Update mypy/checker.py
ikonst a090d56
Remove TODO / comment around suppress_unreachable_warnings
ikonst 91a97c8
Merge branch 'master' into 07-19-intentionally_empty_generator_marked…
ikonst 7c2a83b
Update checker.py comment
ikonst df177cd
Update mypy/checker.py
ikonst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||||||||||||
|
@@ -1240,7 +1241,10 @@ def check_func_def( | |||||||||||
# have no good way of doing this. | ||||||||||||
# | ||||||||||||
# TODO: Find a way of working around this limitation | ||||||||||||
if len(expanded) >= 2: | ||||||||||||
# | ||||||||||||
# We suppress reachability warnings for empty generators (return; yield), since there's | ||||||||||||
# no way to promote a function into a generator except by adding an "unreachable" yield. | ||||||||||||
if len(expanded) >= 2 or _is_empty_generator(item): | ||||||||||||
self.binder.suppress_unreachable_warnings() | ||||||||||||
self.accept(item.body) | ||||||||||||
unreachable = self.binder.is_unreachable() | ||||||||||||
|
@@ -6961,6 +6965,21 @@ def is_literal_not_implemented(n: Expression) -> bool: | |||||||||||
return isinstance(n, NameExpr) and n.fullname == "builtins.NotImplemented" | ||||||||||||
|
||||||||||||
|
||||||||||||
def _is_empty_generator(func: FuncItem) -> bool: | ||||||||||||
ikonst marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
""" | ||||||||||||
Checks whether a function's body is 'return; yield' (the yield being added only | ||||||||||||
to promote the function into a generator). | ||||||||||||
ikonst marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
""" | ||||||||||||
return ( | ||||||||||||
len(body := func.body.body) == 2 | ||||||||||||
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. Nit: I think the first walrus here is slightly gratuitous
Suggested change
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. |
||||||||||||
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 builtin_item_type(tp: Type) -> Type | None: | ||||||||||||
"""Get the item type of a builtin container. | ||||||||||||
|
||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
I had "also" at first, but I wanted to be able to remove the first comment w/o touching the second.
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.
Meh, I think I'd personally prioritise a readable comment over a tiny bit more churn in
git blame
:pThere 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.
Ahh, easy. I've just made the "TODO remove me" part second. :P