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: Long dictionary and f-string not formatted after breaking first line #3747

Open
workonoss opened this issue Jun 23, 2023 · 5 comments
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. F: strings Related to our handling of strings T: bug Something isn't working

Comments

@workonoss
Copy link

Describe the bug

Long strings and f-strings are not being formatted to line-width.
New strings created by black are not respecting line-width.

To Reproduce

For example, take this code for variable a:

a = f"{nested_ui_dict_val[i][j][k]['name']} ansible_host={nested_ui_dict_val[i][j][k]['mgmt_ip']} MYPORT=111222333 PATH={nested_ui_dict_val[i][j][k]['path']} ip={nested_ui_dict_val[i][j][k]['ip']} NODESTUFF={nested_ui_dict_val[i][j][k]['nodestuff']} MOREID={nested_ui_dict_val[i][j][k].get('id')} MAINTENANCE={nested_ui_dict_val[i][j][k]['instance_in_maintenance'] or nested_ui_dict_val[i][j][k]['node_in_maintenance']}\n"
b = "a loooooooooooooooooooo oooooooong string with {} and nnnnnnooo sssssssssssssssss ppppppppac overrrrrrrr a limittttttt".format(1)
c = f"f-strings definitely make things more {difficult} than they need to be for {{black}}. But boy they sure are handy. The problem is that some lines will need to have the 'f' whereas others do not. This {line}, for example, needs one."

And run it against black with --preview:

$ $ python -m black --preview -c 'a = f"{nested_ui_dict_val[i][j][k]['name']} ansible_host={nested_ui_dict_val[i][j][k]['mgmt_ip']} MYPORT=111222333 PATH={nested_ui_dict_val[i][j][k]['path']} ip={nested_ui_dict_val[i][j][k]['ip']} NODESTUFF={nested_ui_dict_val[i][j][k]['nodestuff']} MOREID={nested_ui_dict_val[i][j][k].get('id')} MAINTENANCE={nested_ui_dict_val[i][j][k]['instance_in_maintenance'] or nested_ui_dict_val[i][j][k]['node_in_maintenance']}\n"'

The resulting output is:

a = (
    f"{nested_ui_dict_val[i][j][k][name]} ansible_host={nested_ui_dict_val[i][j][k][mgmt_ip]} MYPORT=111222333"
    f" PATH={nested_ui_dict_val[i][j][k][path]} ip={nested_ui_dict_val[i][j][k][ip]} NODESTUFF={nested_ui_dict_val[i][j][k][nodestuff]} MOREID={nested_ui_dict_val[i][j][k].get(id)} MAINTENANCE={nested_ui_dict_val[i][j][k][instance_in_maintenance] or nested_ui_dict_val[i][j][k][node_in_maintenance]}\n"
)

Expected behavior

I expect the subsequent parts of the formatted string to also respect line-width or best fit

a = (
    f"{nested_ui_dict_val[i][j][k][name]} ansible_host={nested_ui_dict_val[i][j][k][mgmt_ip]} MYPORT=111222333"
    f" PATH={nested_ui_dict_val[i][j][k][path]} ip={nested_ui_dict_val[i][j][k][ip]}"
    f" NODESTUFF={nested_ui_dict_val[i][j][k][nodestuff]} MOREID={nested_ui_dict_val[i][j][k].get(id)}"
    f" MAINTENANCE={nested_ui_dict_val[i][j][k][instance_in_maintenance] or nested_ui_dict_val[i][j][k][node_in_maintenance]}\n" << not sure about this line.
)

Environment

Macos Ventura 13.3.1
python -m black, 23.3.0 (compiled: yes)
Python (CPython) 3.11.3

Additional context

I also tried it in the black online formatter:
https://black.vercel.app/?version=main&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AOJAdRdAD2IimZxl1N_WlOfrjrxSR7DDzvx2We1aheMjV5DGeP3u-eAGsSx8HeKvvAO8NY_fW7oqtVDitrobuxhblPs7o5pkfycKe7coiigf4mPPAZzzYFp-cMZRWORSB-Gj8tZzbtjzppAeLljuxNSZ0A5RCV0fU-HJTjsoJc7ia75Is2LS2Tu1KbNm3kdoOWZ1lmI1teQxlevW_ki5EgHQTsHjH1unxS2zduAZ5N-n4KaVIXZCXWG-ndFoavjycobm9g_LlocqxpUEXGgdEKQsg026FHiMDrO7fSRm5xXs-CF8p_jdgvDtFHTAnsqv_EuLkcWlTG0CI50eVgVRV5SAdQEXVCHrpFaikYcq0P50IlKVI_UJV9IfQrKXMLKBOyk-MCziU8OIm9236R2F9_AteAtJx_jbMwYrNEl2pw9T1qGB4xcpbIKEr7--8CJHm3fXIebb1pN5s1mT-xRzUdu1Y-zfzvJD8vZmTR-faR2GQPdmaJpQVm-G1vOgO34Do6ZW_PRCw4niJNPWjJ3sw63ZgUZviQuogsynfAlmDXGSiMsWO4aO5Syrus3KyOyb4vCpOabT4FSexTYJ49zMb4Il6QJ_WbdJ_9nA1Zm9BwUhd_T0u_otmz-YgATsBh3NAAHHwAB8AOKBwAAzvfMFrHEZ_sCAAAAAARZWg==

@workonoss workonoss added the T: bug Something isn't working label Jun 23, 2023
@workonoss
Copy link
Author

I looked through the open issues but didn't see anything similiar to this.
Thank you for any feedback or attention to this case.

@JelleZijlstra
Copy link
Collaborator

This is fixed in the preview style (--preview), which is expected to become the default next year.

@workonoss
Copy link
Author

@JelleZijlstra I used the --preview in the example but that doesn't show the expected output.

$ python -m black --preview -c 'a = f"{nested_ui_dict_val[i][j][k]['name']} ansible_host={nested_ui_dict_val[i][j][k]['mgmt_ip']} MYPORT=111222333 PATH={nested_ui_dict_val[i][j][k]['path']} ip={nested_ui_dict_val[i][j][k]['ip']} NODESTUFF={nested_ui_dict_val[i][j][k]['nodestuff']} MOREID={nested_ui_dict_val[i][j][k].get('id')} MAINTENANCE={nested_ui_dict_val[i][j][k]['instance_in_maintenance'] or nested_ui_dict_val[i][j][k]['node_in_maintenance']}\n"'

It was my understanding that if using the --preview flag, it should already format as I expect with all new strings also formatted. Please clarify.

@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 Jun 23, 2023
@JelleZijlstra
Copy link
Collaborator

Ah, sorry for missing that. Yes, seems like we should split the string more in that case.

@JelleZijlstra JelleZijlstra reopened this Jun 23, 2023
@workonoss
Copy link
Author

Adding screenshot of online formatter since it will change when code is modified:
online formatter link
Screenshot 2023-06-23 at 15 56 37

@hauntsaninja hauntsaninja added the F: strings Related to our handling of strings label Sep 3, 2023
@JelleZijlstra JelleZijlstra changed the title Long dictionary and f-string not formatted after breaking first line string_processing: Long dictionary and f-string not formatted after breaking first line 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: strings Related to our handling of strings T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants