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 changes f-string into string even though there is a substitution in it. #1469

Closed
Jackenmen opened this issue May 31, 2020 · 3 comments · Fixed by #2287
Closed

Black changes f-string into string even though there is a substitution in it. #1469

Jackenmen opened this issue May 31, 2020 · 3 comments · Fixed by #2287
Labels
C: invalid code Black destroyed a valid Python file F: strings Related to our handling of strings T: bug Something isn't working

Comments

@Jackenmen
Copy link
Contributor

Jackenmen commented May 31, 2020

Describe the bug:
Black changes f-string into string even though there is a substitution in it.

To Reproduce:

  1. Put this into a file:
temp_msg = (
    f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
    f"{balance: <{bal_len + 5}} "
    f"<<{author.display_name}>>\n"
)
  1. Run Black on it with no arguments:
  2. See error
error: cannot format test.py: INTERNAL ERROR: Black produced code that is not equivalent to the source.  Please report a bug on https://github.com/psf/black/issues. This diff might be helpful: C:\Users\Jakub\AppData\Local\Temp\blk_08urbts5.log

Contents of the log file:

--- src
+++ dst
@@ -85,60 +85,11 @@
                     )  # /Constant
                 )  # /JoinedStr
             )  # /FormattedValue
             Constant(
               value=
-                '',  # str
-            )  # /Constant
-            FormattedValue(
-              conversion=
-                -1,  # int
-              format_spec=
-                JoinedStr(
-                  values=
-                    Constant(
-                      value=
-                        '<',  # str
-                    )  # /Constant
-                    FormattedValue(
-                      conversion=
-                        -1,  # int
-                      format_spec=
-                        None,  # NoneType
-                      value=
-                        BinOp(
-                          left=
-                            Name(
-                              ctx=
-                                Load(
-                                )  # /Load
-                              id=
-                                'bal_len',  # str
-                            )  # /Name
-                          op=
-                            Add(
-                            )  # /Add
-                          right=
-                            Constant(
-                              value=
-                                5,  # int
-                            )  # /Constant
-                        )  # /BinOp
-                    )  # /FormattedValue
-                )  # /JoinedStr
-              value=
-                Name(
-                  ctx=
-                    Load(
-                    )  # /Load
-                  id=
-                    'balance',  # str
-                )  # /Name
-            )  # /FormattedValue
-            Constant(
-              value=
-                '<<',  # str
+                '{balance: <{bal_len + 5} <<',  # str
             )  # /Constant
             FormattedValue(
               conversion=
                 -1,  # int
               format_spec=

Expected behavior:
I would expect Black to not touch that f-string.

Environment:

  • Version: master (commit cd3a93a, with --experimental-string-processing flag)
  • OS and Python version: Windows/Python 3.8.3

Does this bug also happen on master?
It only happens on master, it doesn't happen on current release as there's no string reformatting in it yet. Online formatter doesn't error, but it does produce this output which is obviously not equivalent to the original:

temp_msg = (
    f"{f'{humanize_number(pos)}.': <{pound_len+2}} "
    "{balance: <{bal_len + 5} "
    f"<<{author.display_name}>>\n"
)

Additional context:
None

I'm sorry for the amount of issues but I tried to run Black's master on Cog-Creators/Red-DiscordBot's code base which resulted in quite a few of errors and also some undesired (by me) changes which I wanted to at least report even if they're not gonna be changed.

@Jackenmen Jackenmen added the T: bug Something isn't working label May 31, 2020
@ichard26 ichard26 added C: invalid code Black destroyed a valid Python file F: strings Related to our handling of strings labels May 30, 2021
@ichard26
Copy link
Collaborator

Can confirm this still fails on main (519f807).

@JelleZijlstra
Copy link
Collaborator

cc @bbugyi200 if you're interested. This crash happens only with --experimental-string-processing.

@bbugyi200
Copy link
Contributor

@JelleZijlstra Thanks for tagging me. This crash should be fixed with the following PR: #2287

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid code Black destroyed a valid Python file F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants