Skip to content
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

Black fails to format complicated f-string #4126

Closed
georgepittock opened this issue Dec 23, 2023 · 2 comments
Closed

Black fails to format complicated f-string #4126

georgepittock opened this issue Dec 23, 2023 · 2 comments
Labels
T: bug Something isn't working

Comments

@georgepittock
Copy link

georgepittock commented Dec 23, 2023

Black fails to format complicated f-string

To Reproduce

Black Playground

SUBSCRIPTS = {i: chr(0x2080 + i) for i in range(10)}

def cant_format(row):
    return " + ".join(
                f"{coefficient if coefficient != 1 else ""}x{''.join(SUBSCRIPTS[int(digit)] for digit in str(i+1))}"
                for i, coefficient in enumerate(row)
            )


cant_format([1, 2])

And run it with these arguments:

$ black simplex\utils.py

The resulting error is:

> [cannot format file.py: INTERNAL ERROR: ...](error: cannot format: simplex\utils.py: INTERNAL ERROR: Black produced invalid code: f-string: expecting '}' (<unknown>, line 6). Please report a bug on https://github.com/psf/black/issues.  This invalid output might be helpful: Temp\blk_k8cady1s.log)

Log File Output

  File "src\black\__init__.py", line 1476, in assert_equivalent
  File "src\black\parsing.py", line 140, in parse_ast
SUBSCRIPTS = {i: chr(0x2080 + i) for i in range(10)}


def cant_format(row):
    return " + ".join(
        f"{coefficient if coefficient != 1 else "
        "}x{''.join(SUBSCRIPTS[int(digit)] for digit in str(i+1))}"
        for i, coefficient in enumerate(row)
    )


cant_format([1, 2])

The error is due to the }being carried to the next line. I would expect the whole f-expression to be on one line.

If single quotes are used in the first statement after the else, then black correctly formats this i.e. the below works fine.

SUBSCRIPTS = {i: chr(0x2080 + i) for i in range(10)}

def cant_format(row):
    return " + ".join(
                f"{coefficient if coefficient != 1 else ''}x{''.join(SUBSCRIPTS[int(digit)] for digit in str(i+1))}"
                for i, coefficient in enumerate(row)
            )


cant_format([1, 2])

Expected behaviour

Black should correctly format the code, I would expect something very similar to the following:

SUBSCRIPTS = {i: chr(0x2080 + i) for i in range(10)}


def cant_format(row):
    return " + ".join(
        f"{coefficient if coefficient != 1 else ''}x{''.join(SUBSCRIPTS[int(digit)] for digit in str(i+1))}"
        for i, coefficient in enumerate(row)
    )


cant_format([1, 2])

Environment

>black --version
black, 23.12.1 (compiled: yes)
Python (CPython) 3.12.0
@georgepittock georgepittock added the T: bug Something isn't working label Dec 23, 2023
@Lilneo786
Copy link

This should work:

SUBSCRIPTS = {i: chr(0x2080 + i) for i in range(10)}

def cant_format(row):
    return " + ".join(
        [
            f"{coefficient if coefficient != 1 else ''}x{''.join(SUBSCRIPTS[int(digit)] for digit in str(i+1))}"
            for i, coefficient in enumerate(row)
        ]
    )

print(cant_format([1, 2]))

@hauntsaninja
Copy link
Collaborator

Thanks, duplicate of #3746

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants