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 String transformers that can split and merge strings.
6 from abc import ABC, abstractmethod
7 from collections import defaultdict
8 from dataclasses import dataclass
26 if sys.version_info < (3, 8):
27 from typing_extensions import Final, Literal
29 from typing import Literal, Final
31 from mypy_extensions import trait
33 from black.brackets import BracketMatchError
34 from black.comments import contains_pragma_comment
35 from black.lines import Line, append_leaves
36 from black.mode import Feature
37 from black.nodes import (
48 from black.rusty import Err, Ok, Result
49 from black.strings import (
50 assert_is_leaf_string,
53 normalize_string_quotes,
55 from blib2to3.pgen2 import token
56 from blib2to3.pytree import Leaf, Node
59 class CannotTransform(Exception):
60 """Base class for errors raised by Transformers."""
65 LN = Union[Leaf, Node]
66 Transformer = Callable[[Line, Collection[Feature]], Iterator[Line]]
71 TResult = Result[T, CannotTransform] # (T)ransform Result
72 TMatchResult = TResult[Index]
75 def TErr(err_msg: str) -> Err[CannotTransform]:
78 Convenience function used when working with the TResult type.
80 cant_transform = CannotTransform(err_msg)
81 return Err(cant_transform)
84 def hug_power_op(line: Line, features: Collection[Feature]) -> Iterator[Line]:
85 """A transformer which normalizes spacing around power operators."""
87 # Performance optimization to avoid unnecessary Leaf clones and other ops.
88 for leaf in line.leaves:
89 if leaf.type == token.DOUBLESTAR:
92 raise CannotTransform("No doublestar token was found in the line.")
94 def is_simple_lookup(index: int, step: Literal[1, -1]) -> bool:
95 # Brackets and parentheses indicate calls, subscripts, etc. ...
96 # basically stuff that doesn't count as "simple". Only a NAME lookup
97 # or dotted lookup (eg. NAME.NAME) is OK.
99 disallowed = {token.RPAR, token.RSQB}
101 disallowed = {token.LPAR, token.LSQB}
103 while 0 <= index < len(line.leaves):
104 current = line.leaves[index]
105 if current.type in disallowed:
107 if current.type not in {token.NAME, token.DOT} or current.value == "for":
108 # If the current token isn't disallowed, we'll assume this is simple as
109 # only the disallowed tokens are semantically attached to this lookup
110 # expression we're checking. Also, stop early if we hit the 'for' bit
111 # of a comprehension.
118 def is_simple_operand(index: int, kind: Literal["base", "exponent"]) -> bool:
119 # An operand is considered "simple" if's a NAME, a numeric CONSTANT, a simple
120 # lookup (see above), with or without a preceding unary operator.
121 start = line.leaves[index]
122 if start.type in {token.NAME, token.NUMBER}:
123 return is_simple_lookup(index, step=(1 if kind == "exponent" else -1))
125 if start.type in {token.PLUS, token.MINUS, token.TILDE}:
126 if line.leaves[index + 1].type in {token.NAME, token.NUMBER}:
127 # step is always one as bases with a preceding unary op will be checked
128 # for simplicity starting from the next token (so it'll hit the check
130 return is_simple_lookup(index + 1, step=1)
134 new_line = line.clone()
136 for idx, leaf in enumerate(line.leaves):
137 new_leaf = leaf.clone()
143 (0 < idx < len(line.leaves) - 1)
144 and leaf.type == token.DOUBLESTAR
145 and is_simple_operand(idx - 1, kind="base")
146 and line.leaves[idx - 1].value != "lambda"
147 and is_simple_operand(idx + 1, kind="exponent")
152 # We have to be careful to make a new line properly:
153 # - bracket related metadata must be maintained (handled by Line.append)
154 # - comments need to copied over, updating the leaf IDs they're attached to
155 new_line.append(new_leaf, preformatted=True)
156 for comment_leaf in line.comments_after(leaf):
157 new_line.append(comment_leaf, preformatted=True)
162 class StringTransformer(ABC):
164 An implementation of the Transformer protocol that relies on its
165 subclasses overriding the template methods `do_match(...)` and
168 This Transformer works exclusively on strings (for example, by merging
171 The following sections can be found among the docstrings of each concrete
172 StringTransformer subclass.
175 Which requirements must be met of the given Line for this
176 StringTransformer to be applied?
179 If the given Line meets all of the above requirements, which string
180 transformations can you expect to be applied to it by this
184 What contractual agreements does this StringTransformer have with other
185 StringTransfomers? Such collaborations should be eliminated/minimized
189 __name__: Final = "StringTransformer"
191 # Ideally this would be a dataclass, but unfortunately mypyc breaks when used with
193 def __init__(self, line_length: int, normalize_strings: bool) -> None:
194 self.line_length = line_length
195 self.normalize_strings = normalize_strings
198 def do_match(self, line: Line) -> TMatchResult:
201 * Ok(string_idx) such that `line.leaves[string_idx]` is our target
202 string, if a match was able to be made.
204 * Err(CannotTransform), if a match was not able to be made.
208 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
211 * Ok(new_line) where new_line is the new transformed line.
213 * Err(CannotTransform) if the transformation failed for some reason. The
214 `do_match(...)` template method should usually be used to reject
215 the form of the given Line, but in some cases it is difficult to
216 know whether or not a Line meets the StringTransformer's
217 requirements until the transformation is already midway.
220 This method should NOT mutate @line directly, but it MAY mutate the
221 Line's underlying Node structure. (WARNING: If the underlying Node
222 structure IS altered, then this method should NOT be allowed to
223 yield an CannotTransform after that point.)
226 def __call__(self, line: Line, _features: Collection[Feature]) -> Iterator[Line]:
228 StringTransformer instances have a call signature that mirrors that of
229 the Transformer type.
232 CannotTransform(...) if the concrete StringTransformer class is unable
235 # Optimization to avoid calling `self.do_match(...)` when the line does
236 # not contain any string.
237 if not any(leaf.type == token.STRING for leaf in line.leaves):
238 raise CannotTransform("There are no strings in this line.")
240 match_result = self.do_match(line)
242 if isinstance(match_result, Err):
243 cant_transform = match_result.err()
244 raise CannotTransform(
245 f"The string transformer {self.__class__.__name__} does not recognize"
246 " this line as one that it can transform."
247 ) from cant_transform
249 string_idx = match_result.ok()
251 for line_result in self.do_transform(line, string_idx):
252 if isinstance(line_result, Err):
253 cant_transform = line_result.err()
254 raise CannotTransform(
255 "StringTransformer failed while attempting to transform string."
256 ) from cant_transform
257 line = line_result.ok()
263 """A custom (i.e. manual) string split.
265 A single CustomSplit instance represents a single substring.
268 Consider the following string:
275 This string will correspond to the following three CustomSplit instances:
277 CustomSplit(False, 16)
278 CustomSplit(False, 17)
279 CustomSplit(True, 16)
288 class CustomSplitMapMixin:
290 This mixin class is used to map merged strings to a sequence of
291 CustomSplits, which will then be used to re-split the strings iff none of
292 the resultant substrings go over the configured max line length.
295 _Key: ClassVar = Tuple[StringID, str]
296 _CUSTOM_SPLIT_MAP: ClassVar[Dict[_Key, Tuple[CustomSplit, ...]]] = defaultdict(
301 def _get_key(string: str) -> "CustomSplitMapMixin._Key":
304 A unique identifier that is used internally to map @string to a
305 group of custom splits.
307 return (id(string), string)
309 def add_custom_splits(
310 self, string: str, custom_splits: Iterable[CustomSplit]
312 """Custom Split Map Setter Method
315 Adds a mapping from @string to the custom splits @custom_splits.
317 key = self._get_key(string)
318 self._CUSTOM_SPLIT_MAP[key] = tuple(custom_splits)
320 def pop_custom_splits(self, string: str) -> List[CustomSplit]:
321 """Custom Split Map Getter Method
324 * A list of the custom splits that are mapped to @string, if any
330 Deletes the mapping between @string and its associated custom
331 splits (which are returned to the caller).
333 key = self._get_key(string)
335 custom_splits = self._CUSTOM_SPLIT_MAP[key]
336 del self._CUSTOM_SPLIT_MAP[key]
338 return list(custom_splits)
340 def has_custom_splits(self, string: str) -> bool:
343 True iff @string is associated with a set of custom splits.
345 key = self._get_key(string)
346 return key in self._CUSTOM_SPLIT_MAP
349 class StringMerger(StringTransformer, CustomSplitMapMixin):
350 """StringTransformer that merges strings together.
353 (A) The line contains adjacent strings such that ALL of the validation checks
354 listed in StringMerger.__validate_msg(...)'s docstring pass.
356 (B) The line contains a string which uses line continuation backslashes.
359 Depending on which of the two requirements above where met, either:
361 (A) The string group associated with the target string is merged.
363 (B) All line-continuation backslashes are removed from the target string.
366 StringMerger provides custom split information to StringSplitter.
369 def do_match(self, line: Line) -> TMatchResult:
372 is_valid_index = is_valid_index_factory(LL)
374 for i, leaf in enumerate(LL):
376 leaf.type == token.STRING
377 and is_valid_index(i + 1)
378 and LL[i + 1].type == token.STRING
382 if leaf.type == token.STRING and "\\\n" in leaf.value:
385 return TErr("This line has no strings that need merging.")
387 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
389 rblc_result = self._remove_backslash_line_continuation_chars(
392 if isinstance(rblc_result, Ok):
393 new_line = rblc_result.ok()
395 msg_result = self._merge_string_group(new_line, string_idx)
396 if isinstance(msg_result, Ok):
397 new_line = msg_result.ok()
399 if isinstance(rblc_result, Err) and isinstance(msg_result, Err):
400 msg_cant_transform = msg_result.err()
401 rblc_cant_transform = rblc_result.err()
402 cant_transform = CannotTransform(
403 "StringMerger failed to merge any strings in this line."
406 # Chain the errors together using `__cause__`.
407 msg_cant_transform.__cause__ = rblc_cant_transform
408 cant_transform.__cause__ = msg_cant_transform
410 yield Err(cant_transform)
415 def _remove_backslash_line_continuation_chars(
416 line: Line, string_idx: int
419 Merge strings that were split across multiple lines using
420 line-continuation backslashes.
423 Ok(new_line), if @line contains backslash line-continuation
426 Err(CannotTransform), otherwise.
430 string_leaf = LL[string_idx]
432 string_leaf.type == token.STRING
433 and "\\\n" in string_leaf.value
434 and not has_triple_quotes(string_leaf.value)
437 f"String leaf {string_leaf} does not contain any backslash line"
438 " continuation characters."
441 new_line = line.clone()
442 new_line.comments = line.comments.copy()
443 append_leaves(new_line, line, LL)
445 new_string_leaf = new_line.leaves[string_idx]
446 new_string_leaf.value = new_string_leaf.value.replace("\\\n", "")
450 def _merge_string_group(self, line: Line, string_idx: int) -> TResult[Line]:
452 Merges string group (i.e. set of adjacent strings) where the first
453 string in the group is `line.leaves[string_idx]`.
456 Ok(new_line), if ALL of the validation checks found in
457 __validate_msg(...) pass.
459 Err(CannotTransform), otherwise.
463 is_valid_index = is_valid_index_factory(LL)
465 vresult = self._validate_msg(line, string_idx)
466 if isinstance(vresult, Err):
469 # If the string group is wrapped inside an Atom node, we must make sure
470 # to later replace that Atom with our new (merged) string leaf.
471 atom_node = LL[string_idx].parent
473 # We will place BREAK_MARK in between every two substrings that we
474 # merge. We will then later go through our final result and use the
475 # various instances of BREAK_MARK we find to add the right values to
476 # the custom split map.
477 BREAK_MARK = "@@@@@ BLACK BREAKPOINT MARKER @@@@@"
479 QUOTE = LL[string_idx].value[-1]
481 def make_naked(string: str, string_prefix: str) -> str:
482 """Strip @string (i.e. make it a "naked" string)
485 * assert_is_leaf_string(@string)
488 A string that is identical to @string except that
489 @string_prefix has been stripped, the surrounding QUOTE
490 characters have been removed, and any remaining QUOTE
491 characters have been escaped.
493 assert_is_leaf_string(string)
495 RE_EVEN_BACKSLASHES = r"(?:(?<!\\)(?:\\\\)*)"
496 naked_string = string[len(string_prefix) + 1 : -1]
497 naked_string = re.sub(
498 "(" + RE_EVEN_BACKSLASHES + ")" + QUOTE, r"\1\\" + QUOTE, naked_string
502 # Holds the CustomSplit objects that will later be added to the custom
506 # Temporary storage for the 'has_prefix' part of the CustomSplit objects.
509 # Sets the 'prefix' variable. This is the prefix that the final merged
511 next_str_idx = string_idx
515 and is_valid_index(next_str_idx)
516 and LL[next_str_idx].type == token.STRING
518 prefix = get_string_prefix(LL[next_str_idx].value).lower()
521 # The next loop merges the string group. The final string will be
524 # The following convenience variables are used:
529 # NSS: naked next string
533 next_str_idx = string_idx
534 while is_valid_index(next_str_idx) and LL[next_str_idx].type == token.STRING:
537 SS = LL[next_str_idx].value
538 next_prefix = get_string_prefix(SS).lower()
540 # If this is an f-string group but this substring is not prefixed
542 if "f" in prefix and "f" not in next_prefix:
543 # Then we must escape any braces contained in this substring.
544 SS = re.sub(r"(\{|\})", r"\1\1", SS)
546 NSS = make_naked(SS, next_prefix)
548 has_prefix = bool(next_prefix)
549 prefix_tracker.append(has_prefix)
551 S = prefix + QUOTE + NS + NSS + BREAK_MARK + QUOTE
552 NS = make_naked(S, prefix)
556 S_leaf = Leaf(token.STRING, S)
557 if self.normalize_strings:
558 S_leaf.value = normalize_string_quotes(S_leaf.value)
560 # Fill the 'custom_splits' list with the appropriate CustomSplit objects.
561 temp_string = S_leaf.value[len(prefix) + 1 : -1]
562 for has_prefix in prefix_tracker:
563 mark_idx = temp_string.find(BREAK_MARK)
566 ), "Logic error while filling the custom string breakpoint cache."
568 temp_string = temp_string[mark_idx + len(BREAK_MARK) :]
569 breakpoint_idx = mark_idx + (len(prefix) if has_prefix else 0) + 1
570 custom_splits.append(CustomSplit(has_prefix, breakpoint_idx))
572 string_leaf = Leaf(token.STRING, S_leaf.value.replace(BREAK_MARK, ""))
574 if atom_node is not None:
575 replace_child(atom_node, string_leaf)
577 # Build the final line ('new_line') that this method will later return.
578 new_line = line.clone()
579 for i, leaf in enumerate(LL):
581 new_line.append(string_leaf)
583 if string_idx <= i < string_idx + num_of_strings:
584 for comment_leaf in line.comments_after(LL[i]):
585 new_line.append(comment_leaf, preformatted=True)
588 append_leaves(new_line, line, [leaf])
590 self.add_custom_splits(string_leaf.value, custom_splits)
594 def _validate_msg(line: Line, string_idx: int) -> TResult[None]:
595 """Validate (M)erge (S)tring (G)roup
597 Transform-time string validation logic for __merge_string_group(...).
600 * Ok(None), if ALL validation checks (listed below) pass.
602 * Err(CannotTransform), if any of the following are true:
603 - The target string group does not contain ANY stand-alone comments.
604 - The target string is not in a string group (i.e. it has no
606 - The string group has more than one inline comment.
607 - The string group has an inline comment that appears to be a pragma.
608 - The set of all string prefixes in the string group is of
609 length greater than one and is not equal to {"", "f"}.
610 - The string group consists of raw strings.
612 # We first check for "inner" stand-alone comments (i.e. stand-alone
613 # comments that have a string leaf before them AND after them).
616 found_sa_comment = False
617 is_valid_index = is_valid_index_factory(line.leaves)
618 while is_valid_index(i) and line.leaves[i].type in [
622 if line.leaves[i].type == STANDALONE_COMMENT:
623 found_sa_comment = True
624 elif found_sa_comment:
626 "StringMerger does NOT merge string groups which contain "
627 "stand-alone comments."
632 num_of_inline_string_comments = 0
633 set_of_prefixes = set()
635 for leaf in line.leaves[string_idx:]:
636 if leaf.type != token.STRING:
637 # If the string group is trailed by a comma, we count the
638 # comments trailing the comma to be one of the string group's
640 if leaf.type == token.COMMA and id(leaf) in line.comments:
641 num_of_inline_string_comments += 1
644 if has_triple_quotes(leaf.value):
645 return TErr("StringMerger does NOT merge multiline strings.")
648 prefix = get_string_prefix(leaf.value).lower()
650 return TErr("StringMerger does NOT merge raw strings.")
652 set_of_prefixes.add(prefix)
654 if id(leaf) in line.comments:
655 num_of_inline_string_comments += 1
656 if contains_pragma_comment(line.comments[id(leaf)]):
657 return TErr("Cannot merge strings which have pragma comments.")
659 if num_of_strings < 2:
661 f"Not enough strings to merge (num_of_strings={num_of_strings})."
664 if num_of_inline_string_comments > 1:
666 f"Too many inline string comments ({num_of_inline_string_comments})."
669 if len(set_of_prefixes) > 1 and set_of_prefixes != {"", "f"}:
670 return TErr(f"Too many different prefixes ({set_of_prefixes}).")
675 class StringParenStripper(StringTransformer):
676 """StringTransformer that strips surrounding parentheses from strings.
679 The line contains a string which is surrounded by parentheses and:
680 - The target string is NOT the only argument to a function call.
681 - The target string is NOT a "pointless" string.
682 - If the target string contains a PERCENT, the brackets are not
683 preceded or followed by an operator with higher precedence than
687 The parentheses mentioned in the 'Requirements' section are stripped.
690 StringParenStripper has its own inherent usefulness, but it is also
691 relied on to clean up the parentheses created by StringParenWrapper (in
692 the event that they are no longer needed).
695 def do_match(self, line: Line) -> TMatchResult:
698 is_valid_index = is_valid_index_factory(LL)
700 for idx, leaf in enumerate(LL):
701 # Should be a string...
702 if leaf.type != token.STRING:
705 # If this is a "pointless" string...
708 and leaf.parent.parent
709 and leaf.parent.parent.type == syms.simple_stmt
713 # Should be preceded by a non-empty LPAR...
715 not is_valid_index(idx - 1)
716 or LL[idx - 1].type != token.LPAR
717 or is_empty_lpar(LL[idx - 1])
721 # That LPAR should NOT be preceded by a function name or a closing
722 # bracket (which could be a function which returns a function or a
723 # list/dictionary that contains a function)...
724 if is_valid_index(idx - 2) and (
725 LL[idx - 2].type == token.NAME or LL[idx - 2].type in CLOSING_BRACKETS
731 # Skip the string trailer, if one exists.
732 string_parser = StringParser()
733 next_idx = string_parser.parse(LL, string_idx)
735 # if the leaves in the parsed string include a PERCENT, we need to
736 # make sure the initial LPAR is NOT preceded by an operator with
737 # higher or equal precedence to PERCENT
738 if is_valid_index(idx - 2):
739 # mypy can't quite follow unless we name this
740 before_lpar = LL[idx - 2]
741 if token.PERCENT in {leaf.type for leaf in LL[idx - 1 : next_idx]} and (
758 # only unary PLUS/MINUS
760 and before_lpar.parent.type == syms.factor
761 and (before_lpar.type in {token.PLUS, token.MINUS})
766 # Should be followed by a non-empty RPAR...
768 is_valid_index(next_idx)
769 and LL[next_idx].type == token.RPAR
770 and not is_empty_rpar(LL[next_idx])
772 # That RPAR should NOT be followed by anything with higher
773 # precedence than PERCENT
774 if is_valid_index(next_idx + 1) and LL[next_idx + 1].type in {
782 return Ok(string_idx)
784 return TErr("This line has no strings wrapped in parens.")
786 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
789 string_parser = StringParser()
790 rpar_idx = string_parser.parse(LL, string_idx)
792 for leaf in (LL[string_idx - 1], LL[rpar_idx]):
793 if line.comments_after(leaf):
795 "Will not strip parentheses which have comments attached to them."
799 new_line = line.clone()
800 new_line.comments = line.comments.copy()
802 append_leaves(new_line, line, LL[: string_idx - 1])
803 except BracketMatchError:
804 # HACK: I believe there is currently a bug somewhere in
805 # right_hand_split() that is causing brackets to not be tracked
806 # properly by a shared BracketTracker.
807 append_leaves(new_line, line, LL[: string_idx - 1], preformatted=True)
809 string_leaf = Leaf(token.STRING, LL[string_idx].value)
810 LL[string_idx - 1].remove()
811 replace_child(LL[string_idx], string_leaf)
812 new_line.append(string_leaf)
815 new_line, line, LL[string_idx + 1 : rpar_idx] + LL[rpar_idx + 1 :]
818 LL[rpar_idx].remove()
823 class BaseStringSplitter(StringTransformer):
825 Abstract class for StringTransformers which transform a Line's strings by splitting
826 them or placing them on their own lines where necessary to avoid going over
827 the configured line length.
830 * The target string value is responsible for the line going over the
831 line length limit. It follows that after all of black's other line
832 split methods have been exhausted, this line (or one of the resulting
833 lines after all line splits are performed) would still be over the
834 line_length limit unless we split this string.
836 * The target string is NOT a "pointless" string (i.e. a string that has
837 no parent or siblings).
839 * The target string is not followed by an inline comment that appears
842 * The target string is not a multiline (i.e. triple-quote) string.
845 STRING_OPERATORS: Final = [
858 def do_splitter_match(self, line: Line) -> TMatchResult:
860 BaseStringSplitter asks its clients to override this method instead of
861 `StringTransformer.do_match(...)`.
863 Follows the same protocol as `StringTransformer.do_match(...)`.
865 Refer to `help(StringTransformer.do_match)` for more information.
868 def do_match(self, line: Line) -> TMatchResult:
869 match_result = self.do_splitter_match(line)
870 if isinstance(match_result, Err):
873 string_idx = match_result.ok()
874 vresult = self._validate(line, string_idx)
875 if isinstance(vresult, Err):
880 def _validate(self, line: Line, string_idx: int) -> TResult[None]:
882 Checks that @line meets all of the requirements listed in this classes'
883 docstring. Refer to `help(BaseStringSplitter)` for a detailed
884 description of those requirements.
887 * Ok(None), if ALL of the requirements are met.
889 * Err(CannotTransform), if ANY of the requirements are NOT met.
893 string_leaf = LL[string_idx]
895 max_string_length = self._get_max_string_length(line, string_idx)
896 if len(string_leaf.value) <= max_string_length:
898 "The string itself is not what is causing this line to be too long."
901 if not string_leaf.parent or [L.type for L in string_leaf.parent.children] == [
906 f"This string ({string_leaf.value}) appears to be pointless (i.e. has"
910 if id(line.leaves[string_idx]) in line.comments and contains_pragma_comment(
911 line.comments[id(line.leaves[string_idx])]
914 "Line appears to end with an inline pragma comment. Splitting the line"
915 " could modify the pragma's behavior."
918 if has_triple_quotes(string_leaf.value):
919 return TErr("We cannot split multiline strings.")
923 def _get_max_string_length(self, line: Line, string_idx: int) -> int:
925 Calculates the max string length used when attempting to determine
926 whether or not the target string is responsible for causing the line to
927 go over the line length limit.
929 WARNING: This method is tightly coupled to both StringSplitter and
930 (especially) StringParenWrapper. There is probably a better way to
931 accomplish what is being done here.
934 max_string_length: such that `line.leaves[string_idx].value >
935 max_string_length` implies that the target string IS responsible
936 for causing this line to exceed the line length limit.
940 is_valid_index = is_valid_index_factory(LL)
942 # We use the shorthand "WMA4" in comments to abbreviate "We must
943 # account for". When giving examples, we use STRING to mean some/any
946 # Finally, we use the following convenience variables:
948 # P: The leaf that is before the target string leaf.
949 # N: The leaf that is after the target string leaf.
950 # NN: The leaf that is after N.
952 # WMA4 the whitespace at the beginning of the line.
953 offset = line.depth * 4
955 if is_valid_index(string_idx - 1):
956 p_idx = string_idx - 1
958 LL[string_idx - 1].type == token.LPAR
959 and LL[string_idx - 1].value == ""
962 # If the previous leaf is an empty LPAR placeholder, we should skip it.
966 if P.type in self.STRING_OPERATORS:
967 # WMA4 a space and a string operator (e.g. `+ STRING` or `== STRING`).
968 offset += len(str(P)) + 1
970 if P.type == token.COMMA:
971 # WMA4 a space, a comma, and a closing bracket [e.g. `), STRING`].
974 if P.type in [token.COLON, token.EQUAL, token.PLUSEQUAL, token.NAME]:
975 # This conditional branch is meant to handle dictionary keys,
976 # variable assignments, 'return STRING' statement lines, and
977 # 'else STRING' ternary expression lines.
979 # WMA4 a single space.
982 # WMA4 the lengths of any leaves that came before that space,
983 # but after any closing bracket before that space.
984 for leaf in reversed(LL[: p_idx + 1]):
985 offset += len(str(leaf))
986 if leaf.type in CLOSING_BRACKETS:
989 if is_valid_index(string_idx + 1):
990 N = LL[string_idx + 1]
991 if N.type == token.RPAR and N.value == "" and len(LL) > string_idx + 2:
992 # If the next leaf is an empty RPAR placeholder, we should skip it.
993 N = LL[string_idx + 2]
995 if N.type == token.COMMA:
996 # WMA4 a single comma at the end of the string (e.g `STRING,`).
999 if is_valid_index(string_idx + 2):
1000 NN = LL[string_idx + 2]
1002 if N.type == token.DOT and NN.type == token.NAME:
1003 # This conditional branch is meant to handle method calls invoked
1004 # off of a string literal up to and including the LPAR character.
1006 # WMA4 the '.' character.
1010 is_valid_index(string_idx + 3)
1011 and LL[string_idx + 3].type == token.LPAR
1013 # WMA4 the left parenthesis character.
1016 # WMA4 the length of the method's name.
1017 offset += len(NN.value)
1019 has_comments = False
1020 for comment_leaf in line.comments_after(LL[string_idx]):
1021 if not has_comments:
1023 # WMA4 two spaces before the '#' character.
1026 # WMA4 the length of the inline comment.
1027 offset += len(comment_leaf.value)
1029 max_string_length = self.line_length - offset
1030 return max_string_length
1033 def iter_fexpr_spans(s: str) -> Iterator[Tuple[int, int]]:
1035 Yields spans corresponding to expressions in a given f-string.
1036 Spans are half-open ranges (left inclusive, right exclusive).
1037 Assumes the input string is a valid f-string, but will not crash if the input
1040 stack: List[int] = [] # our curly paren stack
1044 # if we're in a string part of the f-string, ignore escaped curly braces
1045 if not stack and i + 1 < len(s) and s[i + 1] == "{":
1057 # we've made it back out of the expression! yield the span
1063 # if we're in an expression part of the f-string, fast forward through strings
1064 # note that backslashes are not legal in the expression portion of f-strings
1067 if s[i : i + 3] in ("'''", '"""'):
1068 delim = s[i : i + 3]
1069 elif s[i] in ("'", '"'):
1073 while i < len(s) and s[i : i + len(delim)] != delim:
1080 def fstring_contains_expr(s: str) -> bool:
1081 return any(iter_fexpr_spans(s))
1084 class StringSplitter(BaseStringSplitter, CustomSplitMapMixin):
1086 StringTransformer that splits "atom" strings (i.e. strings which exist on
1087 lines by themselves).
1090 * The line consists ONLY of a single string (possibly prefixed by a
1091 string operator [e.g. '+' or '==']), MAYBE a string trailer, and MAYBE
1094 * All of the requirements listed in BaseStringSplitter's docstring.
1097 The string mentioned in the 'Requirements' section is split into as
1098 many substrings as necessary to adhere to the configured line length.
1100 In the final set of substrings, no substring should be smaller than
1101 MIN_SUBSTR_SIZE characters.
1103 The string will ONLY be split on spaces (i.e. each new substring should
1104 start with a space). Note that the string will NOT be split on a space
1105 which is escaped with a backslash.
1107 If the string is an f-string, it will NOT be split in the middle of an
1108 f-expression (e.g. in f"FooBar: {foo() if x else bar()}", {foo() if x
1109 else bar()} is an f-expression).
1111 If the string that is being split has an associated set of custom split
1112 records and those custom splits will NOT result in any line going over
1113 the configured line length, those custom splits are used. Otherwise the
1114 string is split as late as possible (from left-to-right) while still
1115 adhering to the transformation rules listed above.
1118 StringSplitter relies on StringMerger to construct the appropriate
1119 CustomSplit objects and add them to the custom split map.
1122 MIN_SUBSTR_SIZE: Final = 6
1124 def do_splitter_match(self, line: Line) -> TMatchResult:
1127 is_valid_index = is_valid_index_factory(LL)
1131 # The first two leaves MAY be the 'not in' keywords...
1134 and is_valid_index(idx + 1)
1135 and [LL[idx].type, LL[idx + 1].type] == [token.NAME, token.NAME]
1136 and str(LL[idx]) + str(LL[idx + 1]) == "not in"
1139 # Else the first leaf MAY be a string operator symbol or the 'in' keyword...
1140 elif is_valid_index(idx) and (
1141 LL[idx].type in self.STRING_OPERATORS
1142 or LL[idx].type == token.NAME
1143 and str(LL[idx]) == "in"
1147 # The next/first leaf MAY be an empty LPAR...
1148 if is_valid_index(idx) and is_empty_lpar(LL[idx]):
1151 # The next/first leaf MUST be a string...
1152 if not is_valid_index(idx) or LL[idx].type != token.STRING:
1153 return TErr("Line does not start with a string.")
1157 # Skip the string trailer, if one exists.
1158 string_parser = StringParser()
1159 idx = string_parser.parse(LL, string_idx)
1161 # That string MAY be followed by an empty RPAR...
1162 if is_valid_index(idx) and is_empty_rpar(LL[idx]):
1165 # That string / empty RPAR leaf MAY be followed by a comma...
1166 if is_valid_index(idx) and LL[idx].type == token.COMMA:
1169 # But no more leaves are allowed...
1170 if is_valid_index(idx):
1171 return TErr("This line does not end with a string.")
1173 return Ok(string_idx)
1175 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
1178 QUOTE = LL[string_idx].value[-1]
1180 is_valid_index = is_valid_index_factory(LL)
1181 insert_str_child = insert_str_child_factory(LL[string_idx])
1183 prefix = get_string_prefix(LL[string_idx].value).lower()
1185 # We MAY choose to drop the 'f' prefix from substrings that don't
1186 # contain any f-expressions, but ONLY if the original f-string
1187 # contains at least one f-expression. Otherwise, we will alter the AST
1189 drop_pointless_f_prefix = ("f" in prefix) and fstring_contains_expr(
1190 LL[string_idx].value
1193 first_string_line = True
1195 string_op_leaves = self._get_string_operator_leaves(LL)
1196 string_op_leaves_length = (
1197 sum([len(str(prefix_leaf)) for prefix_leaf in string_op_leaves]) + 1
1202 def maybe_append_string_operators(new_line: Line) -> None:
1205 If @line starts with a string operator and this is the first
1206 line we are constructing, this function appends the string
1207 operator to @new_line and replaces the old string operator leaf
1208 in the node structure. Otherwise this function does nothing.
1210 maybe_prefix_leaves = string_op_leaves if first_string_line else []
1211 for i, prefix_leaf in enumerate(maybe_prefix_leaves):
1212 replace_child(LL[i], prefix_leaf)
1213 new_line.append(prefix_leaf)
1216 is_valid_index(string_idx + 1) and LL[string_idx + 1].type == token.COMMA
1219 def max_last_string() -> int:
1222 The max allowed length of the string value used for the last
1223 line we will construct.
1225 result = self.line_length
1226 result -= line.depth * 4
1227 result -= 1 if ends_with_comma else 0
1228 result -= string_op_leaves_length
1231 # --- Calculate Max Break Index (for string value)
1232 # We start with the line length limit
1233 max_break_idx = self.line_length
1234 # The last index of a string of length N is N-1.
1236 # Leading whitespace is not present in the string value (e.g. Leaf.value).
1237 max_break_idx -= line.depth * 4
1238 if max_break_idx < 0:
1240 f"Unable to split {LL[string_idx].value} at such high of a line depth:"
1245 # Check if StringMerger registered any custom splits.
1246 custom_splits = self.pop_custom_splits(LL[string_idx].value)
1247 # We use them ONLY if none of them would produce lines that exceed the
1249 use_custom_breakpoints = bool(
1251 and all(csplit.break_idx <= max_break_idx for csplit in custom_splits)
1254 # Temporary storage for the remaining chunk of the string line that
1255 # can't fit onto the line currently being constructed.
1256 rest_value = LL[string_idx].value
1258 def more_splits_should_be_made() -> bool:
1261 True iff `rest_value` (the remaining string value from the last
1262 split), should be split again.
1264 if use_custom_breakpoints:
1265 return len(custom_splits) > 1
1267 return len(rest_value) > max_last_string()
1269 string_line_results: List[Ok[Line]] = []
1270 while more_splits_should_be_made():
1271 if use_custom_breakpoints:
1272 # Custom User Split (manual)
1273 csplit = custom_splits.pop(0)
1274 break_idx = csplit.break_idx
1276 # Algorithmic Split (automatic)
1277 max_bidx = max_break_idx - string_op_leaves_length
1278 maybe_break_idx = self._get_break_idx(rest_value, max_bidx)
1279 if maybe_break_idx is None:
1280 # If we are unable to algorithmically determine a good split
1281 # and this string has custom splits registered to it, we
1282 # fall back to using them--which means we have to start
1283 # over from the beginning.
1285 rest_value = LL[string_idx].value
1286 string_line_results = []
1287 first_string_line = True
1288 use_custom_breakpoints = True
1291 # Otherwise, we stop splitting here.
1294 break_idx = maybe_break_idx
1296 # --- Construct `next_value`
1297 next_value = rest_value[:break_idx] + QUOTE
1299 # HACK: The following 'if' statement is a hack to fix the custom
1300 # breakpoint index in the case of either: (a) substrings that were
1301 # f-strings but will have the 'f' prefix removed OR (b) substrings
1302 # that were not f-strings but will now become f-strings because of
1303 # redundant use of the 'f' prefix (i.e. none of the substrings
1304 # contain f-expressions but one or more of them had the 'f' prefix
1305 # anyway; in which case, we will prepend 'f' to _all_ substrings).
1307 # There is probably a better way to accomplish what is being done
1310 # If this substring is an f-string, we _could_ remove the 'f'
1311 # prefix, and the current custom split did NOT originally use a
1314 next_value != self._normalize_f_string(next_value, prefix)
1315 and use_custom_breakpoints
1316 and not csplit.has_prefix
1318 # Then `csplit.break_idx` will be off by one after removing
1321 next_value = rest_value[:break_idx] + QUOTE
1323 if drop_pointless_f_prefix:
1324 next_value = self._normalize_f_string(next_value, prefix)
1326 # --- Construct `next_leaf`
1327 next_leaf = Leaf(token.STRING, next_value)
1328 insert_str_child(next_leaf)
1329 self._maybe_normalize_string_quotes(next_leaf)
1331 # --- Construct `next_line`
1332 next_line = line.clone()
1333 maybe_append_string_operators(next_line)
1334 next_line.append(next_leaf)
1335 string_line_results.append(Ok(next_line))
1337 rest_value = prefix + QUOTE + rest_value[break_idx:]
1338 first_string_line = False
1340 yield from string_line_results
1342 if drop_pointless_f_prefix:
1343 rest_value = self._normalize_f_string(rest_value, prefix)
1345 rest_leaf = Leaf(token.STRING, rest_value)
1346 insert_str_child(rest_leaf)
1348 # NOTE: I could not find a test case that verifies that the following
1349 # line is actually necessary, but it seems to be. Otherwise we risk
1350 # not normalizing the last substring, right?
1351 self._maybe_normalize_string_quotes(rest_leaf)
1353 last_line = line.clone()
1354 maybe_append_string_operators(last_line)
1356 # If there are any leaves to the right of the target string...
1357 if is_valid_index(string_idx + 1):
1358 # We use `temp_value` here to determine how long the last line
1359 # would be if we were to append all the leaves to the right of the
1360 # target string to the last string line.
1361 temp_value = rest_value
1362 for leaf in LL[string_idx + 1 :]:
1363 temp_value += str(leaf)
1364 if leaf.type == token.LPAR:
1367 # Try to fit them all on the same line with the last substring...
1369 len(temp_value) <= max_last_string()
1370 or LL[string_idx + 1].type == token.COMMA
1372 last_line.append(rest_leaf)
1373 append_leaves(last_line, line, LL[string_idx + 1 :])
1375 # Otherwise, place the last substring on one line and everything
1376 # else on a line below that...
1378 last_line.append(rest_leaf)
1381 non_string_line = line.clone()
1382 append_leaves(non_string_line, line, LL[string_idx + 1 :])
1383 yield Ok(non_string_line)
1384 # Else the target string was the last leaf...
1386 last_line.append(rest_leaf)
1387 last_line.comments = line.comments.copy()
1390 def _iter_nameescape_slices(self, string: str) -> Iterator[Tuple[Index, Index]]:
1393 All ranges of @string which, if @string were to be split there,
1394 would result in the splitting of an \\N{...} expression (which is NOT
1397 # True - the previous backslash was unescaped
1398 # False - the previous backslash was escaped *or* there was no backslash
1399 previous_was_unescaped_backslash = False
1400 it = iter(enumerate(string))
1403 previous_was_unescaped_backslash = not previous_was_unescaped_backslash
1405 if not previous_was_unescaped_backslash or c != "N":
1406 previous_was_unescaped_backslash = False
1408 previous_was_unescaped_backslash = False
1410 begin = idx - 1 # the position of backslash before \N{...}
1416 # malformed nameescape expression?
1417 # should have been detected by AST parsing earlier...
1418 raise RuntimeError(f"{self.__class__.__name__} LOGIC ERROR!")
1421 def _iter_fexpr_slices(self, string: str) -> Iterator[Tuple[Index, Index]]:
1424 All ranges of @string which, if @string were to be split there,
1425 would result in the splitting of an f-expression (which is NOT
1428 if "f" not in get_string_prefix(string).lower():
1430 yield from iter_fexpr_spans(string)
1432 def _get_illegal_split_indices(self, string: str) -> Set[Index]:
1433 illegal_indices: Set[Index] = set()
1435 self._iter_fexpr_slices(string),
1436 self._iter_nameescape_slices(string),
1438 for it in iterators:
1439 for begin, end in it:
1440 illegal_indices.update(range(begin, end + 1))
1441 return illegal_indices
1443 def _get_break_idx(self, string: str, max_break_idx: int) -> Optional[int]:
1445 This method contains the algorithm that StringSplitter uses to
1446 determine which character to split each string at.
1449 @string: The substring that we are attempting to split.
1450 @max_break_idx: The ideal break index. We will return this value if it
1451 meets all the necessary conditions. In the likely event that it
1452 doesn't we will try to find the closest index BELOW @max_break_idx
1453 that does. If that fails, we will expand our search by also
1454 considering all valid indices ABOVE @max_break_idx.
1457 * assert_is_leaf_string(@string)
1458 * 0 <= @max_break_idx < len(@string)
1461 break_idx, if an index is able to be found that meets all of the
1462 conditions listed in the 'Transformations' section of this classes'
1467 is_valid_index = is_valid_index_factory(string)
1469 assert is_valid_index(max_break_idx)
1470 assert_is_leaf_string(string)
1472 _illegal_split_indices = self._get_illegal_split_indices(string)
1474 def breaks_unsplittable_expression(i: Index) -> bool:
1477 True iff returning @i would result in the splitting of an
1478 unsplittable expression (which is NOT allowed).
1480 return i in _illegal_split_indices
1482 def passes_all_checks(i: Index) -> bool:
1485 True iff ALL of the conditions listed in the 'Transformations'
1486 section of this classes' docstring would be be met by returning @i.
1488 is_space = string[i] == " "
1490 is_not_escaped = True
1492 while is_valid_index(j) and string[j] == "\\":
1493 is_not_escaped = not is_not_escaped
1497 len(string[i:]) >= self.MIN_SUBSTR_SIZE
1498 and len(string[:i]) >= self.MIN_SUBSTR_SIZE
1504 and not breaks_unsplittable_expression(i)
1507 # First, we check all indices BELOW @max_break_idx.
1508 break_idx = max_break_idx
1509 while is_valid_index(break_idx - 1) and not passes_all_checks(break_idx):
1512 if not passes_all_checks(break_idx):
1513 # If that fails, we check all indices ABOVE @max_break_idx.
1515 # If we are able to find a valid index here, the next line is going
1516 # to be longer than the specified line length, but it's probably
1517 # better than doing nothing at all.
1518 break_idx = max_break_idx + 1
1519 while is_valid_index(break_idx + 1) and not passes_all_checks(break_idx):
1522 if not is_valid_index(break_idx) or not passes_all_checks(break_idx):
1527 def _maybe_normalize_string_quotes(self, leaf: Leaf) -> None:
1528 if self.normalize_strings:
1529 leaf.value = normalize_string_quotes(leaf.value)
1531 def _normalize_f_string(self, string: str, prefix: str) -> str:
1534 * assert_is_leaf_string(@string)
1537 * If @string is an f-string that contains no f-expressions, we
1538 return a string identical to @string except that the 'f' prefix
1539 has been stripped and all double braces (i.e. '{{' or '}}') have
1540 been normalized (i.e. turned into '{' or '}').
1542 * Otherwise, we return @string.
1544 assert_is_leaf_string(string)
1546 if "f" in prefix and not fstring_contains_expr(string):
1547 new_prefix = prefix.replace("f", "")
1549 temp = string[len(prefix) :]
1550 temp = re.sub(r"\{\{", "{", temp)
1551 temp = re.sub(r"\}\}", "}", temp)
1554 return f"{new_prefix}{new_string}"
1558 def _get_string_operator_leaves(self, leaves: Iterable[Leaf]) -> List[Leaf]:
1561 string_op_leaves = []
1563 while LL[i].type in self.STRING_OPERATORS + [token.NAME]:
1564 prefix_leaf = Leaf(LL[i].type, str(LL[i]).strip())
1565 string_op_leaves.append(prefix_leaf)
1567 return string_op_leaves
1570 class StringParenWrapper(BaseStringSplitter, CustomSplitMapMixin):
1572 StringTransformer that splits non-"atom" strings (i.e. strings that do not
1573 exist on lines by themselves).
1576 All of the requirements listed in BaseStringSplitter's docstring in
1577 addition to the requirements listed below:
1579 * The line is a return/yield statement, which returns/yields a string.
1581 * The line is part of a ternary expression (e.g. `x = y if cond else
1582 z`) such that the line starts with `else <string>`, where <string> is
1585 * The line is an assert statement, which ends with a string.
1587 * The line is an assignment statement (e.g. `x = <string>` or `x +=
1588 <string>`) such that the variable is being assigned the value of some
1591 * The line is a dictionary key assignment where some valid key is being
1592 assigned the value of some string.
1595 The chosen string is wrapped in parentheses and then split at the LPAR.
1597 We then have one line which ends with an LPAR and another line that
1598 starts with the chosen string. The latter line is then split again at
1599 the RPAR. This results in the RPAR (and possibly a trailing comma)
1600 being placed on its own line.
1602 NOTE: If any leaves exist to the right of the chosen string (except
1603 for a trailing comma, which would be placed after the RPAR), those
1604 leaves are placed inside the parentheses. In effect, the chosen
1605 string is not necessarily being "wrapped" by parentheses. We can,
1606 however, count on the LPAR being placed directly before the chosen
1609 In other words, StringParenWrapper creates "atom" strings. These
1610 can then be split again by StringSplitter, if necessary.
1613 In the event that a string line split by StringParenWrapper is
1614 changed such that it no longer needs to be given its own line,
1615 StringParenWrapper relies on StringParenStripper to clean up the
1616 parentheses it created.
1619 def do_splitter_match(self, line: Line) -> TMatchResult:
1622 if line.leaves[-1].type in OPENING_BRACKETS:
1624 "Cannot wrap parens around a line that ends in an opening bracket."
1628 self._return_match(LL)
1629 or self._else_match(LL)
1630 or self._assert_match(LL)
1631 or self._assign_match(LL)
1632 or self._dict_match(LL)
1635 if string_idx is not None:
1636 string_value = line.leaves[string_idx].value
1637 # If the string has no spaces...
1638 if " " not in string_value:
1639 # And will still violate the line length limit when split...
1640 max_string_length = self.line_length - ((line.depth + 1) * 4)
1641 if len(string_value) > max_string_length:
1642 # And has no associated custom splits...
1643 if not self.has_custom_splits(string_value):
1644 # Then we should NOT put this string on its own line.
1646 "We do not wrap long strings in parentheses when the"
1647 " resultant line would still be over the specified line"
1648 " length and can't be split further by StringSplitter."
1650 return Ok(string_idx)
1652 return TErr("This line does not contain any non-atomic strings.")
1655 def _return_match(LL: List[Leaf]) -> Optional[int]:
1658 string_idx such that @LL[string_idx] is equal to our target (i.e.
1659 matched) string, if this line matches the return/yield statement
1660 requirements listed in the 'Requirements' section of this classes'
1665 # If this line is apart of a return/yield statement and the first leaf
1666 # contains either the "return" or "yield" keywords...
1667 if parent_type(LL[0]) in [syms.return_stmt, syms.yield_expr] and LL[
1669 ].value in ["return", "yield"]:
1670 is_valid_index = is_valid_index_factory(LL)
1672 idx = 2 if is_valid_index(1) and is_empty_par(LL[1]) else 1
1673 # The next visible leaf MUST contain a string...
1674 if is_valid_index(idx) and LL[idx].type == token.STRING:
1680 def _else_match(LL: List[Leaf]) -> Optional[int]:
1683 string_idx such that @LL[string_idx] is equal to our target (i.e.
1684 matched) string, if this line matches the ternary expression
1685 requirements listed in the 'Requirements' section of this classes'
1690 # If this line is apart of a ternary expression and the first leaf
1691 # contains the "else" keyword...
1693 parent_type(LL[0]) == syms.test
1694 and LL[0].type == token.NAME
1695 and LL[0].value == "else"
1697 is_valid_index = is_valid_index_factory(LL)
1699 idx = 2 if is_valid_index(1) and is_empty_par(LL[1]) else 1
1700 # The next visible leaf MUST contain a string...
1701 if is_valid_index(idx) and LL[idx].type == token.STRING:
1707 def _assert_match(LL: List[Leaf]) -> Optional[int]:
1710 string_idx such that @LL[string_idx] is equal to our target (i.e.
1711 matched) string, if this line matches the assert statement
1712 requirements listed in the 'Requirements' section of this classes'
1717 # If this line is apart of an assert statement and the first leaf
1718 # contains the "assert" keyword...
1719 if parent_type(LL[0]) == syms.assert_stmt and LL[0].value == "assert":
1720 is_valid_index = is_valid_index_factory(LL)
1722 for i, leaf in enumerate(LL):
1723 # We MUST find a comma...
1724 if leaf.type == token.COMMA:
1725 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
1727 # That comma MUST be followed by a string...
1728 if is_valid_index(idx) and LL[idx].type == token.STRING:
1731 # Skip the string trailer, if one exists.
1732 string_parser = StringParser()
1733 idx = string_parser.parse(LL, string_idx)
1735 # But no more leaves are allowed...
1736 if not is_valid_index(idx):
1742 def _assign_match(LL: List[Leaf]) -> Optional[int]:
1745 string_idx such that @LL[string_idx] is equal to our target (i.e.
1746 matched) string, if this line matches the assignment statement
1747 requirements listed in the 'Requirements' section of this classes'
1752 # If this line is apart of an expression statement or is a function
1753 # argument AND the first leaf contains a variable name...
1755 parent_type(LL[0]) in [syms.expr_stmt, syms.argument, syms.power]
1756 and LL[0].type == token.NAME
1758 is_valid_index = is_valid_index_factory(LL)
1760 for i, leaf in enumerate(LL):
1761 # We MUST find either an '=' or '+=' symbol...
1762 if leaf.type in [token.EQUAL, token.PLUSEQUAL]:
1763 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
1765 # That symbol MUST be followed by a string...
1766 if is_valid_index(idx) and LL[idx].type == token.STRING:
1769 # Skip the string trailer, if one exists.
1770 string_parser = StringParser()
1771 idx = string_parser.parse(LL, string_idx)
1773 # The next leaf MAY be a comma iff this line is apart
1774 # of a function argument...
1776 parent_type(LL[0]) == syms.argument
1777 and is_valid_index(idx)
1778 and LL[idx].type == token.COMMA
1782 # But no more leaves are allowed...
1783 if not is_valid_index(idx):
1789 def _dict_match(LL: List[Leaf]) -> Optional[int]:
1792 string_idx such that @LL[string_idx] is equal to our target (i.e.
1793 matched) string, if this line matches the dictionary key assignment
1794 statement requirements listed in the 'Requirements' section of this
1799 # If this line is apart of a dictionary key assignment...
1800 if syms.dictsetmaker in [parent_type(LL[0]), parent_type(LL[0].parent)]:
1801 is_valid_index = is_valid_index_factory(LL)
1803 for i, leaf in enumerate(LL):
1804 # We MUST find a colon...
1805 if leaf.type == token.COLON:
1806 idx = i + 2 if is_empty_par(LL[i + 1]) else i + 1
1808 # That colon MUST be followed by a string...
1809 if is_valid_index(idx) and LL[idx].type == token.STRING:
1812 # Skip the string trailer, if one exists.
1813 string_parser = StringParser()
1814 idx = string_parser.parse(LL, string_idx)
1816 # That string MAY be followed by a comma...
1817 if is_valid_index(idx) and LL[idx].type == token.COMMA:
1820 # But no more leaves are allowed...
1821 if not is_valid_index(idx):
1826 def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
1829 is_valid_index = is_valid_index_factory(LL)
1830 insert_str_child = insert_str_child_factory(LL[string_idx])
1833 ends_with_comma = False
1834 if LL[comma_idx].type == token.COMMA:
1835 ends_with_comma = True
1837 leaves_to_steal_comments_from = [LL[string_idx]]
1839 leaves_to_steal_comments_from.append(LL[comma_idx])
1842 first_line = line.clone()
1843 left_leaves = LL[:string_idx]
1845 # We have to remember to account for (possibly invisible) LPAR and RPAR
1846 # leaves that already wrapped the target string. If these leaves do
1847 # exist, we will replace them with our own LPAR and RPAR leaves.
1848 old_parens_exist = False
1849 if left_leaves and left_leaves[-1].type == token.LPAR:
1850 old_parens_exist = True
1851 leaves_to_steal_comments_from.append(left_leaves[-1])
1854 append_leaves(first_line, line, left_leaves)
1856 lpar_leaf = Leaf(token.LPAR, "(")
1857 if old_parens_exist:
1858 replace_child(LL[string_idx - 1], lpar_leaf)
1860 insert_str_child(lpar_leaf)
1861 first_line.append(lpar_leaf)
1863 # We throw inline comments that were originally to the right of the
1864 # target string to the top line. They will now be shown to the right of
1866 for leaf in leaves_to_steal_comments_from:
1867 for comment_leaf in line.comments_after(leaf):
1868 first_line.append(comment_leaf, preformatted=True)
1870 yield Ok(first_line)
1872 # --- Middle (String) Line
1873 # We only need to yield one (possibly too long) string line, since the
1874 # `StringSplitter` will break it down further if necessary.
1875 string_value = LL[string_idx].value
1878 depth=line.depth + 1,
1879 inside_brackets=True,
1880 should_split_rhs=line.should_split_rhs,
1881 magic_trailing_comma=line.magic_trailing_comma,
1883 string_leaf = Leaf(token.STRING, string_value)
1884 insert_str_child(string_leaf)
1885 string_line.append(string_leaf)
1887 old_rpar_leaf = None
1888 if is_valid_index(string_idx + 1):
1889 right_leaves = LL[string_idx + 1 :]
1893 if old_parens_exist:
1894 assert right_leaves and right_leaves[-1].type == token.RPAR, (
1895 "Apparently, old parentheses do NOT exist?!"
1896 f" (left_leaves={left_leaves}, right_leaves={right_leaves})"
1898 old_rpar_leaf = right_leaves.pop()
1900 append_leaves(string_line, line, right_leaves)
1902 yield Ok(string_line)
1905 last_line = line.clone()
1906 last_line.bracket_tracker = first_line.bracket_tracker
1908 new_rpar_leaf = Leaf(token.RPAR, ")")
1909 if old_rpar_leaf is not None:
1910 replace_child(old_rpar_leaf, new_rpar_leaf)
1912 insert_str_child(new_rpar_leaf)
1913 last_line.append(new_rpar_leaf)
1915 # If the target string ended with a comma, we place this comma to the
1916 # right of the RPAR on the last line.
1918 comma_leaf = Leaf(token.COMMA, ",")
1919 replace_child(LL[comma_idx], comma_leaf)
1920 last_line.append(comma_leaf)
1927 A state machine that aids in parsing a string's "trailer", which can be
1928 either non-existent, an old-style formatting sequence (e.g. `% varX` or `%
1929 (varX, varY)`), or a method-call / attribute access (e.g. `.format(varX,
1932 NOTE: A new StringParser object MUST be instantiated for each string
1933 trailer we need to parse.
1936 We shall assume that `line` equals the `Line` object that corresponds
1937 to the following line of python code:
1939 x = "Some {}.".format("String") + some_other_string
1942 Furthermore, we will assume that `string_idx` is some index such that:
1944 assert line.leaves[string_idx].value == "Some {}."
1947 The following code snippet then holds:
1949 string_parser = StringParser()
1950 idx = string_parser.parse(line.leaves, string_idx)
1951 assert line.leaves[idx].type == token.PLUS
1955 DEFAULT_TOKEN: Final = 20210605
1957 # String Parser States
1962 SINGLE_FMT_ARG: Final = 5
1967 # Lookup Table for Next State
1968 _goto: Final[Dict[Tuple[ParserState, NodeType], ParserState]] = {
1969 # A string trailer may start with '.' OR '%'.
1970 (START, token.DOT): DOT,
1971 (START, token.PERCENT): PERCENT,
1972 (START, DEFAULT_TOKEN): DONE,
1973 # A '.' MUST be followed by an attribute or method name.
1974 (DOT, token.NAME): NAME,
1975 # A method name MUST be followed by an '(', whereas an attribute name
1976 # is the last symbol in the string trailer.
1977 (NAME, token.LPAR): LPAR,
1978 (NAME, DEFAULT_TOKEN): DONE,
1979 # A '%' symbol can be followed by an '(' or a single argument (e.g. a
1980 # string or variable name).
1981 (PERCENT, token.LPAR): LPAR,
1982 (PERCENT, DEFAULT_TOKEN): SINGLE_FMT_ARG,
1983 # If a '%' symbol is followed by a single argument, that argument is
1984 # the last leaf in the string trailer.
1985 (SINGLE_FMT_ARG, DEFAULT_TOKEN): DONE,
1986 # If present, a ')' symbol is the last symbol in a string trailer.
1987 # (NOTE: LPARS and nested RPARS are not included in this lookup table,
1988 # since they are treated as a special case by the parsing logic in this
1989 # classes' implementation.)
1990 (RPAR, DEFAULT_TOKEN): DONE,
1993 def __init__(self) -> None:
1994 self._state = self.START
1995 self._unmatched_lpars = 0
1997 def parse(self, leaves: List[Leaf], string_idx: int) -> int:
2000 * @leaves[@string_idx].type == token.STRING
2003 The index directly after the last leaf which is apart of the string
2004 trailer, if a "trailer" exists.
2006 @string_idx + 1, if no string "trailer" exists.
2008 assert leaves[string_idx].type == token.STRING
2010 idx = string_idx + 1
2011 while idx < len(leaves) and self._next_state(leaves[idx]):
2015 def _next_state(self, leaf: Leaf) -> bool:
2018 * On the first call to this function, @leaf MUST be the leaf that
2019 was directly after the string leaf in question (e.g. if our target
2020 string is `line.leaves[i]` then the first call to this method must
2021 be `line.leaves[i + 1]`).
2022 * On the next call to this function, the leaf parameter passed in
2023 MUST be the leaf directly following @leaf.
2026 True iff @leaf is apart of the string's trailer.
2028 # We ignore empty LPAR or RPAR leaves.
2029 if is_empty_par(leaf):
2032 next_token = leaf.type
2033 if next_token == token.LPAR:
2034 self._unmatched_lpars += 1
2036 current_state = self._state
2038 # The LPAR parser state is a special case. We will return True until we
2039 # find the matching RPAR token.
2040 if current_state == self.LPAR:
2041 if next_token == token.RPAR:
2042 self._unmatched_lpars -= 1
2043 if self._unmatched_lpars == 0:
2044 self._state = self.RPAR
2045 # Otherwise, we use a lookup table to determine the next state.
2047 # If the lookup table matches the current state to the next
2048 # token, we use the lookup table.
2049 if (current_state, next_token) in self._goto:
2050 self._state = self._goto[current_state, next_token]
2052 # Otherwise, we check if a the current state was assigned a
2054 if (current_state, self.DEFAULT_TOKEN) in self._goto:
2055 self._state = self._goto[current_state, self.DEFAULT_TOKEN]
2056 # If no default has been assigned, then this parser has a logic
2059 raise RuntimeError(f"{self.__class__.__name__} LOGIC ERROR!")
2061 if self._state == self.DONE:
2067 def insert_str_child_factory(string_leaf: Leaf) -> Callable[[LN], None]:
2069 Factory for a convenience function that is used to orphan @string_leaf
2070 and then insert multiple new leaves into the same part of the node
2071 structure that @string_leaf had originally occupied.
2074 Let `string_leaf = Leaf(token.STRING, '"foo"')` and `N =
2075 string_leaf.parent`. Assume the node `N` has the following
2082 Leaf(STRING, '"foo"'),
2086 We then run the code snippet shown below.
2088 insert_str_child = insert_str_child_factory(string_leaf)
2090 lpar = Leaf(token.LPAR, '(')
2091 insert_str_child(lpar)
2093 bar = Leaf(token.STRING, '"bar"')
2094 insert_str_child(bar)
2096 rpar = Leaf(token.RPAR, ')')
2097 insert_str_child(rpar)
2100 After which point, it follows that `string_leaf.parent is None` and
2101 the node `N` now has the following structure:
2108 Leaf(STRING, '"bar"'),
2113 string_parent = string_leaf.parent
2114 string_child_idx = string_leaf.remove()
2116 def insert_str_child(child: LN) -> None:
2117 nonlocal string_child_idx
2119 assert string_parent is not None
2120 assert string_child_idx is not None
2122 string_parent.insert_child(string_child_idx, child)
2123 string_child_idx += 1
2125 return insert_str_child
2128 def is_valid_index_factory(seq: Sequence[Any]) -> Callable[[int], bool]:
2134 is_valid_index = is_valid_index_factory(my_list)
2136 assert is_valid_index(0)
2137 assert is_valid_index(2)
2139 assert not is_valid_index(3)
2140 assert not is_valid_index(-1)
2144 def is_valid_index(idx: int) -> bool:
2147 True iff @idx is positive AND seq[@idx] does NOT raise an
2150 return 0 <= idx < len(seq)
2152 return is_valid_index