psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.85k stars 2.45k forks source link

Unstable formatting: Comment is moved to lower line twice #3701

Open Zac-HD opened 1 year ago

Zac-HD commented 1 year ago

With current latest black, 23.3.0 (compiled: yes):

import black

code = """
assert some_long_name, (  # long __________________________ comment
        'long ___________________________________ string %s' % str(variable))
"""
black.format_file_contents(code, fast=False, mode=black.FileMode())

Specifically, this stabilizes at the second run:

assert some_long_name, (  # long __________________________ comment
        'long ___________________________________ string %s' % str(variable))

assert some_long_name, (
    "long ___________________________________ string %s"
    % str(variable)  # long __________________________ comment
)

assert some_long_name, "long ___________________________________ string %s" % str(
    variable
)  # long __________________________ comment
JelleZijlstra commented 4 months ago

3706 and #4384 reported additional cases of what looks like the same bug.