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

Function assert_is_leaf_string

src/black/strings.py:108–140  ·  view source on GitHub ↗

Checks the pre-condition that @string has the format that you would expect of `leaf.value` where `leaf` is some Leaf such that `leaf.type == token.STRING`. A more precise description of the pre-conditions that are checked are listed below. Pre-conditions: * @string star

(string: str)

Source from the content-addressed store, hash-verified

106
107
108def assert_is_leaf_string(string: str) -> None:
109 """
110 Checks the pre-condition that @string has the format that you would expect
111 of `leaf.value` where `leaf` is some Leaf such that `leaf.type ==
112 token.STRING`. A more precise description of the pre-conditions that are
113 checked are listed below.
114
115 Pre-conditions:
116 * @string starts with either ', ", <prefix>', or <prefix>" where
117 `set(<prefix>)` is some subset of `set(STRING_PREFIX_CHARS)`.
118 * @string ends with a quote character (&#x27; or ").
119
120 Raises:
121 AssertionError(...) if the pre-conditions listed above are not
122 satisfied.
123 """
124 dquote_idx = string.find('"')
125 squote_idx = string.find("'")
126 if -1 in [dquote_idx, squote_idx]:
127 quote_idx = max(dquote_idx, squote_idx)
128 else:
129 quote_idx = min(squote_idx, dquote_idx)
130
131 assert (
132 0 <= quote_idx < len(string) - 1
133 ), f"{string!r} is missing a starting quote character (' or \")."
134 assert string[-1] in (
135 "'",
136 '"',
137 ), f"{string!r} is missing an ending quote character (' or \")."
138 assert set(string[:quote_idx]).issubset(
139 set(STRING_PREFIX_CHARS)
140 ), f"{set(string[:quote_idx])} is NOT a subset of {set(STRING_PREFIX_CHARS)}."
141
142
143def normalize_string_prefix(s: str) -> str:

Callers 4

make_nakedMethod · 0.90
_get_break_idxMethod · 0.90
_normalize_f_stringMethod · 0.90
get_string_prefixFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected