Skip to content

Commit

Permalink
fix: preserve double curly braces in python code [#215]
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Jan 31, 2024
1 parent 1d11dba commit 1cbcfb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions snakefmt/parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import tokenize
from abc import ABC, abstractmethod
from typing import NamedTuple, Optional
Expand Down Expand Up @@ -324,3 +325,12 @@ def get_next_queriable(self, snakefile: TokenIterator) -> Status:
if not pythonable and token.type != tokenize.COMMENT:
pythonable = True
buffer += token.string
if (
token is not None
and sys.version_info >= (3, 12)
and token.type == tokenize.FSTRING_MIDDLE
):
if token.string.endswith("}"):
buffer += "}"
elif token.string.endswith("{"):
buffer += "{"
13 changes: 13 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,19 @@ def test_f_string_with_double_braces_in_input(self):
formatter = setup_formatter(snakecode)
assert formatter.get_formatted() == snakecode

def test_f_string_with_double_braces_in_python_code(self):
"""https://github.com/snakemake/snakefmt/issues/215"""
"""def get_test_regions(wildcards):
benchmark = config["variant-calls"][wildcards.callset]["benchmark"]
return f"resources/regions/{benchmark}/test-regions.cov-{{cov}}.bed"""
snakecode = (
"def get_test_regions(wildcards):\n"
f'{TAB * 1}benchmark = config["variant-calls"][wildcards.callset]["benchmark"]\n' # noqa: E501
f'{TAB * 1}return f"resources/regions/{{benchmark}}/test-regions.cov-{{{{cov}}}}.bed"\n' # noqa: E501
)
formatter = setup_formatter(snakecode)
assert formatter.get_formatted() == snakecode


class TestReformatting_SMK_BREAK:
"""
Expand Down

0 comments on commit 1cbcfb1

Please sign in to comment.