MCPcopy Index your code
hub / github.com/python/cpython / _ftstring_helper

Method _ftstring_helper

Lib/_ast_unparse.py:576–616  ·  view source on GitHub ↗
(self, parts)

Source from the content-addressed store, hash-verified

574 self.write(f"{quote_type}{string}{quote_type}")
575
576 def _ftstring_helper(self, parts):
577 new_parts = []
578 quote_types = list(_ALL_QUOTES)
579 fallback_to_repr = False
580 for value, is_constant in parts:
581 if is_constant:
582 value, new_quote_types = self._str_literal_helper(
583 value,
584 quote_types=quote_types,
585 escape_special_whitespace=True,
586 )
587 if set(new_quote_types).isdisjoint(quote_types):
588 fallback_to_repr = True
589 break
590 quote_types = new_quote_types
591 else:
592 if "\n" in value:
593 quote_types = [q for q in quote_types if q in _MULTI_QUOTES]
594 assert quote_types
595
596 new_quote_types = [q for q in quote_types if q not in value]
597 if new_quote_types:
598 quote_types = new_quote_types
599 new_parts.append(value)
600
601 if fallback_to_repr:
602 # If we weren't able to find a quote type that works for all parts
603 # of the JoinedStr, fallback to using repr and triple single quotes.
604 quote_types = ["'''"]
605 new_parts.clear()
606 for value, is_constant in parts:
607 if is_constant:
608 value = repr('"' + value) # force repr to use single quotes
609 expected_prefix = "'\""
610 assert value.startswith(expected_prefix), repr(value)
611 value = value[len(expected_prefix):-1]
612 new_parts.append(value)
613
614 value = "".join(new_parts)
615 quote_type = quote_types[0]
616 self.write(f"{quote_type}{value}{quote_type}")
617
618 def _write_ftstring(self, values, prefix):
619 self.write(prefix)

Callers 1

_write_ftstringMethod · 0.95

Calls 9

_str_literal_helperMethod · 0.95
writeMethod · 0.95
listClass · 0.85
setFunction · 0.85
isdisjointMethod · 0.45
appendMethod · 0.45
clearMethod · 0.45
startswithMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected