question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'yield from' rewrite unsafe when looping variable is referenced later

See original GitHub issue

Consider 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:

https://github.com/erikrose/more-itertools/blob/8d860c355a1dd900e3a9c5fe1303ddf5a778452f/more_itertools/more.py#L1206-L1223

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
asottilecommented, Apr 22, 2020

I’ve picked up work again on this in #286 – it’s getting there but needs more scope handling

1reaction
asottilecommented, Oct 31, 2019

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found