Toggles quotes used in f-string expressions that are `old_quote`. f-string expressions can't contain backslashes, so we need to toggle the quotes if the f-string itself will end up using the same quote. We can simply toggle without escaping because, quotes can't be reused in f-stri
(fstring: str, old_quote: str)
| 1380 | |
| 1381 | |
| 1382 | def _toggle_fexpr_quotes(fstring: str, old_quote: str) -> str: |
| 1383 | """ |
| 1384 | Toggles quotes used in f-string expressions that are `old_quote`. |
| 1385 | |
| 1386 | f-string expressions can't contain backslashes, so we need to toggle the |
| 1387 | quotes if the f-string itself will end up using the same quote. We can |
| 1388 | simply toggle without escaping because, quotes can't be reused in f-string |
| 1389 | expressions. They will fail to parse. |
| 1390 | |
| 1391 | NOTE: If PEP 701 is accepted, above statement will no longer be true. |
| 1392 | Though if quotes can be reused, we can simply reuse them without updates or |
| 1393 | escaping, once Black figures out how to parse the new grammar. |
| 1394 | """ |
| 1395 | new_quote = "'" if old_quote == '"' else '"' |
| 1396 | parts = [] |
| 1397 | previous_index = 0 |
| 1398 | for start, end in iter_fexpr_spans(fstring): |
| 1399 | parts.append(fstring[previous_index:start]) |
| 1400 | parts.append(fstring[start:end].replace(old_quote, new_quote)) |
| 1401 | previous_index = end |
| 1402 | parts.append(fstring[previous_index:]) |
| 1403 | return "".join(parts) |
| 1404 | |
| 1405 | |
| 1406 | class StringSplitter(BaseStringSplitter, CustomSplitMapMixin): |
no test coverage detected