'yield from' rewrite unsafe when looping variable is referenced later
See original GitHub issueConsider the example code:
def foo(it):
item = None
for item in it:
yield item
print("Last item", item)
With --py3-plus
, this is transformed to:
def foo(it):
item = None
yield from it
print("Last item", item)
But these are not equivalent. The line print("Last item", item)
now always prints None
rather than the last item of the passed iterable.
This was discovered in more-itertools:
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (12 by maintainers)
Top Results From Across the Web
What does the "yield" keyword do? - Stack Overflow
yield is a keyword that is used like return , except the function will return ... Then, each subsequent call will run another...
Read more >redefining for loop variable semantics #56010 - GitHub
It seems like an extreme response to force an edit of every for loop that exists today while invalidating all existing documentation and...
Read more >21 Iteration | R for Data Science - Hadley Wickham
A better solution to save the results in a list, and then combine into a single vector after the loop is done: out...
Read more >4. Conditionals and loops — Beginning Python Programming ...
The loop variable is created when the for statement runs, so you do not need to create the variable before then. Each iteration...
Read more >Loop control statements
The variable used in the loop condition is the number i , which you use to count the integers from 1 to 10...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve picked up work again on this in #286 – it’s getting there but needs more scope handling
it’s going to be a little tricky, but not impossible – basically the ast visitor needs to check if the loop variable(s) are referenced after the loop – you might find some prior art from
flake8-bugbear
which has a lint check for this