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

string_processing: Ugly reformatting of string literal with .format() method #3410

Open
JelleZijlstra opened this issue Dec 9, 2022 · 1 comment
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings T: bug Something isn't working

Comments

@JelleZijlstra
Copy link
Collaborator

% cat quash.py 
class X:
    def generate_shared_lib_init(self) -> None:
        emitter.emit_lines(
            "{",
            (
                'static PyModuleDef def = {{ PyModuleDef_HEAD_INIT, "{}", NULL, -1, NULL, NULL }};'.format(
                    shared_lib_name(self.group_name)
                )
            ),
        )
% black --preview --diff quash.py
--- quash.py	2022-12-09 03:02:23.973269 +0000
+++ quash.py	2022-12-09 03:03:57.327044 +0000
@@ -1,10 +1,7 @@
 class X:
     def generate_shared_lib_init(self) -> None:
         emitter.emit_lines(
             "{",
-            (
-                'static PyModuleDef def = {{ PyModuleDef_HEAD_INIT, "{}", NULL, -1, NULL, NULL }};'.format(
-                    shared_lib_name(self.group_name)
-                )
-            ),
+            'static PyModuleDef def = {{ PyModuleDef_HEAD_INIT, "{}", NULL, -1, NULL, NULL }};'
+            .format(shared_lib_name(self.group_name)),
         )
would reformat quash.py

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

I like the old code better so I wouldn't want stable style to start behaving like this (ref. #3407).

@JelleZijlstra JelleZijlstra added T: bug Something isn't working F: parentheses Too many parentheses, not enough parentheses, and so on. labels Dec 9, 2022
@JelleZijlstra JelleZijlstra added the C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. label Dec 19, 2022
@felix-hilden
Copy link
Collaborator

felix-hilden commented Jan 18, 2023

I'll disagree for this particular case: it's two lines and one nesting level less, and having "format" on a separate line avoids having to scan at the end of the previous line. But it seems to be formatted differently depending on the line length (playground):

class X:
    def generate_shared_lib_init(self) -> None:
        emitter.emit_lines(
            "{",
            'static PyModuleDef def = {{ PyModuleDef_HEAD_INIT, "{}", NULL, -1, NULL,'
            " NULL }};".format(shared_lib_name(self.group_name)),
        )

Not so sure about that one, especially since we now put implicitly concatenated strings in parens elsewhere (also in the playground example):

call(
    a,
    (
        "static PyModuleDefwithmorestringnesstopadoutthelinelength def = {{"
        " PyModuleDef_HEAD_INIT,{}, NULL, -1, NULL, NULL }};"
    ),
    b,
)

@hauntsaninja hauntsaninja added the F: strings Related to our handling of strings label Sep 3, 2023
@JelleZijlstra JelleZijlstra changed the title Preview: Ugly reformatting of string literal with .format() method string_processing: Ugly reformatting of string literal with .format() method Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. F: parentheses Too many parentheses, not enough parentheses, and so on. F: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants