-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add isort to sort imports alphabetically #745
Conversation
[tool.isort] | ||
profile = "black" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used isort
and quite like it, but I've found different isort
versions to apply the sort styles rather inconsistently (specifically the standard lib/firstparty/thirdparty sections, see https://pycqa.github.io/isort/#custom-sections-and-ordering). Does this "black" profile keep things simple?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this "black" profile keep things simple?
No, profile = "black"
is equivalent to the following settings:
multi_line_output: 3
include_trailing_comma: True
force_grid_wrap: 0
use_parentheses: True
ensure_newline_before_comments: True
line_length: 88
As for section orders, the default order (https://pycqa.github.io/isort/docs/configuration/options/#sections) is:
('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER')
Do you mean the default values change in different isort
versions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there was a point where they separated well known third-party libaries (e.g. numpy and pandas) from other third-party libraries. Maybe that has changed now with newer isort
versions, but I'm not too sure.
Do you think we should add other configuration options? I'm looking at xarray's at pydata/xarray#4518 and they have some extra ones besides black
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will look at these options later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of our examples import some of the four packages: pygmt, numpy, pandas and xarray.
Currently, isort
sorts them like
import numpy as np
import pandas as pd
import xarray as xr
import pygmt
because numpy, pandas, and xarray are known third-party packages, and pygmt is first-party package (not sure if I understand it correctly).
I don't think I like it. We can add options:
known_third_party = "pygmt"
force_to_top = "pygmt
so that they will be sorted like:
import pygmt
import numpy as np
import pandas as pd
import xarray as xr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After reading through all the isort
options, I added the following ones to pyproject.toml
, similar to pydata/xarray#4518:
skip_gitignore = true
force_to_top = "pygmt"
known_third_party = "pygmt"
the only exception is default_section = THIRDPARTY
, as I don't understand what it is, and adding it doesn't change the codes at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to remove force_to_top
, otherwise pylint will complain.
Description of proposed changes
This PR adds
isort
to automatically sort imports.Notes:
profile = "black"
is added topyproject.toml
for compatibility betweenblack
andisort
, which requires isort>=5pylint
also depends onisort
. Thus when we install pylint,isort
is already installed.References:
Reminders
make format
andmake check
to make sure the code follows the style guide.doc/api/index.rst
.Notes
/format
in the first line of a comment to lint the code automatically