-
Notifications
You must be signed in to change notification settings - Fork 7
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
Revise isort config #106
Revise isort config #106
Conversation
I've taken the opportunity to remove the configs of Python-specific tools from the blueprints root |
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 didn't test it to be honest but It looks good to me :)
I'll apply this to our repos today or tomorrow, then I can give some feedback on whether this causes frequent code changes. I don't expect so since with |
Forgot to give feedback here; all good, no unintended consequences. Ready to merge as far as I'm concerned. |
Perfect, thanks a lot! You can merge anytine :) |
Adapt and improve config of
isort
(which groups and sorts imports) inpyproject.toml
.The examples below have been tested with Python 3.10.8, isort 5.12.0 and black 22.10.0.
Step 1: Black profile
Use the (newish) "black" profile option, which replaces some previously set options and notably implies the previously unset
include_trailing_comma
(which might change existing code slightly).Old:
New:
Step 2: NOQA
In one project, I stumbled upon a problematic case with imports that required multiple directive comments that caused the lines to be too long. Complete example:
The setup above with
profile = "black"
etc. produces this after runningisort
:and this after additionally running
black
:Neither versions walidates with
pylint
because of misplaced# pylint: disable
directives (which is ultimately a bug -- or at least a missing feature -- inblack
; there are open issues about this). The problem is that placing comments correctly is nontrivial, especially when directives of different tools with different placement/scoping rules are involved.Luckily,
isort
provides the optionmulti_line_output=7
, where 7 stands for7-noqa
, which doesn't change too long lines but simply adds a# NOQA
directive (must be uppercase...) if there is none already, which tells some linters (likeflake8
) to ignore the line:Running
isort
now yields:Unfortunately, a bug in
isort
causes# NOQA
not to be added to lines withas
imports (I've opened an issue), so we'll have to add it manually. Furthermore, whileblack
doesn't expand the firstnetCDF4
line because the trailing comment starts with# type: ignore
(see here), it still expands the next line, thereby still misplacing the# pylint
directive. We thus need to manually disableblack
for this line by wrapping it in# fmt: off
and# fmt: on
:(Note that while black now supports the inline directive
fmt: skip
to skip single line, it's unfortunately still buggy; see multiple open issues such as this. Also note that we wrap the whole "Third-party" block infmt: off/on
to make sure thatisort
does not misplace the directives while sorting the imports because it considers comments to belong to the next import line.)Running
isort
over this adapted code with the new config yields:which validates with both
flake8
andpylint
and whichblack
doesn't change any further.The final proposed
isort
config with a rather detailed comment: