Skip to content

COM812 breaks conditionals inside multi-line f-strings #8556

Description

@parafoxia

Synopsis

The COM812 rule is capable of breaking conditional statements within triple f-strings by converting the else-statement into a tuple (or a tuple containing the original tuple).

Of course you can use # noqa: COM812, though I think that linters should be incapable of breaking code.

MRE

(This example assumes the line should be split across multiple lines.)

Original mre.py:

x = f"""This is a test. {
    "Another sentence."
    if True else
    "Alternative route!"
}"""

print(x)
$ py test.py
This is a test. Another sentence.

$ ruff check mre.py
mre.py:4:25: COM812 [*] Trailing comma missing
Found 1 error.
[*] 1 fixable with the `--fix` option.

"Fixed" mre.py

x = f"""This is a test. {
    "Another sentence."
    if True else
    "Alternative route!",
}"""

print(x)
$ py test.py
This is a test. ('Another sentence.',)

$ ruff check mre.py
(Exit code 0)

Environment

  • Ruff version: 0.1.3
  • Python version: 3.9.11
  • OS: macOS Sonoma 14.0

Activity

  1. added
    bug
    on Nov 9, 2023
  2. self-assigned this
    on Nov 9, 2023
  3. charliermarsh commented on Nov 9, 2023

    @charliermarsh
    Member

    Thanks, I'll take a look at this!

  4. dhruvmanila commented on Nov 9, 2023

    @dhruvmanila
    Member

    I think it must be considering the curly braces as ContextType::Dict. It needs to take into account the new f-string tokens and skip if inside a f-string.

  5. charliermarsh commented on Nov 9, 2023

    @charliermarsh
    Member

    @dhruvmanila - Feel free to fix if you're there, I won't get to it for a few hours.

  6. charliermarsh commented on Nov 9, 2023

    @charliermarsh
    Member

    Taking a look now.

  7. added a commit that references this issue on Nov 10, 2023
    d5606b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions