Skip to content

Commit eebc93d

Browse files
janselpytorchmergebot
authored andcommitted
Better fix for f-strings in set_linter for py3.12 (pytorch#143725)
pytorch#143628 didn't handle a few cases right for example: ```py $ python3 tools/linter/adapters/set_linter.py torch/_inductor/scheduler.py torch/_inductor/scheduler.py:261:24: Builtin `set` is deprecated 259 | multiline=False, 260 | ) 261 | return f"{self}{data_str}" ^ 262 | 263 | def log_details(self) -> None: torch/_inductor/scheduler.py:261:33: Builtin `set` is deprecated 259 | multiline=False, 260 | ) 261 | return f"{self}{data_str}" ^ 262 | 263 | def log_details(self) -> None: ``` also multi-line fstrings Pull Request resolved: pytorch#143725 Approved by: https://github.com/yanboliang
1 parent 41cdc7f commit eebc93d

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

tools/linter/adapters/_linter.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@
1616
from typing_extensions import Never
1717

1818

19-
FSTRING_TOKENS = dict.fromkeys(
20-
[
21-
getattr(token, "FSTRING_START", -1),
22-
getattr(token, "FSTRING_MIDDLE", -1),
23-
getattr(token, "FSTRING_END", -1),
24-
]
25-
)
19+
FSTRING_START = getattr(token, "FSTRING_START", None) # py3.12+
20+
FSTRING_END = getattr(token, "FSTRING_END", None)
2621
EMPTY_TOKENS = dict.fromkeys(
2722
[
2823
token.COMMENT,
@@ -294,10 +289,18 @@ def bracket_pairs(tokens: Sequence[TokenInfo]) -> dict[int, int]:
294289
elif inv := BRACKETS_INV.get(t.string):
295290
ParseError.check(stack, t, "Never opened")
296291
begin = stack.pop()
297-
braces[begin] = i
292+
293+
if not (stack and stack[-1] == FSTRING_START):
294+
braces[begin] = i
298295

299296
b = tokens[begin].string
300297
ParseError.check(b == inv, t, f"Mismatched braces '{b}' at {begin}")
298+
elif FSTRING_START and t.type == FSTRING_START:
299+
stack.append(FSTRING_START)
300+
elif FSTRING_END and t.type == FSTRING_END:
301+
ParseError.check(
302+
stack.pop() == FSTRING_START, t, "Mismatched FSTRING_START/FSTRING_END"
303+
)
301304

302305
if tokens:
303306
ParseError.check(not stack, t, "Left open")

tools/linter/adapters/set_linter.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ def is_set(self, i: int) -> bool:
131131
return True
132132

133133
def is_braced_set(self, begin: int, end: int) -> bool:
134-
if (
135-
begin + 1 == end
136-
or self.tokens[begin].string != "{"
137-
or self.tokens[begin - 1].type in _linter.FSTRING_TOKENS
138-
):
134+
if begin + 1 == end or self.tokens[begin].string != "{":
139135
return False
140136
i = begin + 1
141137
empty = True

0 commit comments

Comments
 (0)