PyCQA/flake8-bugbear

`B023` False positive

Aberta

#380 aberto em 20 de abr. de 2023

 (4 comentários) (5 reações) (0 responsável)Python (116 forks)github user discovery
bugenhancementhelp wanted

Métricas do repositório

Stars
 (1.116 estrelas)
Métricas de merge de PR
 (Mesclagem média 4h 17m) (3 fundiu PRs em 30d)

Description

arr = ['a', 'b', 'c', 'd', 'e', 'f']
mydict = {} 
for i in range(0, len(arr)):
    def foo():
        mydict[arr[i]] = 0
    foo()

This example is obviously too simple to be realistic - our actual code has more logic in foo() and passes parameters to it, but is equivalent in terms of everything the rule cares about. We get an error of Function definition does not bind loop variable 'i'.Flake8(B023). This should not happen because foo is not a closure. It's never called outside the loop, and execution is never delayed. i should always have the correct value.

The reason we aren't immediately doing mydict[arr[i]] = 0 is because we have other logic that determines the parameters passed to foo().

Guia do colaborador