Skip to content

Commit

Permalink
fix: don't remove double braces in f-strings in rule directives [closes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhall88 committed Jan 9, 2024
1 parent c961ddb commit 8b47454
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
11 changes: 10 additions & 1 deletion snakefmt/parser/syntax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Code in charge of parsing and validating Snakemake syntax
"""
import sys
import tokenize
from abc import ABC, abstractmethod
from re import match as re_match
Expand Down Expand Up @@ -329,9 +330,17 @@ def in_brackets(self):
def parse_params(self, snakefile: TokenIterator):
cur_param = Parameter(self.token)
prev_token = None

while True:
cur_param = self.process_token(cur_param, prev_token)
if (
self.token is not None
and sys.version_info >= (3, 12)
and self.token.type == tokenize.FSTRING_MIDDLE
):
if self.token.string.endswith("}"):
cur_param.value += "}"
elif self.token.string.endswith("{"):
cur_param.value += "{"
try:
prev_token = self.token
self.token = next(snakefile)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,23 @@ def test_tpq_inside_run_block(self):

assert formatter.get_formatted() == snakecode

def test_f_string_with_double_braces_in_input(self):
"""rule align:
input:
sequences="results/{build_name}/filtered.fasta",
params:
translation_template=lambda w: f"{w.build_name}/{{cds}}.fasta","""
snakecode = (
"rule align:\n"
f"{TAB * 1}input:\n"
f'{TAB * 2}sequences="results/{{build_name}}/filtered.fasta",\n'
f"{TAB * 1}params:\n"
f"{TAB * 2}"
+ 'translation_template=lambda w: f"{w.build_name}/{{cds}}.fasta",\n'
)
formatter = setup_formatter(snakecode)
assert formatter.get_formatted() == snakecode


class TestReformatting_SMK_BREAK:
"""
Expand Down

0 comments on commit 8b47454

Please sign in to comment.