MCPcopy Create free account
hub / github.com/psf/black / assert_is_leaf_string

Function assert_is_leaf_string

src/black/strings.py:118–150  ·  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

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