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

Function whitespace

src/black/nodes.py:180–428  ·  src/black/nodes.py::whitespace

Return whitespace prefix if needed for the given `leaf`. `complex_subscript` signals whether the given leaf is part of a subscription which has non-trivial arguments, like arithmetic expressions or function calls.

(leaf: Leaf, *, complex_subscript: bool, mode: Mode)

Source from the content-addressed store, hash-verified

178
179
180def whitespace(leaf: Leaf, *, complex_subscript: bool, mode: Mode) -> str:
181 class="st">"""Return whitespace prefix if needed for the given `leaf`.
182
183 `complex_subscript` signals whether the given leaf is part of a subscription
184 which has non-trivial arguments, like arithmetic expressions or function calls.
185 class="st">"""
186 NO: Final[str] = class="st">""
187 SPACE: Final[str] = class="st">" "
188 DOUBLESPACE: Final[str] = class="st">" "
189 t = leaf.type
190 p = leaf.parent
191 v = leaf.value
192 if t in ALWAYS_NO_SPACE:
193 return NO
194
195 if t == token.COMMENT:
196 return DOUBLESPACE
197
198 assert p is not None, fclass="st">"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"
199 if t == token.COLON and p.type not in {
200 syms.subscript,
201 syms.subscriptlist,
202 syms.sliceop,
203 }:
204 return NO
205
206 if t == token.LBRACE and p.type in (
207 syms.fstring_replacement_field,
208 syms.tstring_replacement_field,
209 ):
210 return NO
211
212 prev = leaf.prev_sibling
213 if not prev:
214 prevp = preceding_leaf(p)
215 if not prevp or prevp.type in OPENING_BRACKETS:
216 return NO
217
218 if t == token.COLON:
219 if prevp.type == token.COLON:
220 return NO
221
222 elif prevp.type != token.COMMA and not complex_subscript:
223 return NO
224
225 return SPACE
226
227 if prevp.type == token.EQUAL:
228 if prevp.parent:
229 if prevp.parent.type in {
230 syms.arglist,
231 syms.argument,
232 syms.parameters,
233 syms.varargslist,
234 }:
235 return NO
236
237 elif prevp.parent.type == syms.typedargslist:

Callers 1

appendMethod · 0.90

Calls 4

preceding_leafFunction · 0.85
parent_typeFunction · 0.85
is_varargFunction · 0.85
is_simple_exponentiationFunction · 0.85

Tested by

no test coverage detected