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

Inserting newlines into triple-quoted f-strings on Python 3.12 #744

Open
rix0rrr opened this issue Apr 24, 2024 · 0 comments
Open

Inserting newlines into triple-quoted f-strings on Python 3.12 #744

rix0rrr opened this issue Apr 24, 2024 · 0 comments

Comments

@rix0rrr
Copy link

rix0rrr commented Apr 24, 2024


autopep8 seems to be adding newlines to what looks like an expression (but isn't!) inside triple-quoted f-strings.

It does not make the same mistake for triple-quoted non-f-strings, nor for regular-quoted f-strings.

I was originally confused myself, because the {{ ... }} below could be interpreted as a { ... } placeholder containing a literal dict expression, but the PEP seems to say that {{ should always be interpreted as a literal {... which make the characters that follow definitely not a valid expression and so not eligible for newline insertion.

Python Code

txt = f"""\
    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}
"""

txt2 = """\
    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}
"""

txt3 = "\
    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}\
"

No special configuration.

Command Line

$ autopep8 -i example.py
$ git diff
@@ -1,5 +1,6 @@
 txt = f"""\
-    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purpl>
+    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green',
+        'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}
 """

It added a newline inside the first triple-quoted f-string literal, but not inside the other ones.

Your Environment

  • Python version: Python 3.12.2
  • autopep8 version: autopep8 2.1.0 (pycodestyle: 2.11.1)
  • Platform: macOS

Additional proof

Proof that the reformatting changes the meaning of the string:

txt = f"""\
    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}
"""

txt2 = """\
    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}
"""

txt3 = f"\
    color_dict = {{'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}}\n\
"

#------------------------------------------------------------
# The rest is for mechanical proof

reference = "    color_dict = {'black': 'black', 'blue': 'blue', 'brown': 'brown', 'gray': 'gray', 'green': 'green', 'orange': 'orange', 'pink': 'pink', 'purple': 'purple', 'red': 'red', 'white': 'white', 'yellow': 'yellow'}\n"

# Normalize txt2 for the comparison below
txt2 = txt2.replace('{{', '{').replace('}}', '}')

equal = [x == reference for x in [txt, txt2, txt3]]
print(equal)
assert all(equal)
$ python3 example.py
[True, True, True]

$ autopep8 -i example.py

$ python3 example.py
[False, True, True]
Traceback (most recent call last):
  File "example.py", line 24, in <module>
    assert all(equal)
AssertionError
@rix0rrr rix0rrr changed the title Inserting newlines into triple-quoted f-strings Inserting newlines into triple-quoted f-strings on Python 3.12 Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant