Skip to content

Commit 5ac923e

Browse files
committed
Patch add_trailing_comma to fix f-strings on Python 3.12
1 parent 4c7ccd5 commit 5ac923e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

formate_trailing_commas/_vendor/add_trailing_comma/_main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _fix_src(contents_text: str, min_version: tuple[int, ...]) -> str:
4343
for callback in callbacks.get(token.offset, ()):
4444
callback(i, tokens)
4545

46-
if token.src in START_BRACES:
46+
if token.name == 'OP' and token.src in START_BRACES:
4747
fix_brace(
4848
tokens, find_simple(i, tokens),
4949
add_comma=False,

formate_trailing_commas/_vendor/add_trailing_comma/_token_helpers.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def find_simple(first_brace: int, tokens: list[Token]) -> Fix | None:
2727

2828
for i in range(first_brace + 1, len(tokens)):
2929
token = tokens[i]
30-
if token.src in START_BRACES:
30+
if token.name == 'OP' and token.src in START_BRACES:
3131
brace_stack.append(i)
32-
elif token.src in END_BRACES:
32+
elif token.name == 'OP' and token.src in END_BRACES:
3333
brace_stack.pop()
3434

3535
if len(brace_stack) == 1 and token.src == ',':
@@ -94,11 +94,11 @@ def find_call(
9494
paren_stack = []
9595
for i in range(i, len(tokens)):
9696
token = tokens[i]
97-
if token.src == '(':
97+
if token.name == 'OP' and token.src == '(':
9898
paren_stack.append(i)
9999
# the ast lies to us about the beginning of parenthesized functions.
100100
# See #3. (why we make sure there's something to pop here)
101-
elif token.src == ')' and paren_stack:
101+
elif token.name == 'OP' and token.src == ')' and paren_stack:
102102
paren_stack.pop()
103103

104104
if (token.line, token.utf8_byte_offset) in arg_offsets:
@@ -132,6 +132,10 @@ def fix_brace(
132132
# Don't unhug when containing a single token (such as a triple
133133
# quoted string).
134134
first_brace + 2 == last_brace or
135+
(
136+
tokens[first_brace + 1].name == 'FSTRING_START' and
137+
tokens[last_brace - 1].name == 'FSTRING_END'
138+
) or
135139
# don't unhug if it is a single line
136140
fix_data.remove_comma
137141
):

0 commit comments

Comments
 (0)