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

f-string internal formatting to be consistent with non-f-string formatting #4315

Closed
ColemanTom opened this issue Apr 19, 2024 · 1 comment
Closed
Labels
F: strings Related to our handling of strings T: enhancement New feature or request

Comments

@ColemanTom
Copy link

Is your feature request related to a problem? Please describe.

Consider a simple python script:

import numpy as np
a=np.arange(9).reshape((3,3))
print(a[1,2])
print(f"a[1,2] = {a[1,2]}")

Running black on this it would by default provide:

$ black --diff a.py
--- a.py        2024-04-19 01:29:28.562943+00:00
+++ a.py        2024-04-19 01:29:52.064944+00:00
@@ -1,4 +1,5 @@
 import numpy as np
-a=np.arange(9).reshape((3,3))
-print(a[1,2])
+
+a = np.arange(9).reshape((3, 3))
+print(a[1, 2])
 print(f"a[1,2] = {a[1,2]}")
would reformat a.py

All done! ✨ 🍰 ✨
1 file would be reformatted.

You can see the final two print statements are inconsistent formatting now. The first has inserted a space after the comma in a[1,2], but, the f-string has not adjusted the formatting of {a[1,2]} to be {a[1, 2]}.

If I accept the formatting black suggests, and then run flake8 on the same piece of code, flake8 will complain about the lack of a space after the comma in the f-string.

$ black a.py
reformatted a.py

All done! ✨ 🍰 ✨
1 file reformatted.
$ flake8 a.py
a.py:5:22: E231 missing whitespace after ','

Describe the solution you'd like

Formatting applied to code outside of f-strings should also apply to the code within the {..} in the f-strings.

Describe alternatives you've considered

Disable that warning in flake8.

Additional context

N/A.

@ColemanTom ColemanTom added the T: enhancement New feature or request label Apr 19, 2024
@JelleZijlstra JelleZijlstra added the F: strings Related to our handling of strings label Apr 19, 2024
@JelleZijlstra
Copy link
Collaborator

Duplicate of #567.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: strings Related to our handling of strings T: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants