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

Function _format_str_once

src/black/__init__.py:1258–1318  ·  view source on GitHub ↗
(
    src_contents: str, *, mode: Mode, lines: Collection[tuple[int, int]] = ()
)

Source from the content-addressed store, hash-verified

1256
1257
1258def _format_str_once(
1259 src_contents: str, *, mode: Mode, lines: Collection[tuple[int, int]] = ()
1260) -> str:
1261 # Use the encoding overwrite since the src_contents may contain a different
1262 # magic encoding comment than utf-8
1263 normalized_contents, _, newline_type = decode_bytes(
1264 src_contents.encode("utf-8"), mode, encoding_overwrite="utf-8"
1265 )
1266
1267 src_node = lib2to3_parse(
1268 normalized_contents.lstrip(), target_versions=mode.target_versions
1269 )
1270
1271 dst_blocks: list[LinesBlock] = []
1272 if mode.target_versions:
1273 versions = mode.target_versions
1274 else:
1275 future_imports = get_future_imports(src_node)
1276 versions = detect_target_versions(src_node, future_imports=future_imports)
1277
1278 line_generation_features = {
1279 feature
1280 for feature in {
1281 Feature.PARENTHESIZED_CONTEXT_MANAGERS,
1282 Feature.UNPARENTHESIZED_EXCEPT_TYPES,
1283 Feature.T_STRINGS,
1284 }
1285 if supports_feature(versions, feature)
1286 }
1287 normalize_fmt_off(src_node, mode, lines)
1288 if lines:
1289 # This should be called after normalize_fmt_off.
1290 convert_unchanged_lines(src_node, lines)
1291
1292 line_generator = LineGenerator(mode=mode, features=line_generation_features)
1293 elt = EmptyLineTracker(mode=mode)
1294 split_line_features = {
1295 feature
1296 for feature in {
1297 Feature.TRAILING_COMMA_IN_CALL,
1298 Feature.TRAILING_COMMA_IN_DEF,
1299 }
1300 if supports_feature(versions, feature)
1301 }
1302 block: LinesBlock | None = None
1303 for current_line in line_generator.visit(src_node):
1304 block = elt.maybe_empty_lines(current_line)
1305 dst_blocks.append(block)
1306 for line in transform_line(
1307 current_line, mode=mode, features=split_line_features
1308 ):
1309 block.content_lines.append(str(line))
1310 if dst_blocks:
1311 dst_blocks[-1].after = 0
1312 dst_contents = []
1313 for block in dst_blocks:
1314 dst_contents.extend(block.all_lines())
1315 if not dst_contents:

Callers 2

format_strFunction · 0.85
assert_stableFunction · 0.85

Calls 15

maybe_empty_linesMethod · 0.95
lib2to3_parseFunction · 0.90
supports_featureFunction · 0.90
normalize_fmt_offFunction · 0.90
convert_unchanged_linesFunction · 0.90
LineGeneratorClass · 0.90
EmptyLineTrackerClass · 0.90
transform_lineFunction · 0.90
decode_bytesFunction · 0.85
get_future_importsFunction · 0.85
detect_target_versionsFunction · 0.85
visitMethod · 0.80

Tested by

no test coverage detected