PyCQA/flake8-bugbear

`B023` False positive

開放

#380 建立於 2023年4月20日

 (4 則留言) (5 個反應) (0 位負責人)Python (116 個分叉)github user discovery
bugenhancementhelp wanted

倉庫指標

星標
 (1,116 顆星)
PR 合併指標
 (平均合併 4小時 17分鐘) (30 天內合併 3 個 PR)

描述

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

貢獻者指南