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

Method do_splitter_match

src/black/trans.py:1446–1512  ·  view source on GitHub ↗
(self, line: Line)

Source from the content-addressed store, hash-verified

1444 MIN_SUBSTR_SIZE: Final = 6
1445
1446 def do_splitter_match(self, line: Line) -> TMatchResult:
1447 LL = line.leaves
1448
1449 if self._prefer_paren_wrap_match(LL) is not None:
1450 return TErr("Line needs to be wrapped in parens first.")
1451
1452 # If the line is just STRING + COMMA (a one-item tuple) and not inside
1453 # brackets, we need to defer to StringParenWrapper to wrap it first.
1454 # Otherwise, splitting the string would create multiple expressions where
1455 # only the last has the comma, breaking AST equivalence. See issue #4912.
1456 if (
1457 not line.inside_brackets
1458 and len(LL) == 2
1459 and LL[0].type == token.STRING
1460 and LL[1].type == token.COMMA
1461 ):
1462 return TErr(
1463 "Line with trailing comma tuple needs to be wrapped in parens first."
1464 )
1465
1466 is_valid_index = is_valid_index_factory(LL)
1467
1468 idx = 0
1469
1470 # The first two leaves MAY be the 'not in' keywords...
1471 if (
1472 is_valid_index(idx)
1473 and is_valid_index(idx + 1)
1474 and [LL[idx].type, LL[idx + 1].type] == [token.NAME, token.NAME]
1475 and str(LL[idx]) + str(LL[idx + 1]) == "not in"
1476 ):
1477 idx += 2
1478 # Else the first leaf MAY be a string operator symbol or the 'in' keyword...
1479 elif is_valid_index(idx) and (
1480 LL[idx].type in self.STRING_OPERATORS
1481 or LL[idx].type == token.NAME
1482 and str(LL[idx]) == "in"
1483 ):
1484 idx += 1
1485
1486 # The next/first leaf MAY be an empty LPAR...
1487 if is_valid_index(idx) and is_empty_lpar(LL[idx]):
1488 idx += 1
1489
1490 # The next/first leaf MUST be a string...
1491 if not is_valid_index(idx) or LL[idx].type != token.STRING:
1492 return TErr("Line does not start with a string.")
1493
1494 string_idx = idx
1495
1496 # Skip the string trailer, if one exists.
1497 string_parser = StringParser()
1498 idx = string_parser.parse(LL, string_idx)
1499
1500 # That string MAY be followed by an empty RPAR...
1501 if is_valid_index(idx) and is_empty_rpar(LL[idx]):
1502 idx += 1
1503

Callers

nothing calls this directly

Calls 9

parseMethod · 0.95
is_empty_lparFunction · 0.90
is_empty_rparFunction · 0.90
OkClass · 0.90
TErrFunction · 0.85
is_valid_index_factoryFunction · 0.85
is_valid_indexFunction · 0.85
StringParserClass · 0.85

Tested by

no test coverage detected