Debian Codegraph @

tests/data/cases/preview_long_strings.py

raw
1 # flags: --unstable
2 x = "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."
3
4 x += "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."
5
6 y = (
7 'Short string'
8 )
9
10 print('This is a really long string inside of a print statement with extra arguments attached at the end of it.', x, y, z)
11
12 print("This is a really long string inside of a print statement with no extra arguments attached at the end of it.")
13
14 D1 = {"The First": "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a dictionary, so formatting is more difficult.", "The Second": "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a dictionary."}
15
16 D2 = {1.0: "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a dictionary, so formatting is more difficult.", 2.0: "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a dictionary."}
17
18 D3 = {x: "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a dictionary, so formatting is more difficult.", y: "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a dictionary."}
19
20 D4 = {"A long and ridiculous {}".format(string_key): "This is a really really really long string that has to go i,side of a dictionary. It is soooo bad.", some_func("calling", "some", "stuff"): "This is a really really really long string that has to go inside of a dictionary. It is {soooo} bad (#{x}).".format(sooo="soooo", x=2), "A %s %s" % ("formatted", "string"): "This is a really really really long string that has to go inside of a dictionary. It is %s bad (#%d)." % ("soooo", 2)}
21
22 D5 = { # Test for https://github.com/psf/black/issues/3261
23 ("This is a really long string that can't be expected to fit in one line and is used as a nested dict's key"): {"inner": "value"},
24 }
25
26 D6 = { # Test for https://github.com/psf/black/issues/3261
27 ("This is a really long string that can't be expected to fit in one line and is used as a dict's key"): ["value1", "value2"],
28 }
29
30 L1 = ["The is a short string", "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a list literal, so it's expected to be wrapped in parens when splitting to avoid implicit str concatenation.", short_call("arg", {"key": "value"}), "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a list literal.", ("parens should be stripped for short string in list")]
31
32 L2 = ["This is a really long string that can't be expected to fit in one line and is the only child of a list literal."]
33
34 S1 = {"The is a short string", "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a set literal, so it's expected to be wrapped in parens when splitting to avoid implicit str concatenation.", short_call("arg", {"key": "value"}), "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a set literal.", ("parens should be stripped for short string in set")}
35
36 S2 = {"This is a really long string that can't be expected to fit in one line and is the only child of a set literal."}
37
38 T1 = ("The is a short string", "This is a really long string that can't possibly be expected to fit all together on one line. Also it is inside a tuple literal, so it's expected to be wrapped in parens when splitting to avoid implicit str concatenation.", short_call("arg", {"key": "value"}), "This is another really really (not really) long string that also can't be expected to fit on one line and is, like the other string, inside a tuple literal.", ("parens should be stripped for short string in list"))
39
40 T2 = ("This is a really long string that can't be expected to fit in one line and is the only child of a tuple literal.",)
41
42 # Test case for https://github.com/psf/black/issues/4912 - unassigned long string with trailing comma
43 "A long string literal that is not assigned to a variable, exceeds line length when string-processing is enabled, and has a trailing comma (to make it a one-item tuple)",
44
45 func_with_keywords(my_arg, my_kwarg="Long keyword strings also need to be wrapped, but they will probably need to be handled a little bit differently.")
46
47 bad_split1 = (
48 'But what should happen when code has already been formatted but in the wrong way? Like'
49 " with a space at the end instead of the beginning. Or what about when it is split too soon?"
50 )
51
52 bad_split2 = "But what should happen when code has already " \
53 "been formatted but in the wrong way? Like " \
54 "with a space at the end instead of the " \
55 "beginning. Or what about when it is split too " \
56 "soon? In the case of a split that is too " \
57 "short, black will try to honer the custom " \
58 "split."
59
60 bad_split3 = (
61 "What if we have inline comments on " # First Comment
62 "each line of a bad split? In that " # Second Comment
63 "case, we should just leave it alone." # Third Comment
64 )
65
66 bad_split_func1(
67 "But what should happen when code has already "
68 "been formatted but in the wrong way? Like "
69 "with a space at the end instead of the "
70 "beginning. Or what about when it is split too "
71 "soon? In the case of a split that is too "
72 "short, black will try to honer the custom "
73 "split.",
74 xxx, yyy, zzz
75 )
76
77 bad_split_func2(
78 xxx, yyy, zzz,
79 long_string_kwarg="But what should happen when code has already been formatted but in the wrong way? Like "
80 "with a space at the end instead of the beginning. Or what about when it is split too "
81 "soon?",
82 )
83
84 bad_split_func3(
85 (
86 "But what should happen when code has already "
87 r"been formatted but in the wrong way? Like "
88 "with a space at the end instead of the "
89 r"beginning. Or what about when it is split too "
90 r"soon? In the case of a split that is too "
91 "short, black will try to honer the custom "
92 "split."
93 ),
94 xxx,
95 yyy,
96 zzz,
97 )
98
99 inline_comments_func1(
100 "if there are inline "
101 "comments in the middle "
102 # Here is the standard alone comment.
103 "of the implicitly concatenated "
104 "string, we should handle "
105 "them correctly",
106 xxx,
107 )
108
109 inline_comments_func2(
110 "what if the string is very very very very very very very very very very long and this part does "
111 "not fit into a single line? "
112 # Here is the standard alone comment.
113 "then the string should still be properly handled by merging and splitting "
114 "it into parts that fit in line length.",
115 xxx,
116 )
117
118 raw_string = r"This is a long raw string. When re-formatting this string, black needs to make sure it prepends the 'r' onto the new string."
119
120 fmt_string1 = "We also need to be sure to preserve any and all {} which may or may not be attached to the string in question.".format("method calls")
121
122 fmt_string2 = "But what about when the string is {} but {}".format("short", "the method call is really really really really really really really really long?")
123
124 old_fmt_string1 = "While we are on the topic of %s, we should also note that old-style formatting must also be preserved, since some %s still uses it." % ("formatting", "code")
125
126 old_fmt_string2 = "This is a %s %s %s %s" % ("really really really really really", "old", "way to format strings!", "Use f-strings instead!")
127
128 old_fmt_string3 = "Whereas only the strings after the percent sign were long in the last example, this example uses a long initial string as well. This is another %s %s %s %s" % ("really really really really really", "old", "way to format strings!", "Use f-strings instead!")
129
130 fstring = 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."
131
132 fstring_with_no_fexprs = f"Some regular string that needs to get split certainly but is NOT an fstring by any means whatsoever."
133
134 comment_string = "Long lines with inline comments should have their comments appended to the reformatted string's enclosing right parentheses." # This comment gets thrown to the top.
135
136 arg_comment_string = print("Long lines with inline comments which are apart of (and not the only member of) an argument list should have their comments appended to the reformatted string's enclosing left parentheses.", # This comment gets thrown to the top.
137 "Arg #2", "Arg #3", "Arg #4", "Arg #5")
138
139 pragma_comment_string1 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa: E501
140
141 pragma_comment_string2 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa
142
143 """This is a really really really long triple quote string and it should not be touched."""
144
145 triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
146
147 assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception."
148
149 assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string {}.".format("formatting")
150
151 assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic string %s." % "formatting"
152
153 assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception, which uses dynamic %s %s." % ("string", "formatting")
154
155 some_function_call("With a reallly generic name and with a really really long string that is, at some point down the line, " + added + " to a variable and then added to another string.")
156
157 some_function_call("With a reallly generic name and with a really really long string that is, at some point down the line, " + added + " to a variable and then added to another string. But then what happens when the final string is also supppppperrrrr long?! Well then that second (realllllllly long) string should be split too.", "and a second argument", and_a_third)
158
159 return "A really really really really really really really really really really really really really long {} {}".format("return", "value")
160
161 func_with_bad_comma(
162 "This is a really long string argument to a function that has a trailing comma which should NOT be there.",
163 )
164
165 func_with_bad_comma(
166 "This is a really long string argument to a function that has a trailing comma which should NOT be there.", # comment after comma
167 )
168
169 func_with_bad_comma(
170 (
171 "This is a really long string argument to a function that has a trailing comma"
172 " which should NOT be there."
173 ),
174 )
175
176 func_with_bad_comma(
177 (
178 "This is a really long string argument to a function that has a trailing comma"
179 " which should NOT be there."
180 ), # comment after comma
181 )
182
183 func_with_bad_parens_that_wont_fit_in_one_line(
184 ("short string that should have parens stripped"),
185 x,
186 y,
187 z
188 )
189
190 func_with_bad_parens_that_wont_fit_in_one_line(
191 x,
192 y,
193 ("short string that should have parens stripped"),
194 z
195 )
196
197 func_with_bad_parens(
198 ("short string that should have parens stripped"),
199 x,
200 y,
201 z,
202 )
203
204 func_with_bad_parens(
205 x,
206 y,
207 ("short string that should have parens stripped"),
208 z,
209 )
210
211 annotated_variable: Final = "This is a large " + STRING + " that has been " + CONCATENATED + "using the '+' operator."
212 annotated_variable: Final = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
213 annotated_variable: Literal["fakse_literal"] = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
214
215 backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\"
216 backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\\\"
217 backslashes = "This is a really 'long' string with \"embedded double quotes\" and 'single' quotes that also handles checking for an odd number of backslashes \\\", like this...\\\\\\"
218
219 short_string = (
220 "Hi"
221 " there."
222 )
223
224 func_call(
225 short_string=(
226 "Hi"
227 " there."
228 )
229 )
230
231 raw_strings = r"Don't" " get" r" merged" " unless they are all raw."
232
233 def foo():
234 yield "This is a really long string that can't possibly be expected to fit all together on one line. In fact it may even take up three or more lines... like four or five... but probably just three."
235
236 x = f"This is a {{really}} long string that needs to be split without a doubt (i.e. most definitely). In short, this {string} that can't possibly be {{expected}} to fit all together on one line. In {fact} it may even take up three or more lines... like four or five... but probably just four."
237
238 long_unmergable_string_with_pragma = (
239 "This is a really long string that can't be merged because it has a likely pragma at the end" # type: ignore
240 " of it."
241 )
242
243 long_unmergable_string_with_pragma = (
244 "This is a really long string that can't be merged because it has a likely pragma at the end" # noqa
245 " of it."
246 )
247
248 long_unmergable_string_with_pragma = (
249 "This is a really long string that can't be merged because it has a likely pragma at the end" # pylint: disable=some-pylint-check
250 " of it."
251 )
252
253 string_with_nameescape = (
254 "........................................................................ \N{LAO KO LA}"
255 )
256
257 string_with_nameescape = (
258 "........................................................................... \N{LAO KO LA}"
259 )
260
261 string_with_nameescape = (
262 "............................................................................ \N{LAO KO LA}"
263 )
264
265 string_with_nameescape_and_escaped_backslash = (
266 "...................................................................... \\\N{LAO KO LA}"
267 )
268
269 string_with_nameescape_and_escaped_backslash = (
270 "......................................................................... \\\N{LAO KO LA}"
271 )
272
273 string_with_nameescape_and_escaped_backslash = (
274 ".......................................................................... \\\N{LAO KO LA}"
275 )
276
277 string_with_escaped_nameescape = (
278 "........................................................................ \\N{LAO KO LA}"
279 )
280
281 string_with_escaped_nameescape = (
282 "........................................................................... \\N{LAO KO LA}"
283 )
284
285 msg = lambda x: f"this is a very very very very long lambda value {x} that doesn't fit on a single line"
286
287 dict_with_lambda_values = {
288 "join": lambda j: (
289 f"{j.__class__.__name__}({some_function_call(j.left)}, "
290 f"{some_function_call(j.right)})"
291 ),
292 }
293
294 # Complex string concatenations with a method call in the middle.
295 code = (
296 (" return [\n")
297 + (
298 ", \n".join(
299 " (%r, self.%s, visitor.%s)"
300 % (attrname, attrname, visit_name)
301 for attrname, visit_name in names
302 )
303 )
304 + ("\n ]\n")
305 )
306
307
308 # Test case of an outer string' parens enclose an inner string's parens.
309 call(body=("%s %s" % ((",".join(items)), suffix)))
310
311 log.info(f'Skipping: {desc["db_id"]=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]=} {desc["exposure_max"]=}')
312
313 log.info(f"Skipping: {desc['db_id']=} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']=} {desc['exposure_max']=}")
314
315 log.info(f'Skipping: {desc["db_id"]} {foo("bar",x=123)} {"foo" != "bar"} {(x := "abc=")} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
316
317 log.info(f'Skipping: {desc["db_id"]} {desc["ms_name"]} {money=} {(x := "abc=")=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
318
319 log.info(f'Skipping: {desc["db_id"]} {foo("bar",x=123)=} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
320
321 log.info(f'Skipping: {foo("asdf")=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
322
323 log.info(f'Skipping: {"a" == "b" == "c" == "d"} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
324
325 log.info(f'Skipping: {"a" == "b" == "c" == "d"=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}')
326
327 log.info(f'Skipping: { longer_longer_longer_longer_longer_longer_name [ "db_id" ] [ "another_key" ] = : .3f }')
328
329 log.info(f'''Skipping: {"a" == 'b'} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}''')
330
331 log.info(f'''Skipping: {'a' == "b"=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}''')
332
333 log.info(f"""Skipping: {'a' == 'b'} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']} {desc['exposure_max']}""")
334
335 x = {
336 "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
337 "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx"
338 )
339 }
340 x = {
341 "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx",
342 }
343 x = {
344 "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
345 "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxx"
346 )
347 }
348
349 # output
350
351
352 x = (
353 "This is a really long string that can't possibly be expected to fit all together"
354 " on one line. In fact it may even take up three or more lines... like four or"
355 " five... but probably just three."
356 )
357
358 x += (
359 "This is a really long string that can't possibly be expected to fit all together"
360 " on one line. In fact it may even take up three or more lines... like four or"
361 " five... but probably just three."
362 )
363
364 y = "Short string"
365
366 print(
367 "This is a really long string inside of a print statement with extra arguments"
368 " attached at the end of it.",
369 x,
370 y,
371 z,
372 )
373
374 print(
375 "This is a really long string inside of a print statement with no extra arguments"
376 " attached at the end of it."
377 )
378
379 D1 = {
380 "The First": (
381 "This is a really long string that can't possibly be expected to fit all"
382 " together on one line. Also it is inside a dictionary, so formatting is more"
383 " difficult."
384 ),
385 "The Second": (
386 "This is another really really (not really) long string that also can't be"
387 " expected to fit on one line and is, like the other string, inside a"
388 " dictionary."
389 ),
390 }
391
392 D2 = {
393 1.0: (
394 "This is a really long string that can't possibly be expected to fit all"
395 " together on one line. Also it is inside a dictionary, so formatting is more"
396 " difficult."
397 ),
398 2.0: (
399 "This is another really really (not really) long string that also can't be"
400 " expected to fit on one line and is, like the other string, inside a"
401 " dictionary."
402 ),
403 }
404
405 D3 = {
406 x: (
407 "This is a really long string that can't possibly be expected to fit all"
408 " together on one line. Also it is inside a dictionary, so formatting is more"
409 " difficult."
410 ),
411 y: (
412 "This is another really really (not really) long string that also can't be"
413 " expected to fit on one line and is, like the other string, inside a"
414 " dictionary."
415 ),
416 }
417
418 D4 = {
419 "A long and ridiculous {}".format(string_key): (
420 "This is a really really really long string that has to go i,side of a"
421 " dictionary. It is soooo bad."
422 ),
423 some_func("calling", "some", "stuff"): (
424 "This is a really really really long string that has to go inside of a"
425 " dictionary. It is {soooo} bad (#{x}).".format(sooo="soooo", x=2)
426 ),
427 "A %s %s"
428 % ("formatted", "string"): (
429 "This is a really really really long string that has to go inside of a"
430 " dictionary. It is %s bad (#%d)." % ("soooo", 2)
431 ),
432 }
433
434 D5 = { # Test for https://github.com/psf/black/issues/3261
435 "This is a really long string that can't be expected to fit in one line and is used as a nested dict's key": {
436 "inner": "value"
437 },
438 }
439
440 D6 = { # Test for https://github.com/psf/black/issues/3261
441 "This is a really long string that can't be expected to fit in one line and is used as a dict's key": [
442 "value1",
443 "value2",
444 ],
445 }
446
447 L1 = [
448 "The is a short string",
449 (
450 "This is a really long string that can't possibly be expected to fit all"
451 " together on one line. Also it is inside a list literal, so it's expected to"
452 " be wrapped in parens when splitting to avoid implicit str concatenation."
453 ),
454 short_call("arg", {"key": "value"}),
455 (
456 "This is another really really (not really) long string that also can't be"
457 " expected to fit on one line and is, like the other string, inside a list"
458 " literal."
459 ),
460 "parens should be stripped for short string in list",
461 ]
462
463 L2 = [
464 "This is a really long string that can't be expected to fit in one line and is the"
465 " only child of a list literal."
466 ]
467
468 S1 = {
469 "The is a short string",
470 (
471 "This is a really long string that can't possibly be expected to fit all"
472 " together on one line. Also it is inside a set literal, so it's expected to be"
473 " wrapped in parens when splitting to avoid implicit str concatenation."
474 ),
475 short_call("arg", {"key": "value"}),
476 (
477 "This is another really really (not really) long string that also can't be"
478 " expected to fit on one line and is, like the other string, inside a set"
479 " literal."
480 ),
481 "parens should be stripped for short string in set",
482 }
483
484 S2 = {
485 "This is a really long string that can't be expected to fit in one line and is the"
486 " only child of a set literal."
487 }
488
489 T1 = (
490 "The is a short string",
491 (
492 "This is a really long string that can't possibly be expected to fit all"
493 " together on one line. Also it is inside a tuple literal, so it's expected to"
494 " be wrapped in parens when splitting to avoid implicit str concatenation."
495 ),
496 short_call("arg", {"key": "value"}),
497 (
498 "This is another really really (not really) long string that also can't be"
499 " expected to fit on one line and is, like the other string, inside a tuple"
500 " literal."
501 ),
502 "parens should be stripped for short string in list",
503 )
504
505 T2 = (
506 (
507 "This is a really long string that can't be expected to fit in one line and is"
508 " the only child of a tuple literal."
509 ),
510 )
511
512 # Test case for https://github.com/psf/black/issues/4912 - unassigned long string with trailing comma
513 (
514 "A long string literal that is not assigned to a variable, exceeds line length when"
515 " string-processing is enabled, and has a trailing comma (to make it a one-item"
516 " tuple)"
517 ),
518
519 func_with_keywords(
520 my_arg,
521 my_kwarg=(
522 "Long keyword strings also need to be wrapped, but they will probably need to"
523 " be handled a little bit differently."
524 ),
525 )
526
527 bad_split1 = (
528 "But what should happen when code has already been formatted but in the wrong way?"
529 " Like with a space at the end instead of the beginning. Or what about when it is"
530 " split too soon?"
531 )
532
533 bad_split2 = (
534 "But what should happen when code has already "
535 "been formatted but in the wrong way? Like "
536 "with a space at the end instead of the "
537 "beginning. Or what about when it is split too "
538 "soon? In the case of a split that is too "
539 "short, black will try to honer the custom "
540 "split."
541 )
542
543 bad_split3 = (
544 "What if we have inline comments on " # First Comment
545 "each line of a bad split? In that " # Second Comment
546 "case, we should just leave it alone." # Third Comment
547 )
548
549 bad_split_func1(
550 "But what should happen when code has already "
551 "been formatted but in the wrong way? Like "
552 "with a space at the end instead of the "
553 "beginning. Or what about when it is split too "
554 "soon? In the case of a split that is too "
555 "short, black will try to honer the custom "
556 "split.",
557 xxx,
558 yyy,
559 zzz,
560 )
561
562 bad_split_func2(
563 xxx,
564 yyy,
565 zzz,
566 long_string_kwarg=(
567 "But what should happen when code has already been formatted but in the wrong"
568 " way? Like with a space at the end instead of the beginning. Or what about"
569 " when it is split too soon?"
570 ),
571 )
572
573 bad_split_func3(
574 (
575 "But what should happen when code has already "
576 r"been formatted but in the wrong way? Like "
577 "with a space at the end instead of the "
578 r"beginning. Or what about when it is split too "
579 r"soon? In the case of a split that is too "
580 "short, black will try to honer the custom "
581 "split."
582 ),
583 xxx,
584 yyy,
585 zzz,
586 )
587
588 inline_comments_func1(
589 "if there are inline comments in the middle "
590 # Here is the standard alone comment.
591 "of the implicitly concatenated string, we should handle them correctly",
592 xxx,
593 )
594
595 inline_comments_func2(
596 "what if the string is very very very very very very very very very very long and"
597 " this part does not fit into a single line? "
598 # Here is the standard alone comment.
599 "then the string should still be properly handled by merging and splitting "
600 "it into parts that fit in line length.",
601 xxx,
602 )
603
604 raw_string = (
605 r"This is a long raw string. When re-formatting this string, black needs to make"
606 r" sure it prepends the 'r' onto the new string."
607 )
608
609 fmt_string1 = (
610 "We also need to be sure to preserve any and all {} which may or may not be"
611 " attached to the string in question.".format("method calls")
612 )
613
614 fmt_string2 = "But what about when the string is {} but {}".format(
615 "short",
616 "the method call is really really really really really really really really long?",
617 )
618
619 old_fmt_string1 = (
620 "While we are on the topic of %s, we should also note that old-style formatting"
621 " must also be preserved, since some %s still uses it." % ("formatting", "code")
622 )
623
624 old_fmt_string2 = "This is a %s %s %s %s" % (
625 "really really really really really",
626 "old",
627 "way to format strings!",
628 "Use f-strings instead!",
629 )
630
631 old_fmt_string3 = (
632 "Whereas only the strings after the percent sign were long in the last example,"
633 " this example uses a long initial string as well. This is another %s %s %s %s"
634 % (
635 "really really really really really",
636 "old",
637 "way to format strings!",
638 "Use f-strings instead!",
639 )
640 )
641
642 fstring = (
643 f"f-strings definitely make things more {difficult} than they need to be for"
644 " {black}. But boy they sure are handy. The problem is that some lines will need"
645 f" to have the 'f' whereas others do not. This {line}, for example, needs one."
646 )
647
648 fstring_with_no_fexprs = (
649 f"Some regular string that needs to get split certainly but is NOT an fstring by"
650 f" any means whatsoever."
651 )
652
653 comment_string = ( # This comment gets thrown to the top.
654 "Long lines with inline comments should have their comments appended to the"
655 " reformatted string's enclosing right parentheses."
656 )
657
658 arg_comment_string = print(
659 "Long lines with inline comments which are apart of (and not the only member of) an"
660 " argument list should have their comments appended to the reformatted string's"
661 " enclosing left parentheses.", # This comment gets thrown to the top.
662 "Arg #2",
663 "Arg #3",
664 "Arg #4",
665 "Arg #5",
666 )
667
668 pragma_comment_string1 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa: E501
669
670 pragma_comment_string2 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa
671
672 """This is a really really really long triple quote string and it should not be touched."""
673
674 triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
675
676 assert some_type_of_boolean_expression, (
677 "Followed by a really really really long string that is used to provide context to"
678 " the AssertionError exception."
679 )
680
681 assert some_type_of_boolean_expression, (
682 "Followed by a really really really long string that is used to provide context to"
683 " the AssertionError exception, which uses dynamic string {}.".format("formatting")
684 )
685
686 assert some_type_of_boolean_expression, (
687 "Followed by a really really really long string that is used to provide context to"
688 " the AssertionError exception, which uses dynamic string %s." % "formatting"
689 )
690
691 assert some_type_of_boolean_expression, (
692 "Followed by a really really really long string that is used to provide context to"
693 " the AssertionError exception, which uses dynamic %s %s."
694 % ("string", "formatting")
695 )
696
697 some_function_call(
698 "With a reallly generic name and with a really really long string that is, at some"
699 " point down the line, "
700 + added
701 + " to a variable and then added to another string."
702 )
703
704 some_function_call(
705 "With a reallly generic name and with a really really long string that is, at some"
706 " point down the line, "
707 + added
708 + " to a variable and then added to another string. But then what happens when the"
709 " final string is also supppppperrrrr long?! Well then that second (realllllllly"
710 " long) string should be split too.",
711 "and a second argument",
712 and_a_third,
713 )
714
715 return (
716 "A really really really really really really really really really really really"
717 " really really long {} {}".format("return", "value")
718 )
719
720 func_with_bad_comma(
721 "This is a really long string argument to a function that has a trailing comma"
722 " which should NOT be there.",
723 )
724
725 func_with_bad_comma(
726 "This is a really long string argument to a function that has a trailing comma"
727 " which should NOT be there.", # comment after comma
728 )
729
730 func_with_bad_comma(
731 "This is a really long string argument to a function that has a trailing comma"
732 " which should NOT be there.",
733 )
734
735 func_with_bad_comma(
736 "This is a really long string argument to a function that has a trailing comma"
737 " which should NOT be there.", # comment after comma
738 )
739
740 func_with_bad_parens_that_wont_fit_in_one_line(
741 "short string that should have parens stripped", x, y, z
742 )
743
744 func_with_bad_parens_that_wont_fit_in_one_line(
745 x, y, "short string that should have parens stripped", z
746 )
747
748 func_with_bad_parens(
749 "short string that should have parens stripped",
750 x,
751 y,
752 z,
753 )
754
755 func_with_bad_parens(
756 x,
757 y,
758 "short string that should have parens stripped",
759 z,
760 )
761
762 annotated_variable: Final = (
763 "This is a large "
764 + STRING
765 + " that has been "
766 + CONCATENATED
767 + "using the '+' operator."
768 )
769 annotated_variable: Final = (
770 "This is a large string that has a type annotation attached to it. A type"
771 " annotation should NOT stop a long string from being wrapped."
772 )
773 annotated_variable: Literal["fakse_literal"] = (
774 "This is a large string that has a type annotation attached to it. A type"
775 " annotation should NOT stop a long string from being wrapped."
776 )
777
778 backslashes = (
779 "This is a really long string with \"embedded\" double quotes and 'single' quotes"
780 " that also handles checking for an even number of backslashes \\"
781 )
782 backslashes = (
783 "This is a really long string with \"embedded\" double quotes and 'single' quotes"
784 " that also handles checking for an even number of backslashes \\\\"
785 )
786 backslashes = (
787 "This is a really 'long' string with \"embedded double quotes\" and 'single' quotes"
788 ' that also handles checking for an odd number of backslashes \\", like'
789 " this...\\\\\\"
790 )
791
792 short_string = "Hi there."
793
794 func_call(short_string="Hi there.")
795
796 raw_strings = r"Don't" " get" r" merged" " unless they are all raw."
797
798
799 def foo():
800 yield (
801 "This is a really long string that can't possibly be expected to fit all"
802 " together on one line. In fact it may even take up three or more lines... like"
803 " four or five... but probably just three."
804 )
805
806
807 x = (
808 "This is a {really} long string that needs to be split without a doubt (i.e."
809 f" most definitely). In short, this {string} that can't possibly be {{expected}} to"
810 f" fit all together on one line. In {fact} it may even take up three or more"
811 " lines... like four or five... but probably just four."
812 )
813
814 long_unmergable_string_with_pragma = (
815 "This is a really long string that can't be merged because it has a likely pragma at the end" # type: ignore
816 " of it."
817 )
818
819 long_unmergable_string_with_pragma = (
820 "This is a really long string that can't be merged because it has a likely pragma at the end" # noqa
821 " of it."
822 )
823
824 long_unmergable_string_with_pragma = (
825 "This is a really long string that can't be merged because it has a likely pragma at the end" # pylint: disable=some-pylint-check
826 " of it."
827 )
828
829 string_with_nameescape = (
830 "........................................................................"
831 " \N{LAO KO LA}"
832 )
833
834 string_with_nameescape = (
835 "..........................................................................."
836 " \N{LAO KO LA}"
837 )
838
839 string_with_nameescape = (
840 "............................................................................"
841 " \N{LAO KO LA}"
842 )
843
844 string_with_nameescape_and_escaped_backslash = (
845 "......................................................................"
846 " \\\N{LAO KO LA}"
847 )
848
849 string_with_nameescape_and_escaped_backslash = (
850 "........................................................................."
851 " \\\N{LAO KO LA}"
852 )
853
854 string_with_nameescape_and_escaped_backslash = (
855 ".........................................................................."
856 " \\\N{LAO KO LA}"
857 )
858
859 string_with_escaped_nameescape = (
860 "........................................................................ \\N{LAO"
861 " KO LA}"
862 )
863
864 string_with_escaped_nameescape = (
865 "..........................................................................."
866 " \\N{LAO KO LA}"
867 )
868
869 msg = lambda x: (
870 f"this is a very very very very long lambda value {x} that doesn't fit on a"
871 " single line"
872 )
873
874 dict_with_lambda_values = {
875 "join": lambda j: (
876 f"{j.__class__.__name__}({some_function_call(j.left)}, "
877 f"{some_function_call(j.right)})"
878 ),
879 }
880
881 # Complex string concatenations with a method call in the middle.
882 code = (
883 " return [\n"
884 + ", \n".join(
885 " (%r, self.%s, visitor.%s)" % (attrname, attrname, visit_name)
886 for attrname, visit_name in names
887 )
888 + "\n ]\n"
889 )
890
891
892 # Test case of an outer string' parens enclose an inner string's parens.
893 call(body="%s %s" % (",".join(items), suffix))
894
895 log.info(
896 f'Skipping: {desc["db_id"]=} {desc["ms_name"]} {money=} {dte=} {pos_share=}'
897 f' {desc["status"]=} {desc["exposure_max"]=}'
898 )
899
900 log.info(
901 f"Skipping: {desc['db_id']=} {desc['ms_name']} {money=} {dte=} {pos_share=}"
902 f" {desc['status']=} {desc['exposure_max']=}"
903 )
904
905 log.info(
906 f'Skipping: {desc["db_id"]} {foo("bar",x=123)} {"foo" != "bar"} {(x := "abc=")}'
907 f' {pos_share=} {desc["status"]} {desc["exposure_max"]}'
908 )
909
910 log.info(
911 f'Skipping: {desc["db_id"]} {desc["ms_name"]} {money=} {(x := "abc=")=}'
912 f' {pos_share=} {desc["status"]} {desc["exposure_max"]}'
913 )
914
915 log.info(
916 f'Skipping: {desc["db_id"]} {foo("bar",x=123)=} {money=} {dte=} {pos_share=}'
917 f' {desc["status"]} {desc["exposure_max"]}'
918 )
919
920 log.info(
921 f'Skipping: {foo("asdf")=} {desc["ms_name"]} {money=} {dte=} {pos_share=}'
922 f' {desc["status"]} {desc["exposure_max"]}'
923 )
924
925 log.info(
926 f'Skipping: {"a" == "b" == "c" == "d"} {desc["ms_name"]} {money=} {dte=}'
927 f' {pos_share=} {desc["status"]} {desc["exposure_max"]}'
928 )
929
930 log.info(
931 f'Skipping: {"a" == "b" == "c" == "d"=} {desc["ms_name"]} {money=} {dte=}'
932 f' {pos_share=} {desc["status"]} {desc["exposure_max"]}'
933 )
934
935 log.info(
936 "Skipping:"
937 f' { longer_longer_longer_longer_longer_longer_name [ "db_id" ] [ "another_key" ] = : .3f }'
938 )
939
940 log.info(
941 f"""Skipping: {"a" == 'b'} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}"""
942 )
943
944 log.info(
945 f"""Skipping: {'a' == "b"=} {desc["ms_name"]} {money=} {dte=} {pos_share=} {desc["status"]} {desc["exposure_max"]}"""
946 )
947
948 log.info(
949 f"""Skipping: {'a' == 'b'} {desc['ms_name']} {money=} {dte=} {pos_share=} {desc['status']} {desc['exposure_max']}"""
950 )
951
952 x = {
953 "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
954 "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx"
955 )
956 }
957 x = {
958 "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": (
959 "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxxx{xx}xxx_xxxxx_xxxxxxxxx_xxxxxxxxxxxx_xxxx"
960 ),
961 }
962 x = {
963 "xx_xxxxx_xxxxxxxxxx_xxxxxxxxx_xx": "xx:xxxxxxxxxxxxxxxxx_xxxxx_xxxxxxx_xxxxxxxxxx"
964 }