Should we bump the recommended black (python formatter) version?

The LLVM coding standards say about python code (emphasis mine):

For consistency and to limit churn, code should be automatically formatted with the black utility, which is PEP 8 compliant. Use its default rules. For example, avoid specifying --line-length even though it does not default to 80. The default rules can change between major versions of black. In order to avoid unnecessary churn in the formatting rules, we currently use black version 23.x in LLVM.

The (not particularly new) CVE-2024-21503 (a Regular Expression Denial of Service vulnerability) affects this recommended version of black. I’ve stumbled over this because following LLVM’s policy of requiring this black version triggers a dependabot alert in Github projects that have it enabled.
I’m not sure if this CVE is practically relevant for us and if the potential harm is worth the potential churn with switching to a new black version, but I think it at least warrants a discussion here:

  1. Under which conditions would we bump the recommended black version?
  2. Does this CVE (and/or the related dependabot alerts) fall under these conditions?
  3. If so, should we bump it to the current 26.x version family?

I only found this comment from 2023 about the version bump policy in the RFC that introduced black as recommended formatter:

I think the easiest is that we recommend black 23.x for now and maybe bump that next year if we feel there is a need for it.

@tobiashieta for visibility

I think we can bump the black version, but I am more worried about how much changes it would introduce when we bump rather then CVE.

Can you run the latest version of black over our Python files and post the diffstats? Will it be changing a lot of files?

cc @JDevlieghere @boomanaiden154-1

Running black 26.1.0 at top-level in the llvm-project monorepo gives me:

982 files reformatted, 1857 files left unchanged, 4 files failed to reformat.

So there are a lot of changes:

$ git diff --stat
[...]
982 files changed, 3523 insertions(+), 4173 deletions(-)

Would it make sense to bump the version but to not reformat everything, and only let commits that change something reformat what they touch? Or is this just too much churn?

That’s unfortunate. Do you know what has been changed here?

If we upgrade and not do the reformat it will fail on every single PR that touches a Python file. That’s not great.

That being said, when I run black 23.12.1 in the same way, I already get changes across quite a few files:

206 files reformatted, 2633 files left unchanged, 4 files failed to reformat.
$ git diff --stat
[...]
206 files changed, 1999 insertions(+), 1469 deletions(-)

Those seem to be just unformatted (or incompletely formatted) files.

If I first run black 23.12.1 and then 26.1, I get this for the second run:

874 files reformatted, 1965 files left unchanged, 4 files failed to reformat.
$ git diff --stat
[...]
874 files changed, 1611 insertions(+), 2853 deletions(-)

Skimming through the changes between those black runs, block comment formatting changed a couple of times, e.g.:

-    print(
-        f"""
+    print(f"""

Also, parentheses around some tuple assignments were removed:

-        (level, header) = parse_line(line)
+        level, header = parse_line(line)

and

-    for (includer, includees) in includes:
+    for includer, includees in includes:

But that’s far from a complete list, there are also added and removed newlines and parentheses for example.

I am completely unworried about this CVE. We do not use black in a context where a DoS attack would be relevant.

However, I would be in favor of bumping the version (or maybe even switching to ruff for increased speed), even if it involved mass reformatting. I think the major blocker for such a change would be finding someone to drive it and ensuring we do not (too) adversely impact downstream forks.

We’re also not really going to be able to enforce that things stay clean without whole-repo post-submit buildbot coverage. If we care that our code is consistently formatted, it’s something that we should set up.

1 Like

+1 to enforcement via CI if we’re doing this. Not to digress from the current topic, but one of my biggest peeves with our project is the fact that clang-format’s LLVM style doesn’t have the bracing rule for single line conditions. It’s very easy to miss as a patch gets refactored and I generally rely on the tool to make sure I’m doing the right thing. Ideally, anything we’re serious about would have some kind of automatic checking.

I’m otherwise neutral as to which python formatter we use. I just want us to be consistent and not to worry about formatting very often in review (e.g. I don’t have to mention formatting if a bot does it first, which is largely the case now).

Just to clarify, my bringing up switching to ruff was specific to ruff given it is aimed at being black but faster. There are some minor behavioral differences, but otherwise it produces identically formatted code. I don’t think switching to a completely different formatter would be worth the churn even if one was tangibly better in some way. Speed is not a big deal in precommit CI where we only need to run it on a handful of files, but if we want to run it on the entire repository at a reasonable pace postcommit, a fast formatter would be helpful.

sorry for the lack of specificity in my response. I should have said “I’m otherwise neutral to the version of the formatter”

The majority of our Python code are tests. I did the same thing as @ritter-x2a and found that we have currently ~40 non-compliant files, and it would affect roughly 600 files to switch from 23 to 26:

587 files changed, 701 insertions(+), 1321 deletions(-)

Most of the changes were removal of newlines and the parenthesis around tuples, nothing unreasonable. I have no concerns about moving to 26.

If we’re looking to update Black, I think we could consider switching directly to Ruff. Ruff claims to be largely compatible with Black’s formatting behavior while being hundreds of times faster than Black.

1 Like