Skip to content

black having trouble parsing if statements within fstring #4200

@BansonVuong

Description

@BansonVuong

Describe the bug

Black cannot parse fstrings with one line if statements

To Reproduce

For example, take this code:

a = f'{'1' if 1 else '2'}'

And run it with these arguments:

$ black file.py --target-version py39

The resulting error is:

error: cannot format cogs\black.py: Cannot parse: 4:11: a = f'{'1' if 1 else '2'}'

Oh no! 💥 💔 💥
1 file failed to reformat.

Expected behavior

A "cannot parse" error does not occur and the formatter is able to continue on parsing through the rest of the file.

Environment

  • Black's version: 24.1.1
  • OS and Python version: Windows/Python 3.12.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions