0

I accidentally ran Black without --line-length. My company uses a line-length of 110. Now all of the code has been formatted to 80. How can I undo this? Running black --line-length 110 has no effect. Cheers.

Minsky
  • 473
  • 2
  • 7
  • 18
  • Heads up that if your company expects a specific line-length, that should be in [black's configuration file](https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#configuration-via-a-file) to begin with. – MisterMiyagi Oct 25 '21 at 13:03

1 Answers1

2

You can revert the change using your version source control (you use a VCS, right ?). Theoretically, if you're not you could revert by removing the magic comma black added and launch black again. Ie if the new code is:

def function(
    a,
    b,
):
    pass

Then:

def function(
    a,
    b
):
    pass

Would be reformatted properly if you relaunch black with -l 110.

Pierre.Sassoulas
  • 3,733
  • 3
  • 33
  • 48
  • Thanks, I'm afraid I noticed this too late to revert cleanly with Git. The formatting change was made before substantive code changes which would all be undone in the process of reverting. Manually removing the magic comma seems to work in some cases, but not all. Plus it's still manual. – Minsky Oct 25 '21 at 13:06
  • You can revert the formatting commit only, and git should create a conflict when there were other changes since. Resolving everything could take some time though. – Pierre.Sassoulas Oct 25 '21 at 13:12
  • 1
    Sorry, I didn't explain that well. There is no separate formatting commit. – Minsky Oct 25 '21 at 13:16
  • You could create one by interactively rebasing before this commit and applying `black -l 88` and committing a formatting commit, before the commit with mixed formatting and change then resolve conflict by taking the mixed version all the time see https://stackoverflow.com/questions/25576415/what-is-the-precise-meaning-of-ours-and-theirs-in-git – Pierre.Sassoulas Oct 25 '21 at 15:01
  • 1
    Thanks Pierre. I appreciate it. This was solved by manually comparing the files after running black without arguments. I still find it crazy that there is no functionality in linting/formatting tools to undo line wrapping when it's still just a logical line. Maybe I have missed something. – Minsky Oct 25 '21 at 17:32