MCPcopy
hub / github.com/psf/black / _validate_msg

Method _validate_msg

src/black/trans.py:756–874  ·  view source on GitHub ↗

Validate (M)erge (S)tring (G)roup Transform-time string validation logic for _merge_string_group(...). Returns: * Ok(None), if ALL validation checks (listed below) pass. OR * Err(CannotTransform), if any of the following are true:

(line: Line, string_idx: int)

Source from the content-addressed store, hash-verified

754
755 @staticmethod
756 def _validate_msg(line: Line, string_idx: int) -> TResult[None]:
757 """Validate (M)erge (S)tring (G)roup
758
759 Transform-time string validation logic for _merge_string_group(...).
760
761 Returns:
762 * Ok(None), if ALL validation checks (listed below) pass.
763 OR
764 * Err(CannotTransform), if any of the following are true:
765 - The target string group does not contain ANY stand-alone comments.
766 - The target string is not in a string group (i.e. it has no
767 adjacent strings).
768 - The string group has more than one inline comment.
769 - The string group has an inline comment that appears to be a pragma.
770 - The set of all string prefixes in the string group is of
771 length greater than one and is not equal to {"", "f"}.
772 - The string group consists of raw strings.
773 - The string group would merge f-strings with different quote types
774 and internal quotes.
775 - The string group is stringified type annotations. We don't want to
776 process stringified type annotations since pyright doesn't support
777 them spanning multiple string values. (NOTE: mypy, pytype, pyre do
778 support them, so we can change if pyright also gains support in the
779 future. See https://github.com/microsoft/pyright/issues/4359.)
780 """
781 # We first check for "inner" stand-alone comments (i.e. stand-alone
782 # comments that have a string leaf before them AND after them).
783 for inc in [1, -1]:
784 i = string_idx
785 found_sa_comment = False
786 is_valid_index = is_valid_index_factory(line.leaves)
787 while is_valid_index(i) and line.leaves[i].type in [
788 token.STRING,
789 STANDALONE_COMMENT,
790 ]:
791 if line.leaves[i].type == STANDALONE_COMMENT:
792 found_sa_comment = True
793 elif found_sa_comment:
794 return TErr(
795 "StringMerger does NOT merge string groups which contain "
796 "stand-alone comments."
797 )
798
799 i += inc
800
801 QUOTE = line.leaves[string_idx].value[-1]
802
803 num_of_inline_string_comments = 0
804 set_of_prefixes = set()
805 num_of_strings = 0
806 for leaf in line.leaves[string_idx:]:
807 if leaf.type != token.STRING:
808 # If the string group is trailed by a comma, we count the
809 # comments trailing the comma to be one of the string group's
810 # comments.
811 if leaf.type == token.COMMA and id(leaf) in line.comments:
812 num_of_inline_string_comments += 1
813 break

Callers 1

_merge_string_groupMethod · 0.95

Calls 7

has_triple_quotesFunction · 0.90
get_string_prefixFunction · 0.90
contains_pragma_commentFunction · 0.90
OkClass · 0.90
is_valid_index_factoryFunction · 0.85
is_valid_indexFunction · 0.85
TErrFunction · 0.85

Tested by

no test coverage detected