feat: process fstring when python version gte 3.12

This commit is contained in:
ifuryst 2024-04-15 18:07:21 +08:00
parent 0d20f18212
commit 8a5e9ade6c
2 changed files with 17 additions and 0 deletions

View file

@ -48,6 +48,8 @@ def fix_strings(filename: str) -> int:
splitcontents = list(contents)
fstring_depth = 0
fstring_content = ''
f_erow = f_ecol = -1
# Iterate in reverse so the offsets are always correct
tokens_l = list(tokenize.generate_tokens(io.StringIO(contents).readline))
@ -55,8 +57,17 @@ def fix_strings(filename: str) -> int:
for token_type, token_text, (srow, scol), (erow, ecol), _ in tokens:
if token_type == FSTRING_START: # pragma: >=3.12 cover
fstring_depth += 1
splitcontents[
line_offsets[srow] + scol:
line_offsets[f_erow] + f_ecol
] = handle_match(token_text + fstring_content)
fstring_content = ''
elif token_type == FSTRING_END: # pragma: >=3.12 cover
fstring_depth -= 1
fstring_content = token_text + fstring_content
f_erow, f_ecol = erow, ecol
elif fstring_depth != 0: # pragma: >=3.12 cover
fstring_content = token_text + fstring_content
elif fstring_depth == 0 and token_type == tokenize.STRING:
new_text = handle_match(token_text)
splitcontents[

View file

@ -43,6 +43,12 @@ TESTS = (
0,
id='ignore nested fstrings',
),
pytest.param(
'f"Error during task loop"',
"f'Error during task loop'",
1,
id='process the fstrings when pyver is gte 3.12',
),
)