Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested for loop in f-string #2881

Closed
sneuensc opened this issue May 17, 2024 · 2 comments · Fixed by #2930
Closed

nested for loop in f-string #2881

sneuensc opened this issue May 17, 2024 · 2 comments · Fixed by #2930
Labels
bug Something isn't working

Comments

@sneuensc
Copy link

Snakemake version
Bug in latest versions. Tested versions 8.7.0 and 8.11.3.
Works in version 7.18.2

Describe the bug
I often use nested 'for' loops to create a list (outside of a rule). This works fine, but if the loop is placed WITHIN an f-string it does not work with the latest versions of snakemake. Before version 8 it was working.

Logs
$ snakemake -j1
SyntaxError in file Snakefile, line 6:
invalid syntax. Perhaps you forgot a comma? (Snakefile, line 6)

Minimal example

works for both versions

a = [(x, y) for x in [1,2,3] for y in [3,1,4]]
print(f"works for both versions: {a}")

works for older versions only

print(f"works only in old version: {[(x, y) for x in [1,2,3] for y in [3,1,4]]}")

@sneuensc sneuensc added the bug Something isn't working label May 17, 2024
@cmeesters
Copy link
Member

Snakemake states an error in your Snakefile. Please provide a minimal example to produce your error using the current version of Snakemake.

@Hocnonsense
Copy link
Contributor

Yes... This is indeed a bug that caused by f-string.

In brief, python will split each 'word' in fstring such as f"works only in old version: {, [, (, x, ,, y, ), for, x, in ...
however, the space between each word are missed. Snakemake read smk file using python parser, and this is difficult to keep it.

This bug will be fixed in the future, but it is also better to use f-string in a more gentle way :)

johanneskoester pushed a commit that referenced this issue Jul 4, 2024
<!--Add a description of your PR here-->

This is another (and hopefully the last) fix for f-string change in
python3.12.

Now everthing inner f-string are ignored, and only care about where
f-string start and end, and read it from snakefile as-is.

will fix #2881 and fix #2657
related PR: #2864, #2649

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants