-
Notifications
You must be signed in to change notification settings - Fork 480
Autoformat imports with isort #3881
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
base: main
Are you sure you want to change the base?
Conversation
A big downside is that this makes merging open PRs a lot harder. Additionally we need to set up pre-commit or something like it to ensure things keep the expected formatting. On the one hand I'd be highly in favor of reformatting, but I think we need a broad concensus among the regular committers. |
we have a There's the issue of open PRs becoming unmergeable that Just mentioned, but it could be worked around by running the same automatic tools on those PR branches to fix the merge conflicts I presume. Running black should be safe, but for isort we need to make sure that the reodering doesn't mess up the functionality for module imports that have side effects, e.g. importing fontTools.subset adds the subset methods to table classes, or importing otTables defines the otData classes etc. |
I think you also need to tell isort to follow black's profile otherwise they'll step over each other: https://pycqa.github.io/isort/docs/configuration/black_compatibility.html |
@madig added these to ufoLib2's pyproject.toml, have a look |
Yes, we did. I ran black after isort again only to clean up some things that isort formats differently, which black would correct then again, e.g. isort’s default style for wrapping long import lines is from fontTools.cffLib import (TopDictIndex, buildDefaults, buildOrder,
privateDictOperators, topDictOperators) while black does from fontTools.cffLib import (
TopDictIndex,
buildDefaults,
buildOrder,
privateDictOperators,
topDictOperators,
) I see now that I could have avoided that by using the isort black profile. So this is really only about adding isort. |
turns out I was wrong, I thought black's --diff flag which we use in tox -e lint would only run black on the diff (i.e. added/modified) but it actually means "show the unified diff in the console output". If we go with isort, we should add something like |
If you're going to change tools around, I highly recommend replacing black and isort with just ruff. It is just better and faster. I also see the existing PR problem pointed out by Just, but unless someone goes through the trouble of reviewing and merging all existing PRs, I don't see how to make it go away. The only alternative I can think of would be to not autoformat. I'd see it as a change for all future PRs. Oh, also, don't forget to add the formatting commit to https://github.com/fonttools/fonttools/blob/main/.git-blame-ignore-revs to keep the Blame view working well. |
+1 to just switch to ruff (I just realized that's what my vscode is also using to autoformat on save) |
technically ruff is not 100% identical to black (here's the list of known deviation https://docs.astral.sh/ruff/formatter/black/) but it's close enough for me not to even realise I was using that instead of black |
we are already autoformatting with black, as Jens said, this PR only adds isort (PR title should be renamed for clarity). He ran black after isort because he had not set up isort to output black-compatible output. Running isort only on future PRs for newly added or modified files is a bit impractical. The GH action would have to first identify all the newly added or changed files (e.g. using https://github.com/tj-actions/changed-files) and then run isort on those only. |
@@ -208,7 +207,7 @@ def tostring( | |||
# Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] | |||
# Here we reversed the pattern to match only the invalid characters. | |||
_invalid_xml_string = re.compile( | |||
"[\u0000-\u0008\u000B-\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE-\uFFFF]" | |||
"[\u0000-\u0008\u000b-\u000c\u000e-\u001f\ud800-\udfff\ufffe-\uffff]" |
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.
Is there away to disable this hex lower casing? I find \udfff
to be less readable than \uDFFF
.
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.
ugh.. looks like it's a new thing they added with psf/black#2916
and black being black, you can't disable it :(
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.
ruff does the same so that's the canonical way we have to get used to i'm afraid
astral-sh/ruff#13859
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.
😢
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.
they probably wanted to make it consistent with the repr()
of these strings which is all lowercase. But it's inconsistent with the way they make hex digits all uppercase:
0xffff # == black ==> 0xFFFF
"\uFFFF" # == black ==> "\uffff"
Here's the ruff version of this PR: main...format-everything-mk2. |
Automatic application of coding styles makes my life easier a lot.
Split out from #3879, this applies import sorting for everything. After that I've run black again to iron out any formatting differences (we run black already).
I've applied both tools with their default settings: