python - 当存在 locals() 时,是否有可以忽略 ' assigned to but never used' 的 python 语法检查器?

标签 python vim syntax pyflakes

假设我有代码

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

最佳答案

没有。连pylint ,我所知道的最强大、最挑剔的 Python linter,它不够聪明,无法检测到这种情况。但如果是,它可能会提示您首先使用的是 locals()。 :)

另一方面,与 pyflakes 不同的是,pylint 确实支持 magic comments忽略特定问题。但我必须警告您,开箱即用的 pylint 非常挑剔(因此速度很慢),因此您需要提前花几分钟来削减它的 list of checks细化到您真正关心的事情。

一个ticket closed as wontfix在字符串格式化的特定情况下改进此行为。看来 pylint 开发人员不想将其实现为一项功能。

关于python - 当存在 locals() 时,是否有可以忽略 ' assigned to but never used' 的 python 语法检查器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14349239/

相关文章:

python - 通过 VIM 选择 Python 函数的最快方法是什么?

python - 如何将.h文件中的常量导入python模块

python - 无法安装 Python secret 包

vim - vim中向上/向下翻页时如何保持光标的垂直位置?

regex - Vim 从位置删除字符

javascript - 谁能解释一下 jCarousel 中的以下 JavaScript 代码?

javascript - 在 javascript 中的 Microsoft Edge 中出现语法错误

ruby - 在 ruby​​ 方法中使用问号

python - 如何使用线程获取数据并将数据写入同一个文本文件

python - 在python pandas中选择最接近某个时间的时间