I am using GitHub Actions to ensure the correct formatting of my Python files, specifically by running the following job in my workflow YAML file:
command: isort --line-length 120 --profile black --check .
Simultaneously, I want to leverage VSCode's capability to automatically reformat .py files upon saving. My current settings.json setup for VSCode includes the following:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-python.black-formatter",
"[python]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
},
"isort.args": ["--line-length", "120", "--profile", "black"]
}
When I have Python files with imports like:
import pandas as pd
from tableau_api_lib.utils.querying import get_datasources_dataframe, get_workbooks_dataframe
The imports are reformatted upon saving into:
import pandas as pd
from tableau_api_lib.utils.querying import (
get_datasources_dataframe,
get_workbooks_dataframe,
)
I intend to keep the second import on one line unless the 120-character threshold is breached. How can I achieve this desired behavior with my current VSCode and isort configuration?
Note:
For this project I am using poetry for managing my library dependencies. Inside of the venv of my project - isort is added to the
pyproject.toml
file - running the following command:isort --line-length 120 --profile black .
Gives me the wanted sorting
I have explored the Force Grid Wrap solution which does not match with my requirements. My goal is to allow reformatting only when the line length exceeds a certain threshold (in this case, 120 characters).
You could create a custom profile in your
pyproject.toml
file.Then, you can remove the
isort.args
setting insetting.json
and set as the following cdoes: