How to highlight unused Python variables in VS Code

Fri 09 October 2020, by Matthew Segal
Category: Programming

I make a lot of stupid mistakes when I'm working on Python code. I tend to:

  • make typos in variable names
  • accidently delete a variable that's used somewhere else
  • leave unused variables lying around when they should be deleted

It's easy to accidentally create code like in the image below, where you have unused variables (y, z, q) and references to variables that aren't defined yet (z).

foo-before

You'll catch these issues when you eventually try to run this function, but it's best to be able to spot them instantly. I want my editor to show me something that looks like this:

foo-after

Here you can see that the vars y, z and q are greyed out, to show that they're not used. The undefined reference to z is highlighted with a yellow squiggle. This kind of instant visual feedback means you can write better code, faster and with less mental overhead.

Having your editor highlight unused variables can also help you remove clutter. For example, it's common to have old imports that aren't used anymore, like copy and requests in this script:

imports-before

It's often hard to see what imports are being used just by looking, which is why it's nice to have your editor tell you:

imports-after

You'll also note that there is an error in my import statement. import copy from copy isn't valid Python. This was an unintentional mistake in my example code that VS Code caught for me.

Setting this up with VS Code

You can get these variable highlights in VS Code very easily by installing PyLance, and alternative "language server" for VS Code. A language server is a tool, which runs alongside the editor, that does static analysis of your code.

To get this language server, go into your extensions tab in VS Code, search for "pylance", install it, and then you'll see this popup:

server-prompt

Click "Yes, and reload".

Alternatives

PyCharm does this kind of static analysis out of the box. I don't like PyCharm quite so much as VS Code, but it's a decent editor and many people swear by it. You can also get this feature by enabling a Python linter in VS Code like flake8, pylint or autopep8. I don't like twiddling with linters, but again other people enjoy using them.

Next steps

If you're looking for more Python productivity helpers, then check out my blog post on the Black auto-formatter.

If you have any feedback or questions email me at [email protected]