How I develop Python in VSCode

Why
Working on Javascript code in Visual Studio Code (VSCode) for the best part of last year got me use to all the good stuff. Autoformatting, linting etc. Coming back to a role where Python and SQL are the primary languages and I had the need to find out the best way to develop in Python.
Autoformatting
I’ll use this example valid Python code to demo my settings. This code isn’t nice. There is a lot of bad spacing, and some arguments being passed to a function of the wrong type.
import pandas as pd
def addcolumn(df:pd.DataFrame)->pd.DataFrame:
'''
Add a generic column to a dataframe
:param df: the source dataframe
:return: a dataframe with a new column populated with new value
'''
df['newcol']='new value'
return df
df = pd.DataFrame({'numbers':[1,2,3],'colors':['red','white','blue']})
print('Test add column')
print(addcolumn(df))
def addition(x:int,y:int)->int:
'''
Add two numbers function using mypy types
:param x: the first number
:param y: the second number
:return: the sum of the two arguments
'''
return x+y
print('Test typing')
print(addition(1,2))
print(addition('1','2'))
As Donald Rumsfield said “But there are also unknown unknowns—the ones we don’t know we don’t know”. And if you didn’t know, we don’t have to manually enforce styles by hitting space bar a lot of times.
To mirror the way I setup my VSCode, follow along.
Add this setting to you settings.json
{
"editor.formatOnSave": true
}
When you save this setting you will get this toaster modal:
Click Yes
and your terminal should load up a pip
install command.
If you have issues pip install
, that is a story for another time.
Now when we save the file, all that hideous formatting is gone. We don’t depend on ourselves to have to remember to hit Alt-Shift-F
to format, or even worse, have to space bar
our way through code to manually format it.
import pandas as pd
def addcolumn(df: pd.DataFrame) -> pd.DataFrame:
'''
Add a generic column to a dataframe
:param df: the source dataframe
:return: a dataframe with a new column populated with new value
'''
df['newcol'] = 'new value'
return df
df = pd.DataFrame({'numbers': [1, 2, 3], 'colors': ['red', 'white', 'blue']})
print('Test add column')
print(addcolumn(df))
def addition(x: int, y: int) -> int:
'''
Add two numbers function using mypy types
:param x: the first number
:param y: the second number
:return: the sum of the two arguments
'''
return x+y
print('Test typing')
print(addition(1, 2))
print(addition('1', '2'))
Typing in Python
These are type hints and not static typing.
Add these to your settings.json
{
"python.linting.mypyEnabled": true,
"python.linting.mypyPath": "~/python-venv/myvenv/bin/mypy"
}
You will need to install mypy
using pip install mypy
.
Ok, now this will not break the program or stop it from running. The issue with the code, you may notice is with calling the addition
function with str
arguments.
It doesn’t cause an issue, but you can imagine another case where using the wrong type as arguments would cause an error, and we want our development environment to let us know this.
We can run mypy
from the command line against a python file to see what it outputs.
# Windows
mypy.exe .\addition.py
The Windows one will return this:
This info will be surfaced right into VScode with the options we enabled in the settings.json
file.
Docstrings
Last but not least, using standard docstrings for functions helps to surface other info as well.