Skip to content

Commit

Permalink
Better fix for f-strings in set_linter for py3.12 (pytorch#143725)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jansel authored and pytorchmergebot committed Dec 22, 2024
1 parent 41cdc7f commit eebc93d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
19 changes: 11 additions & 8 deletions tools/linter/adapters/_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
from typing_extensions import Never


FSTRING_TOKENS = dict.fromkeys(
[
getattr(token, "FSTRING_START", -1),
getattr(token, "FSTRING_MIDDLE", -1),
getattr(token, "FSTRING_END", -1),
]
)
FSTRING_START = getattr(token, "FSTRING_START", None) # py3.12+
FSTRING_END = getattr(token, "FSTRING_END", None)
EMPTY_TOKENS = dict.fromkeys(
[
token.COMMENT,
Expand Down Expand Up @@ -294,10 +289,18 @@ def bracket_pairs(tokens: Sequence[TokenInfo]) -> dict[int, int]:
elif inv := BRACKETS_INV.get(t.string):
ParseError.check(stack, t, "Never opened")
begin = stack.pop()
braces[begin] = i

if not (stack and stack[-1] == FSTRING_START):
braces[begin] = i

b = tokens[begin].string
ParseError.check(b == inv, t, f"Mismatched braces '{b}' at {begin}")
elif FSTRING_START and t.type == FSTRING_START:
stack.append(FSTRING_START)
elif FSTRING_END and t.type == FSTRING_END:
ParseError.check(
stack.pop() == FSTRING_START, t, "Mismatched FSTRING_START/FSTRING_END"
)

if tokens:
ParseError.check(not stack, t, "Left open")
Expand Down
6 changes: 1 addition & 5 deletions tools/linter/adapters/set_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ def is_set(self, i: int) -> bool:
return True

def is_braced_set(self, begin: int, end: int) -> bool:
if (
begin + 1 == end
or self.tokens[begin].string != "{"
or self.tokens[begin - 1].type in _linter.FSTRING_TOKENS
):
if begin + 1 == end or self.tokens[begin].string != "{":
return False
i = begin + 1
empty = True
Expand Down

0 comments on commit eebc93d

Please sign in to comment.