3

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.
JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • Can someone please explain why people are suggesting close this? – JGFMK May 30 '18 at 16:52
  • `len` is the name of a function and you are using the argument for what `len` ​​will now be of the type of the argument you enter fun, which as we see is integer, is a bad programming practice to use reserved names as variable names. – eyllanesc May 30 '18 at 16:53
  • Right. I know that now... But I'm asking is there a lint checker that will flag that up in case you don't know these things as a Python novice. – JGFMK May 30 '18 at 16:54
  • Sure I know about the function. But I didn't expect Python to silently overwrite it when I declared the variable, I'd expect some IDE feedback warnings. – JGFMK May 30 '18 at 16:55
  • It's odd, the existing linting is 'up tight' about unused vars, why can't it be same for 'reserved words'? (https://www.python.org/dev/peps/pep-0008/) A distinct lack of continuity – JGFMK May 30 '18 at 16:59
  • 1
    is that it's not really a reserved word, it's the name of a function. – eyllanesc May 30 '18 at 17:00
  • Sure - semantics... but built in functions would be what I'd consider reserved too. When you add more modules, you'd expect extra reserved/keywords to exist.. – JGFMK May 30 '18 at 17:02
  • I guess I'd rephrase didn't expect Python to silently overwrite... , to more accurately say I'd expect the linting built into the IDE to flag up issues. – JGFMK May 30 '18 at 17:05
  • @JGFMK Not sure why this is being downvoted/flagged that much. Yes you are redefining a built-in in the scope of your function, but that seems to be what your question is about... See my answer on how to automatically warn you about such cases using Pylint. – Samuel Dion-Girardeau May 30 '18 at 17:29
  • Your title is probably offending people (no idea why) by having a trivial mistake. If you just ask *lint for marking reserved keywords in VS Code* it would have been better in my limited experience here. – percusse May 30 '18 at 17:32

2 Answers2

2

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.

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • 1
    That was worth doing. Found I had used 'id' as a variable inadvertently too. – JGFMK May 30 '18 at 18:47
  • It also seems VSCode tries to give you a visual clue making text yellow when it's a function (such as built-ins https://docs.python.org/3/library/functions.html too).- Had never put two and two together. Doh! – JGFMK May 30 '18 at 18:59
  • I also had a module called globals, that I renamed too and refactored. W0622 is definitely a useful one to have. Wish it was a default. Really useful when learning ropes with Python. – JGFMK May 30 '18 at 19:12
1

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

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
Samuel Dion-Girardeau
  • 2,790
  • 1
  • 29
  • 37