All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
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."
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."
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)
12 print("This is a really long string inside of a print statement with no extra arguments attached at the end of it.")
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."}
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."}
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."}
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)}
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"},
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"],
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")]
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."]
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")}
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."}
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"))
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.",)
42 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.")
45 'But what should happen when code has already been formatted but in the wrong way? Like'
46 " with a space at the end instead of the beginning. Or what about when it is split too soon?"
49 bad_split2 = "But what should happen when code has already " \
50 "been formatted but in the wrong way? Like " \
51 "with a space at the end instead of the " \
52 "beginning. Or what about when it is split too " \
53 "soon? In the case of a split that is too " \
54 "short, black will try to honer the custom " \
58 "What if we have inline comments on " # First Comment
59 "each line of a bad split? In that " # Second Comment
60 "case, we should just leave it alone." # Third Comment
64 "But what should happen when code has already "
65 "been formatted but in the wrong way? Like "
66 "with a space at the end instead of the "
67 "beginning. Or what about when it is split too "
68 "soon? In the case of a split that is too "
69 "short, black will try to honer the custom "
76 long_string_kwarg="But what should happen when code has already been formatted but in the wrong way? Like "
77 "with a space at the end instead of the beginning. Or what about when it is split too "
83 "But what should happen when code has already "
84 r"been formatted but in the wrong way? Like "
85 "with a space at the end instead of the "
86 r"beginning. Or what about when it is split too "
87 r"soon? In the case of a split that is too "
88 "short, black will try to honer the custom "
96 inline_comments_func1(
97 "if there are inline "
98 "comments in the middle "
99 # Here is the standard alone comment.
100 "of the implicitly concatenated "
101 "string, we should handle "
106 inline_comments_func2(
107 "what if the string is very very very very very very very very very very long and this part does "
108 "not fit into a single line? "
109 # Here is the standard alone comment.
110 "then the string should still be properly handled by merging and splitting "
111 "it into parts that fit in line length.",
115 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."
117 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")
119 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?")
121 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")
123 old_fmt_string2 = "This is a %s %s %s %s" % ("really really really really really", "old", "way to format strings!", "Use f-strings instead!")
125 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!")
127 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."
129 fstring_with_no_fexprs = f"Some regular string that needs to get split certainly but is NOT an fstring by any means whatsoever."
131 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.
133 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.
134 "Arg #2", "Arg #3", "Arg #4", "Arg #5")
136 pragma_comment_string1 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa: E501
138 pragma_comment_string2 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa
140 """This is a really really really long triple quote string and it should not be touched."""
142 triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
144 assert some_type_of_boolean_expression, "Followed by a really really really long string that is used to provide context to the AssertionError exception."
146 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")
148 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"
150 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")
152 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.")
154 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)
156 return "A really really really really really really really really really really really really really long {} {}".format("return", "value")
159 "This is a really long string argument to a function that has a trailing comma which should NOT be there.",
163 "This is a really long string argument to a function that has a trailing comma which should NOT be there.", # comment after comma
168 "This is a really long string argument to a function that has a trailing comma"
169 " which should NOT be there."
175 "This is a really long string argument to a function that has a trailing comma"
176 " which should NOT be there."
177 ), # comment after comma
180 func_with_bad_parens_that_wont_fit_in_one_line(
181 ("short string that should have parens stripped"),
187 func_with_bad_parens_that_wont_fit_in_one_line(
190 ("short string that should have parens stripped"),
194 func_with_bad_parens(
195 ("short string that should have parens stripped"),
201 func_with_bad_parens(
204 ("short string that should have parens stripped"),
208 annotated_variable: Final = "This is a large " + STRING + " that has been " + CONCATENATED + "using the '+' operator."
209 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."
210 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."
212 backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\"
213 backslashes = "This is a really long string with \"embedded\" double quotes and 'single' quotes that also handles checking for an even number of backslashes \\\\"
214 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...\\\\\\"
228 raw_strings = r"Don't" " get" r" merged" " unless they are all raw."
231 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."
233 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."
235 long_unmergable_string_with_pragma = (
236 "This is a really long string that can't be merged because it has a likely pragma at the end" # type: ignore
240 long_unmergable_string_with_pragma = (
241 "This is a really long string that can't be merged because it has a likely pragma at the end" # noqa
245 long_unmergable_string_with_pragma = (
246 "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 string_with_nameescape = (
251 "........................................................................ \N{LAO KO LA}"
254 string_with_nameescape = (
255 "........................................................................... \N{LAO KO LA}"
258 string_with_nameescape = (
259 "............................................................................ \N{LAO KO LA}"
262 string_with_nameescape_and_escaped_backslash = (
263 "...................................................................... \\\N{LAO KO LA}"
266 string_with_nameescape_and_escaped_backslash = (
267 "......................................................................... \\\N{LAO KO LA}"
270 string_with_nameescape_and_escaped_backslash = (
271 ".......................................................................... \\\N{LAO KO LA}"
274 string_with_escaped_nameescape = (
275 "........................................................................ \\N{LAO KO LA}"
278 string_with_escaped_nameescape = (
279 "........................................................................... \\N{LAO KO LA}"
282 msg = lambda x: f"this is a very very very long lambda value {x} that doesn't fit on a single line"
284 dict_with_lambda_values = {
286 f"{j.__class__.__name__}({some_function_call(j.left)}, "
287 f"{some_function_call(j.right)})"
291 # Complex string concatenations with a method call in the middle.
296 " (%r, self.%s, visitor.%s)"
297 % (attrname, attrname, visit_name)
298 for attrname, visit_name in names
305 # Test case of an outer string' parens enclose an inner string's parens.
306 call(body=("%s %s" % ((",".join(items)), suffix)))
313 "This is a really long string that can't possibly be expected to fit all together"
314 " on one line. In fact it may even take up three or more lines... like four or"
315 " five... but probably just three."
319 "This is a really long string that can't possibly be expected to fit all together"
320 " on one line. In fact it may even take up three or more lines... like four or"
321 " five... but probably just three."
327 "This is a really long string inside of a print statement with extra arguments"
328 " attached at the end of it.",
335 "This is a really long string inside of a print statement with no extra arguments"
336 " attached at the end of it."
341 "This is a really long string that can't possibly be expected to fit all"
342 " together on one line. Also it is inside a dictionary, so formatting is more"
346 "This is another really really (not really) long string that also can't be"
347 " expected to fit on one line and is, like the other string, inside a"
354 "This is a really long string that can't possibly be expected to fit all"
355 " together on one line. Also it is inside a dictionary, so formatting is more"
359 "This is another really really (not really) long string that also can't be"
360 " expected to fit on one line and is, like the other string, inside a"
367 "This is a really long string that can't possibly be expected to fit all"
368 " together on one line. Also it is inside a dictionary, so formatting is more"
372 "This is another really really (not really) long string that also can't be"
373 " expected to fit on one line and is, like the other string, inside a"
379 "A long and ridiculous {}".format(string_key): (
380 "This is a really really really long string that has to go i,side of a"
381 " dictionary. It is soooo bad."
383 some_func("calling", "some", "stuff"): (
384 "This is a really really really long string that has to go inside of a"
385 " dictionary. It is {soooo} bad (#{x}).".format(sooo="soooo", x=2)
388 % ("formatted", "string"): (
389 "This is a really really really long string that has to go inside of a"
390 " dictionary. It is %s bad (#%d)." % ("soooo", 2)
394 D5 = { # Test for https://github.com/psf/black/issues/3261
395 "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": {
400 D6 = { # Test for https://github.com/psf/black/issues/3261
401 "This is a really long string that can't be expected to fit in one line and is used as a dict's key": [
408 "The is a short string",
410 "This is a really long string that can't possibly be expected to fit all"
411 " together on one line. Also it is inside a list literal, so it's expected to"
412 " be wrapped in parens when splitting to avoid implicit str concatenation."
414 short_call("arg", {"key": "value"}),
416 "This is another really really (not really) long string that also can't be"
417 " expected to fit on one line and is, like the other string, inside a list"
420 "parens should be stripped for short string in list",
424 "This is a really long string that can't be expected to fit in one line and is the"
425 " only child of a list literal."
429 "The is a short string",
431 "This is a really long string that can't possibly be expected to fit all"
432 " together on one line. Also it is inside a set literal, so it's expected to be"
433 " wrapped in parens when splitting to avoid implicit str concatenation."
435 short_call("arg", {"key": "value"}),
437 "This is another really really (not really) long string that also can't be"
438 " expected to fit on one line and is, like the other string, inside a set"
441 "parens should be stripped for short string in set",
445 "This is a really long string that can't be expected to fit in one line and is the"
446 " only child of a set literal."
450 "The is a short string",
452 "This is a really long string that can't possibly be expected to fit all"
453 " together on one line. Also it is inside a tuple literal, so it's expected to"
454 " be wrapped in parens when splitting to avoid implicit str concatenation."
456 short_call("arg", {"key": "value"}),
458 "This is another really really (not really) long string that also can't be"
459 " expected to fit on one line and is, like the other string, inside a tuple"
462 "parens should be stripped for short string in list",
467 "This is a really long string that can't be expected to fit in one line and is"
468 " the only child of a tuple literal."
475 "Long keyword strings also need to be wrapped, but they will probably need to"
476 " be handled a little bit differently."
481 "But what should happen when code has already been formatted but in the wrong way?"
482 " Like with a space at the end instead of the beginning. Or what about when it is"
487 "But what should happen when code has already "
488 "been formatted but in the wrong way? Like "
489 "with a space at the end instead of the "
490 "beginning. Or what about when it is split too "
491 "soon? In the case of a split that is too "
492 "short, black will try to honer the custom "
497 "What if we have inline comments on " # First Comment
498 "each line of a bad split? In that " # Second Comment
499 "case, we should just leave it alone." # Third Comment
503 "But what should happen when code has already "
504 "been formatted but in the wrong way? Like "
505 "with a space at the end instead of the "
506 "beginning. Or what about when it is split too "
507 "soon? In the case of a split that is too "
508 "short, black will try to honer the custom "
520 "But what should happen when code has already been formatted but in the wrong"
521 " way? Like with a space at the end instead of the beginning. Or what about"
522 " when it is split too soon?"
528 "But what should happen when code has already "
529 r"been formatted but in the wrong way? Like "
530 "with a space at the end instead of the "
531 r"beginning. Or what about when it is split too "
532 r"soon? In the case of a split that is too "
533 "short, black will try to honer the custom "
541 inline_comments_func1(
542 "if there are inline comments in the middle "
543 # Here is the standard alone comment.
544 "of the implicitly concatenated string, we should handle them correctly",
548 inline_comments_func2(
549 "what if the string is very very very very very very very very very very long and"
550 " this part does not fit into a single line? "
551 # Here is the standard alone comment.
552 "then the string should still be properly handled by merging and splitting "
553 "it into parts that fit in line length.",
558 r"This is a long raw string. When re-formatting this string, black needs to make"
559 r" sure it prepends the 'r' onto the new string."
563 "We also need to be sure to preserve any and all {} which may or may not be"
564 " attached to the string in question.".format("method calls")
567 fmt_string2 = "But what about when the string is {} but {}".format(
569 "the method call is really really really really really really really really long?",
573 "While we are on the topic of %s, we should also note that old-style formatting"
574 " must also be preserved, since some %s still uses it." % ("formatting", "code")
577 old_fmt_string2 = "This is a %s %s %s %s" % (
578 "really really really really really",
580 "way to format strings!",
581 "Use f-strings instead!",
585 "Whereas only the strings after the percent sign were long in the last example,"
586 " this example uses a long initial string as well. This is another %s %s %s %s"
588 "really really really really really",
590 "way to format strings!",
591 "Use f-strings instead!",
596 f"f-strings definitely make things more {difficult} than they need to be for"
597 " {black}. But boy they sure are handy. The problem is that some lines will need"
598 f" to have the 'f' whereas others do not. This {line}, for example, needs one."
601 fstring_with_no_fexprs = (
602 f"Some regular string that needs to get split certainly but is NOT an fstring by"
603 f" any means whatsoever."
606 comment_string = ( # This comment gets thrown to the top.
607 "Long lines with inline comments should have their comments appended to the"
608 " reformatted string's enclosing right parentheses."
611 arg_comment_string = print(
612 "Long lines with inline comments which are apart of (and not the only member of) an"
613 " argument list should have their comments appended to the reformatted string's"
614 " enclosing left parentheses.", # This comment gets thrown to the top.
621 pragma_comment_string1 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa: E501
623 pragma_comment_string2 = "Lines which end with an inline pragma comment of the form `# <pragma>: <...>` should be left alone." # noqa
625 """This is a really really really long triple quote string and it should not be touched."""
627 triple_quote_string = """This is a really really really long triple quote string assignment and it should not be touched."""
629 assert some_type_of_boolean_expression, (
630 "Followed by a really really really long string that is used to provide context to"
631 " the AssertionError exception."
634 assert some_type_of_boolean_expression, (
635 "Followed by a really really really long string that is used to provide context to"
636 " the AssertionError exception, which uses dynamic string {}.".format("formatting")
639 assert some_type_of_boolean_expression, (
640 "Followed by a really really really long string that is used to provide context to"
641 " the AssertionError exception, which uses dynamic string %s." % "formatting"
644 assert some_type_of_boolean_expression, (
645 "Followed by a really really really long string that is used to provide context to"
646 " the AssertionError exception, which uses dynamic %s %s."
647 % ("string", "formatting")
651 "With a reallly generic name and with a really really long string that is, at some"
652 " point down the line, "
654 + " to a variable and then added to another string."
658 "With a reallly generic name and with a really really long string that is, at some"
659 " point down the line, "
661 + " to a variable and then added to another string. But then what happens when the"
662 " final string is also supppppperrrrr long?! Well then that second (realllllllly"
663 " long) string should be split too.",
664 "and a second argument",
669 "A really really really really really really really really really really really"
670 " really really long {} {}".format("return", "value")
674 "This is a really long string argument to a function that has a trailing comma"
675 " which should NOT be there.",
679 "This is a really long string argument to a function that has a trailing comma"
680 " which should NOT be there.", # comment after comma
684 "This is a really long string argument to a function that has a trailing comma"
685 " which should NOT be there.",
689 "This is a really long string argument to a function that has a trailing comma"
690 " which should NOT be there.", # comment after comma
693 func_with_bad_parens_that_wont_fit_in_one_line(
694 "short string that should have parens stripped", x, y, z
697 func_with_bad_parens_that_wont_fit_in_one_line(
698 x, y, "short string that should have parens stripped", z
701 func_with_bad_parens(
702 "short string that should have parens stripped",
708 func_with_bad_parens(
711 "short string that should have parens stripped",
715 annotated_variable: Final = (
720 + "using the '+' operator."
722 annotated_variable: Final = (
723 "This is a large string that has a type annotation attached to it. A type"
724 " annotation should NOT stop a long string from being wrapped."
726 annotated_variable: Literal["fakse_literal"] = (
727 "This is a large string that has a type annotation attached to it. A type"
728 " annotation should NOT stop a long string from being wrapped."
732 "This is a really long string with \"embedded\" double quotes and 'single' quotes"
733 " that also handles checking for an even number of backslashes \\"
736 "This is a really long string with \"embedded\" double quotes and 'single' quotes"
737 " that also handles checking for an even number of backslashes \\\\"
740 "This is a really 'long' string with \"embedded double quotes\" and 'single' quotes"
741 ' that also handles checking for an odd number of backslashes \\", like'
745 short_string = "Hi there."
747 func_call(short_string="Hi there.")
749 raw_strings = r"Don't" " get" r" merged" " unless they are all raw."
754 "This is a really long string that can't possibly be expected to fit all"
755 " together on one line. In fact it may even take up three or more lines... like"
756 " four or five... but probably just three."
761 "This is a {really} long string that needs to be split without a doubt (i.e."
762 f" most definitely). In short, this {string} that can't possibly be {{expected}} to"
763 f" fit all together on one line. In {fact} it may even take up three or more"
764 " lines... like four or five... but probably just four."
767 long_unmergable_string_with_pragma = (
768 "This is a really long string that can't be merged because it has a likely pragma at the end" # type: ignore
772 long_unmergable_string_with_pragma = (
773 "This is a really long string that can't be merged because it has a likely pragma at the end" # noqa
777 long_unmergable_string_with_pragma = (
778 "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
782 string_with_nameescape = (
783 "........................................................................"
787 string_with_nameescape = (
788 "..........................................................................."
792 string_with_nameescape = (
793 "............................................................................"
797 string_with_nameescape_and_escaped_backslash = (
798 "......................................................................"
802 string_with_nameescape_and_escaped_backslash = (
803 "........................................................................."
807 string_with_nameescape_and_escaped_backslash = (
808 ".........................................................................."
812 string_with_escaped_nameescape = (
813 "........................................................................ \\N{LAO"
817 string_with_escaped_nameescape = (
818 "..........................................................................."
824 f"this is a very very very long lambda value {x} that doesn't fit on a single"
829 dict_with_lambda_values = {
831 f"{j.__class__.__name__}({some_function_call(j.left)}, "
832 f"{some_function_call(j.right)})"
836 # Complex string concatenations with a method call in the middle.
840 " (%r, self.%s, visitor.%s)" % (attrname, attrname, visit_name)
841 for attrname, visit_name in names
847 # Test case of an outer string' parens enclose an inner string's parens.
848 call(body="%s %s" % (",".join(items), suffix))