Hi Friends, my name is Dawn Wages
and I am the Python Community Advocate here at Microsoft.
Today I'll show you some power user settings
when enabling your Python linter in VS Code.
The goal is for your developer experience to be intuitive, replicable, and shareable.
This is part of a larger series about Python
hinting and linting in VS Code.
Let's get started.
One of the most important parts of writing code is making sure your code is readable.
There are so many positive downstream effects of clean code. From it's ease to maintain and add features,
debug subtle programming errors
or
recognize uninitialized variables.
Some people call this code hygiene
and today we're going to talk about smelly Python code and how linting can help.
This all ties back to PEP8.
PEP "P" "E" "P" stands for Python Enhancement Proposal.
Eight denotes the number of the proposal and all proposals are enumerated.
Eight specifically goes into the opinions on what clean Python code looks like.
directly from the Python organization.
Although there are many linters and formatters you could use
All roads lead back to PEP8 any particular enhancements on top of PEP8 guidelines.
Here's the agenda for this short video.
We'll jump into how VS Code handles Python linters,
then we'll get into a real demo.
I'll show you how I enable pylint for a real project
enable settings in two different ways: from the settings panel and settings.json file
and then I'll point you in the right direction on how to create your own Python based language extension.
Let's Grokk how VS Code does linting through their extensions.
If you don't have a linter in your development requirements
there are lots of reasons for this
you can use the linter in VS Code exclusively
in your development environment and you do not need install it for your virtual environment.
The Extension Marketplace in VS Code is where you can expect to get the majority of your tooling for development.
There are tons of extensions in the Marketplace so I recommend giving it a cursory look
any time you're tackling a new project or workflow.
I'll actually share my screen in a few minutes to show you updating your settings.json
and we'll solve our first pylint problem together
But a pro-tip if linting a project
and your linter dependencies are in your requirements and you're using artifacts like .pylintrc
I recommend you add this line to your users settings
this way when you install your requirements and select your Python interpreter in VS Code
it will enable the same version you have pinned
and it will use your .pylintrc
that the rest of your team is using as well.
Today we're not going to be talking much about virtual environments.
Getting your head around virtual environments is a non trivial task for Pythonistas
Essentially, each project should
have its own virtual environment.
So when you pip install -r requirements.txt
or pip install the package name
the currently installed version is contained within that project.
When you start your project the first thing you'll likely do is activate your virtual environment
Note that I'm specifying on screen
the requirements hyphen dev file
because you're not going to run linter in production.
This is particularly a development tool that you'll use locally between your team to make sure your code is formatted consistently
and you're not proposing smelly code in your pull request.
You can also do this through the command palette by using CTRL + Shift + P
or CMD + Shift + P.
It will open a dropdown menu from the top of your editor and you can
click which Python you'd like to use.
If your virtual environment has already been created,
select the one in the virtual environment path;
otherwise, you can use the Microsoft store or Anaconda,
or other systems Pythons you may have that's specific to your project.
CMD+Shift+P is a really good command to remember.
The command palette in VS Code will get you almost anywhere you need to go.
There are many linter packages and many more not listed on this slide.
At the time of this video we have extensions supporting pylint,
flake8
mypy and ruff.
Ruff is one of the community contributed extensions.
If you'd like to see your favorite tools supported
use the Python based extension template.
The demo I'm going through is going to use pylint, but we have lots of extensions that support
code quality in Python.
We encourage the community to contribute and support Language Server extensions.
Here's an example of enabling pylint.
At the top Python linting enabled set to true is no longer needed with the new extension experience.
Below that, we're setting the arguments for pylint.
Args are always listed in brackets.
Here I'm supplying some arguments.
Reply in the comments with what you think these features flags are specifying.
Jumping into the code with this slide, but I'll show you how you can switch back and forth between settings.json
and the settings panel in the demo.
A little bit more code, you can see in this snippet
flake8 and ruff are using the same pattern
These would not all be in the same file unless we had all these linters enabled.
Bonus points if you can comment what these additional arguments are saying for flake8 and ruff.
So the last little bit before we jump in, here's some shortcuts in VS Code I use in my demo
that are fun and helpful to remember.
It always feels like I'm
gamifying my development experience by remembering the shortcuts that help navigate faster.
The problems tab is
directly with linting.
You'll want to first have the focus on the file that you like the linter to run on
and then you'll use Ctrl+Shift+M to open up the panel that will display any problems.
Don't forget to sort your imports with Shift+Alt+O or
Shift+Option+O
There's also a setting that you can enable called lintOnSave
which will check for linting errors every time your file saves.
If you want to supercharge this experience
you can enable
file dot autosave
so you can
frequently lint
and frequently save.
If you want to go deeper into pylint settings you can visit aka dot ms
forward slash linting hyphen pylint.
Now, we're jumping into the demo!
So I navigate to the extensions bar and search for pylint
I'm enabling the release version today
but as a good steward of the community I often like to enable the pre-release
so I can get the latest features and report bugs if I come across them.
As an employee of Microsoft, I file my bugs in the same place that the community does
because build out in the open.
And that's it!
First step is done.
Alright, so now we're going to solve our first linting error.
I'm working on a Django Wagtail application
and I navigate back to my files and I'm going to start with my views.
When I open and type Ctrl+Shift+M
the panel for all the problems open.
I want to start with the easy ones.
So I see that I have the wrong import order which is fixed by using
Shift+Alt+O
As I foreshadowed, you can update your linter settings in multiple places.
I'm going to show you how to do that next.
So I see these docstring conventions and I don't think I'm interested in letting them keep bugging me.
So open up the settings panel with Ctrl+,
and I type pylint into the search bar.
I click the checkbox to enable pylint
and now I'm going to use the args bar to disable some of the arguments
that are pesky to me.
When I went back to my file it auto updated and removed that issue.
Let's go back to the panel and do it in the workspace level,
because before I did it in the user level.
I removed C zero one one four from the user level
and added C zero one one four and C zero one one five
to the workspace level.
So these are just two of the three errors that we saw before.
You can see here
that it adds it to the settings dot json.
Now lets add C zero one one six, the last of the three errors.
I can go back
And its all synced up.
What I ended up doing was disabling
only for me which is the users level settings
and then I updated it for my workspace level settings.
It saves it to the files settings dot json
underneath the dot vscode directory.
And if I commit that directory
my teammates using VS Code will also have access to the same workspace settings.
There are so many available settings that I prefer to type them into the search bar
on the left
are commonly used categories.
and I recommend just going through some of these settings
and personalizing your environment. Its a really great step in becoming a power users in VS Code.
Here's a cheat sheet for the linter arguments for the settings dot json file.
For arguments or custom paths they are attributes on the linter extension name
all lowercase
This was a very high level introduction into linting in VS Code,
which was by design
this is a primer
Comment below if you want me to go any deeper into particular extensions
or particular workflows
or if you want me to set up a project
X Y Z. I'm excited to hear from you and excited to continue this hinting and linting series.
Now, if you're up to the task
I'll talk to you a little bit about how you can look into creating your own Python based extension.
I had an awesome time chatting with the Python in VS Code team on The Python Pulse livestream about this exact topic.
We've created the template that keeps you writing in more Python rather than TypeScript to create your extensions.
We had a great time and if you'd like to check out the link you can find it at aka dot ms
Python pulse create your own extension separated by hyphens
which will take you
to The Python Pulse starting at the time in the livestream
where we discussed the template.
You can also go straight to the docs
at code dot visual studio
dot com forward slash
api forward slash advance
hyphen topics
forward slash python hyphen extension hyphen template
This is the first video in the Python hinting and linting in VS Code series.
We have some great content line up for you
like sharing your development settings among your team big or small linters and Black formatter
Type Hinting in Python
a dynamically typed language and doing it in VS Code
and VS Code profiles
and more.
Don't forget to join us at Discord at aka dot ms
forward slash python hyphen discord
to talk all things snakes with us.