Skip to content

string_processing: Long dictionary and f-string not formatted after breaking first line #3747

@workonoss

Description

@workonoss

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==

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: preview styleIssues with the preview and unstable style. Add the name of the responsible feature in the title.F: stringsRelated to our handling of stringsT: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions