Skip to content Skip to sidebar Skip to footer

Is There A Python (3) Lint For Variable Names Such As 'len' (built-in Functions/reserved Words Etc)

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

Solution 1:

Following on from the answer of @Samuel Dion-Girardeau

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

  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"]

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

Solution 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

Post a Comment for "Is There A Python (3) Lint For Variable Names Such As 'len' (built-in Functions/reserved Words Etc)"