3030
3131from mypy_extensions import trait
3232
33- from black .brackets import BracketMatchError
3433from black .comments import contains_pragma_comment
3534from black .lines import Line , append_leaves
3635from black .mode import Feature
4140 is_empty_lpar ,
4241 is_empty_par ,
4342 is_empty_rpar ,
43+ is_part_of_annotation ,
4444 parent_type ,
4545 replace_child ,
4646 syms ,
@@ -351,7 +351,7 @@ class StringMerger(StringTransformer, CustomSplitMapMixin):
351351
352352 Requirements:
353353 (A) The line contains adjacent strings such that ALL of the validation checks
354- listed in StringMerger.__validate_msg (...)'s docstring pass.
354+ listed in StringMerger._validate_msg (...)'s docstring pass.
355355 OR
356356 (B) The line contains a string which uses line continuation backslashes.
357357
@@ -377,6 +377,8 @@ def do_match(self, line: Line) -> TMatchResult:
377377 and is_valid_index (i + 1 )
378378 and LL [i + 1 ].type == token .STRING
379379 ):
380+ if is_part_of_annotation (leaf ):
381+ return TErr ("String is part of type annotation." )
380382 return Ok (i )
381383
382384 if leaf .type == token .STRING and "\\ \n " in leaf .value :
@@ -454,7 +456,7 @@ def _merge_string_group(self, line: Line, string_idx: int) -> TResult[Line]:
454456
455457 Returns:
456458 Ok(new_line), if ALL of the validation checks found in
457- __validate_msg (...) pass.
459+ _validate_msg (...) pass.
458460 OR
459461 Err(CannotTransform), otherwise.
460462 """
@@ -608,7 +610,7 @@ def make_naked(string: str, string_prefix: str) -> str:
608610 def _validate_msg (line : Line , string_idx : int ) -> TResult [None ]:
609611 """Validate (M)erge (S)tring (G)roup
610612
611- Transform-time string validation logic for __merge_string_group (...).
613+ Transform-time string validation logic for _merge_string_group (...).
612614
613615 Returns:
614616 * Ok(None), if ALL validation checks (listed below) pass.
@@ -622,6 +624,11 @@ def _validate_msg(line: Line, string_idx: int) -> TResult[None]:
622624 - The set of all string prefixes in the string group is of
623625 length greater than one and is not equal to {"", "f"}.
624626 - The string group consists of raw strings.
627+ - The string group is stringified type annotations. We don't want to
628+ process stringified type annotations since pyright doesn't support
629+ them spanning multiple string values. (NOTE: mypy, pytype, pyre do
630+ support them, so we can change if pyright also gains support in the
631+ future. See https://github.com/microsoft/pyright/issues/4359.)
625632 """
626633 # We first check for "inner" stand-alone comments (i.e. stand-alone
627634 # comments that have a string leaf before them AND after them).
@@ -812,13 +819,7 @@ def do_transform(self, line: Line, string_idx: int) -> Iterator[TResult[Line]]:
812819
813820 new_line = line .clone ()
814821 new_line .comments = line .comments .copy ()
815- try :
816- append_leaves (new_line , line , LL [: string_idx - 1 ])
817- except BracketMatchError :
818- # HACK: I believe there is currently a bug somewhere in
819- # right_hand_split() that is causing brackets to not be tracked
820- # properly by a shared BracketTracker.
821- append_leaves (new_line , line , LL [: string_idx - 1 ], preformatted = True )
822+ append_leaves (new_line , line , LL [: string_idx - 1 ])
822823
823824 string_leaf = Leaf (token .STRING , LL [string_idx ].value )
824825 LL [string_idx - 1 ].remove ()
0 commit comments