Skip to content

Commit

Permalink
fix: handle python3.12 f-string tokenization [closes #210]
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Jan 9, 2024
1 parent 3e501a1 commit b7e0e47
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions snakefmt/parser/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
tokenize.NUMBER: {tokenize.NAME, tokenize.OP},
tokenize.OP: {tokenize.NAME, tokenize.STRING, tokenize.NUMBER, tokenize.OP},
}
# add fstring start to spacing_triggers if python 3.12 or higher
if hasattr(tokenize, "FSTRING_START"):
spacing_triggers[tokenize.NAME].add(tokenize.FSTRING_START)
spacing_triggers[tokenize.OP].add(tokenize.FSTRING_START)


def operator_skip_spacing(prev_token: Token, token: Token) -> bool:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ def test_decorator_is_handled_correctly(self):
actual = formatter.get_formatted()
assert actual == snakecode

def test_f_strings(self):
"""This is relevant for python3.12"""
snakecode = 'a = f"{1 + 2}" if 1 > 0 else f"{1 - 2}"\n'
formatter = setup_formatter(snakecode)

actual = formatter.get_formatted()
assert actual == snakecode


class TestComplexPythonFormatting:
"""
Expand Down

0 comments on commit b7e0e47

Please sign in to comment.