Skip to content
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

B007 does not detect unused variables when they are shadowed or appear in inner scope #14113

Open
LordAro opened this issue Nov 5, 2024 · 2 comments · May be fixed by #14122
Open

B007 does not detect unused variables when they are shadowed or appear in inner scope #14113

LordAro opened this issue Nov 5, 2024 · 2 comments · May be fixed by #14122
Assignees
Labels
bug Something isn't working

Comments

@LordAro
Copy link

LordAro commented Nov 5, 2024

B007 (unused-loop-control-variable) does not trigger when variables are shadowed.

See this extremely contrived example:

my_dict = my_other_dict = {}
my_list = []
for k, (v1, v2) in my_dict.items():
    for v1 in my_other_dict.values():
        print(v1)
    v2 = "flibble"
    print([k for k in my_list if k > 0])

https://play.ruff.rs/92ded63b-80d4-4d7d-989a-3460865af51b

(Example is somewhat nested loop focused as that's what I thought the problem was initially)

Interestingly, PERF102 does trigger with this example, but I think only because it doesn't process comprehensions at all? If it's expanded out the warning goes away.

The only thing of note from ALL is PLW2901, which detects the variable shadowing.

Related issues: #8786 #9454 #10723

ruff 0.7.2

@LordAro LordAro changed the title B007 does not detect shadowed variables B007 does not detect unused variables when they are shadowed Nov 5, 2024
@dylwil3
Copy link
Collaborator

dylwil3 commented Nov 5, 2024

Excellent, thanks for this! It looks like the same problem occurs even in this smaller example Playground link:

for k in ls:
    print([k for k in otherls])

@dylwil3 dylwil3 added the bug Something isn't working label Nov 5, 2024
@dylwil3 dylwil3 changed the title B007 does not detect unused variables when they are shadowed B007 does not detect unused variables when they are shadowed or appear in inner scope Nov 5, 2024
@dylwil3
Copy link
Collaborator

dylwil3 commented Nov 5, 2024

I guess technically my previous example wasn't "shadowing", but an issue of scope. But shadowing also doesn't work (as you showed well in your original example):

for k in ls:
    k = 2

But, as you also point out, this is caught by PLW2901.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants