3

假设我有代码

def foo():
    bar = 1
    wibble = 3
    return locals()

我当前的语法检查器(带有 syntastic.vim 的 flake8)将在两个变量上抛出“分配给但从未使用”错误。然而 locals() 暗示了一些事情,如果不是明确的,它们实际上正在被使用。

def foo():
    bar = 1
    wibble = 3 # <-- I still want this to throw as it is definitely not being used
    return bar

是否有任何 python 检查器或自定义设置将是 locals() 感知和宽松的?

编辑:

这是 vim/syntastic/flake8 的快速而肮脏的解决方案,它将抑制 .vimrc 中的警告

"Ignore unused variable warnings whenever locals() is being used in a file                                                              
function! LocalsWarningSuppress()                                              
    if ( search("locals()",'nw') > 0)                                          
        let g:syntastic_python_checker='flake8 --ignore=W806'                  
    else                                                                       
        let g:syntastic_python_checker='flake8'                                
    endif                                                                      
endfunction 

au BufWritePre **/(filter_pattern)*py call LocalsWarningSuppress()                 
4

1 回答 1

2

不,即使pylint是我所知道的最强大和最挑剔的 Python linter 也不够聪明,无法检测到这种情况。但如果是这样,它可能会抱怨你一开始就在使用locals()。:)

另一方面,与 pyflakes 不同,pylint 确实支持魔术注释以忽略特定问题。但我必须警告您,pylint 开箱即用非常挑剔(因此很慢),因此您需要提前几分钟将其检查清单缩减为您真正关心的事情。

在字符串格式化的特定情况下,有一张作为 wontfix 关闭的票用于改进此行为。pylint 开发人员似乎不想将其作为一项功能来实现。

于 2013-01-16T00:56:37.590 回答