PyCQA/flake8-bugbear

`B023` False positive

Ouverte

#380 ouverte le 20 avr. 2023

 (4 commentaires) (5 réactions) (0 personne assignée)Python (116 forks)github user discovery
bugenhancementhelp wanted

Métriques du dépôt

Stars
 (1 116 étoiles)
Métriques de merge PR
 (Merge moyen 4h 17m) (3 PRs mergées en 30 j)

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().

Guide contributeur