Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Error formatting f-string with # fmt: off inside a pair of brackets #4511

Open
Azureblade3808 opened this issue Nov 14, 2024 · 1 comment
Open
Labels
F: fmtoff fmt: off implementation T: bug Something isn't working

Comments

@Azureblade3808
Copy link

Describe the bug

If there is a f-string (single-line or multi-line) following a # fmt: off (with or without a pairing # fmt: on) and both the f-string and # fmt: off are inside a pair of brackets ((), [], {}), the code cannot be formatted.

The error reported is something like:

error: cannot format ???.py: {' ', 'r', 'f', 'o', ':', 'm', 't', '#', '\n'} is NOT a subset of {'U', 'r', 'f', 'u', 'F', 'R', 'b', 'B'}.

To Reproduce

Sample:

(
# fmt: off
f"""
"""
# fmt: on
)

Expected behavior

No error is reported and the code is formatted as if the f-string is a normal string.

Environment

  • Black's version: 24.10.0
  • OS and Python version: Windows and Linux / Python 3.13.0

Additional context

None

@Azureblade3808 Azureblade3808 added the T: bug Something isn't working label Nov 14, 2024
@hauntsaninja hauntsaninja added the F: fmtoff fmt: off implementation label Nov 14, 2024
@MeGaGiGaGon
Copy link
Collaborator

This is fixed by #3978, though with interesting results. I'd instinctively say that # fmt: skip/off/on comments should never be moved by black, though I don't know if that's every been formalized/is possible thanks to issues with indented comments.

Output with #3978:

(
    # fmt: off
f"""
"""
    # fmt: on
)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
F: fmtoff fmt: off implementation T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants