+
Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Autoformat imports with isort #3881

wants to merge 2 commits into from

Conversation

jenskutilek
Copy link
Collaborator

@jenskutilek jenskutilek commented Jul 4, 2025

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:

$ isort .
$ black .

@justvanrossum
Copy link
Collaborator

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.

@anthrotype
Copy link
Member

we have a tox -e lint task that is run by the CI on PRs which already runs black --check --diff to check that (new) things are properly formatted, so we could do the same with isort I guess? Although this probably would only apply to the newly added/modified files. This PR on the other hand is applying these tools to the entire codebase, some very old files which haven't been touched in ages. By the way, didn't we run black on all files before setting up the CI job? I think we did, but of course black itself has changed..

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.

@anthrotype
Copy link
Member

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

@anthrotype
Copy link
Member

@jenskutilek
Copy link
Collaborator Author

jenskutilek commented Jul 4, 2025

By the way, didn't we run black on all files before setting up the CI job? I think we did, but of course black itself has changed..

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.

@anthrotype
Copy link
Member

Although this probably would only apply to the newly added/modified files.

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 isort --profile black --skip-gitignore --check-only --diff . to the [testenv:lint] section of tox.ini so that it gets run on PRs, similar to the exiting black --check --diff .

@madig
Copy link
Collaborator

madig commented Jul 4, 2025

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.

@anthrotype
Copy link
Member

I highly recommend replacing black and isort with just ruff. It is just better and faster.

+1 to just switch to ruff (I just realized that's what my vscode is also using to autoformat on save)

@anthrotype
Copy link
Member

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

@anthrotype
Copy link
Member

The only alternative I can think of would be to not autoformat. I'd see it as a change for all future PRs.

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]"
Copy link
Collaborator

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.

Copy link
Member

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 :(

Copy link
Member

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

Copy link
Member

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"

@jenskutilek jenskutilek changed the title Autoformat with isort and black Autoformat with isort Jul 4, 2025
@jenskutilek jenskutilek changed the title Autoformat with isort Autoformat imports with isort Jul 4, 2025
@madig
Copy link
Collaborator

madig commented Jul 4, 2025

Here's the ruff version of this PR: main...format-everything-mk2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载