是否有Python(3)绒毛用于变量名(例如"len")(内置函数/保留字等) [英] Is there a Python (3) lint for variable names such as 'len' (built-in functions/reserved words etc)

查看:128
本文介绍了是否有Python(3)绒毛用于变量名(例如"len")(内置函数/保留字等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被这个一个(使用len在方法调用的参数中),然后定义一个列表,并对其进行len处理,得到:

I got stung by this one (using len in an argument on a method call), then defining a list, and doing len on it, yielding:

def fun(len):
  a = []
  ...
  len(a)

>>>TypeError: 'int' object is not callable

是否存在用于VS Code IDE的Python3棉绒,您可以将其配置为报告不是保留字/内置函数的变量?或一般来说是掩盖/覆盖. 我没想到这种行为.

Is there Python3 lint for the VS Code IDE, that you can configure to report on variables not being reserved words/built-in functions? Or masking/overwriting in general. I didn't expect that behaviour.

反思时,我知道它是Python的一个功能,您可以将函数作为参数传递,因此lenlen()的双重语法.但这肯定让我感到惊讶!

On reflection I am aware it's a feature of Python that you can pass functions as arguments, hence the dual syntax of len and len(). But it certainly caught me by surprise!

棉绒似乎报告未使用的变量之类的东西.

Lint seems to report things like unused variables.

似乎不一致,它也没有提供现成的名称掩码报告.

It seems inconsistent it doesn't provide name mask reporting out of the box too.

如果这是可行的,请问有人可以建议如何在VS Code中进行设置吗?

If this is feasible, can someone please advise how to set it up in VS Code?

环境:

  • VS代码:1.23.1版
  • Python 3.6.5
  • Python扩展2018.4.0
  • Microsoft Windows Server 2012 RC2.

推荐答案

紧接着@Samuel Dion-Girardeau的回答

Following on from the answer of @Samuel Dion-Girardeau

  1. 似乎VS Code没有直接使用这些代码. 相反,它使用更具描述性的键W0622 . >这里. redefined-builtin在这种情况下.
  2. 在我的VS Code设置(文件">首选项">设置")中,我看到:
    2.1 python.linting.pylintUseMinimalCheckers": true
    2.2 "python.linting.pylintArgs": []
  1. It seems VS Code doesn't use these codes directly. Rather it defines W0622 with more descriptive key here. redefined-builtin in this case.
  2. In my VS Code settings (File>Preferences>Settings), I see:
    2.1 python.linting.pylintUseMinimalCheckers": true
    2.2 "python.linting.pylintArgs": []

2.1等同于此请参见此处

   --disable=all --enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode

在该同一个地方

如果您在pylintArgs中指定一个值或使用Pylint配置 文件,然后将pylintUseMinimalCheckers隐式设置为false.

If you specify a value in pylintArgs or use a Pylint configuration file then pylintUseMinimalCheckers is implicitly set to false.

  1. 因此,我需要附加以下内容:
    3.1 redefined-builtin"python.linting.pylintArgs": []
    --enable部分 所以我们最后得到:
    3.2 python.linting.pylintUseMinimalCheckers": false
    (推断这部分不是必需的...)
    3.3 "python.linting.pylintArgs": [ "--disable=all", "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,redefined-builtin"]
  1. So it follows I need to append:
    3.1 redefined-builtin to the --enable part of "python.linting.pylintArgs": []
    So we end up with:
    3.2 python.linting.pylintUseMinimalCheckers": false
    (It infers this part is not required...)
    3.3 "python.linting.pylintArgs": [ "--disable=all", "--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,redefined-builtin"]

(我从DEFAULT USER SETTINGS复制并粘贴到USER_SETTINGS).

(I copy and pasted from DEFAULT USER SETTINGS into USER_SETTINGS).

然后在此处应用更改,请确保在键/值对之间添加逗号.

Then applied the changes there, being sure to add a comma between the key/value pairs.

脚注: 我最近也在亚马逊实例上进行了设置.

Footnote: I was recently setting this up on an Amazon instance too.

我忘记了您也需要运行pip install pylint 参见此处.

I forgot you also need to run pip install pylint too See here.

这篇关于是否有Python(3)绒毛用于变量名(例如"len")(内置函数/保留字等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆