Scope of variables used in for loops is not respected when renaming symbols
See original GitHub issueEnvironment 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:
- Select for loop variable
- Right click and select Rename Symbol
- Change the variable name
Logs
Output for Python
in the Output
panel (View
→Output
, 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:
- Created 4 years ago
- Reactions:1
- Comments:8 (4 by maintainers)
Top 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 >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
Python’s scoping rules are:
nonlocal
So
for
loops don’t create a scope. This is why you can still read the loop variant after afor
loop executes, so the variable should be considered the same for the entire local scope of the function.@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.