问题
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
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.
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.
If this is feasible, can someone please advise how to set it up in VS Code?
Environment:
- VS Code: Version 1.23.1
- Python 3.6.5
- Python Extension 2018.4.0
- Microsoft Windows Server 2012 RC2.
回答1:
Following on from the answer of @Samuel Dion-Girardeau
- It seems VS Code doesn't use these codes directly.
Rather it defines W0622 with more descriptive key here.
redefined-builtin
in this case. - In my VS Code settings (File>Preferences>Settings), I see:
2.1python.linting.pylintUseMinimalCheckers": true
2.2"python.linting.pylintArgs": []
2.1 equates to this See here
--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
In that same place
If you specify a value in pylintArgs or use a Pylint configuration file then pylintUseMinimalCheckers is implicitly set to false.
- So it follows I need to append:
3.1redefined-builtin
to the--enable
part of"python.linting.pylintArgs": []
So we end up with:
3.2python.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"]
(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.
I forgot you also need to run pip install pylint
too
See here.
回答2:
You can use Pylint to check that for you.
It has a dedicated warning code, W0622
, for "Redefining built-in" (see list of all error codes)
To set it up in Visual Studio Code, you can follow the official guide: Linting Python in VS Code
来源:https://stackoverflow.com/questions/50609715/is-there-a-python-3-lint-for-variable-names-such-as-len-built-in-functions