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

B023 potential false positive (with shadowed variable names) #4090

Closed
XN137 opened this issue Apr 25, 2023 · 2 comments · Fixed by #4111
Closed

B023 potential false positive (with shadowed variable names) #4090

XN137 opened this issue Apr 25, 2023 · 2 comments · Fixed by #4111
Labels
bug Something isn't working

Comments

@XN137
Copy link

XN137 commented Apr 25, 2023

python code:

lambdas = []
for val in range(3):
    lambdas.append(lambda val=val: print(val))

for lam in lambdas:
    lam()

funcs = []
for val in range(3):
    def make_func(val):
        def tmp():
            return print(val)

        return tmp


    funcs.append(make_func(val))

for func in funcs:
    func()

funcs = []
for val in range(3):
    def make_func(val=val):
        def tmp():
            return print(val)

        return tmp


    funcs.append(make_func())

for func in funcs:
    func()

outputs:

0
1
2
0
1
2
0
1
2

but ruff 0.0.263 reports B023 violations for the make_func variants (because the val variable is shadowed):

falsepositive.py:
  12:26 B023 Function definition does not bind loop variable `val`

  26:26 B023 Function definition does not bind loop variable `val`

note that the last make_func variant might be a common pattern in python codebases, see https://stackoverflow.com/a/3431699

@charliermarsh charliermarsh added the bug Something isn't working label Apr 25, 2023
@charliermarsh
Copy link
Member

Agree, does look like a bug. We probably don't have proper scope tracking there.

@XN137
Copy link
Author

XN137 commented May 2, 2023

just to confirm: after upgrading to 0.0.264 we could enable B023 across our codebase without violations, thanks a lot for the quick fix!

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