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.

Scope of variables used in for loops is not respected when renaming symbols

See original GitHub issue

Environment data

  • VS Code version: 1.36
  • Extension version (available under the Extensions sidebar): 2019.6.22090
  • OS and version: Windows 10 home 1903
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.3
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): N/A
  • Relevant/affected Python packages and their versions: N/A
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info #3977): Jedi

Expected behaviour

When using the rename symbols function, if a variable that is an index variable for a for loop but is also used for a different for loop, then only the relavent for loop will be changed.

Actual behaviour

All instances of the variable are changed.

Steps to reproduce:

  1. Select for loop variable
  2. Right click and select Rename Symbol
  3. Change the variable name

image

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

##########Linting Output - mypy##########
##########Linting Output - pydocstyle##########
d:\Documents\test\test.py:1 at module level:
        D100: Missing docstring in public module
##########Linting Output - pep8##########
Starting Jedi Python language engine.
##########Linting Output - pylint##########
************* Module test
1,0,convention,missing-docstring:Missing module docstring

------------------------------------------------------------------

Your code has been rated at 6.67/10 (previous run: 6.67/10, +0.00)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brettcannoncommented, Sep 21, 2021

Python’s scoping rules are:

  1. local
  2. closure/nonlocal
  3. global
  4. built-ins

So for loops don’t create a scope. This is why you can still read the loop variant after a for loop executes, so the variable should be considered the same for the entire local scope of the function.

0reactions
sudoandroscommented, Sep 22, 2021

@jakebailey @brettcannon Well, yeah, that’s right but I don’t think a lot of people use variables this way (IMHO it would not be an easy to read code). So it would be nevertheless a cool feature to have: “rename” bound by loop. These behaviour may be optional. But I see your point, then it becomes more a feature request than a bug. And it may be difficult to implement.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Variable Scope | Fix error: 'yourVariable' was not declared in ...
We've got a function called setup and we have a function called loop. If I create a variable inside setup I can't use...
Read more >
For loop iterator variable scope - Snap! Forums
The scope and lifetime of the "i" variable created by the for loop is confusing. What should Snap! do instead? Use some unique...
Read more >
I would like to rename all my variables within a script without ...
Rename the variable at first to a unqiue name by editing one instance and hitting - I do not remember, perhaps Shift-Return (it...
Read more >
Template Designer Documentation - Jinja
Filters are separated from the variable by a pipe symbol ( | ) and may have ... {# note: commented-out template because we...
Read more >
for-Loop in R (10 Examples) | Writing, Running & Using Loops ...
How to write & use for-loops in R - 10 R programming examples - Reproducible R programming code in ... 9) Example 8:...
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