Skip to content

fix: compilation error when final line contains an f-string - #4234

Merged
johanneskoester merged 4 commits into
snakemake:mainfrom
schnea:parser_fstring
Jul 28, 2026
Merged

johanneskoester merged 4 commits into
snakemake:mainfrom
schnea:parser_fstring

Conversation

@schnea

@schnea schnea commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

fixes #3918 .

Adds a check for a final newline character to the parse_fstring function. Previously, the Snakefile

rule align:
    input: "reads/{sample}.fastq"
    output: "aligned/{sample}.bam"
    threads: 8
    shell:
        f"bwa mem -t {threads} {input} > {output}"

(with no final newline) would not pick up the final f-string, resulting in an erroneous compilation:

@workflow.shellcmd (

                                )
@workflow.run
def __rule_1(input, output, params, wildcards, threads, resources, log, rule, conda_env, container_img, singularity_args, use_singularity, env_modules, bench_record, jobid, is_shell, bench_iteration, cleanup_scripts, shadow_dir, edit_notebook, conda_base_path, basedir, sourcecache_path, runtime_sourcecache_path, runtime_paths, __is_snakemake_rule_func=True):
                                        shell ( , bench_record=bench_record, bench_iteration=bench_iteration
                                ) 

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • 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).
  • I, as a human being, have checked each line of code in this pull request

AI-assistance disclosure

I used AI assistance for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an edge case in f-string parsing for files that don’t end with a trailing newline.
  • Refactor

    • Improved consistency in typing/import usage and reformatted internal code and generated messages for readability, without changing behavior.

@schnea
schnea requested a review from johanneskoester as a code owner June 18, 2026 12:08
@coderabbitai

coderabbitai Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 0febbccd-fa39-4235-8f58-6dd24fe3d7f4

📥 Commits

Reviewing files that changed from the base of the PR and between ac4d2b2 and 6d7fd02.

📒 Files selected for processing (1)
  • src/snakemake/parser.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/snakemake/parser.py

📝 Walkthrough

Walkthrough

src/snakemake/parser.py fixes f-string extraction when the final source line lacks a trailing newline. The remaining changes consolidate typing imports and rewrap assertions, decorators, and syntax-error messages without changing their semantics.

Changes

Parser f-string fix and formatting cleanup

Layer / File(s) Summary
F-string end-index handling
src/snakemake/parser.py
parse_fstring adjusts the extracted substring end index according to whether the token source line ends with "\n".
Parser formatting cleanup
src/snakemake/parser.py
Typing imports, an assertion, the generated rule decorator, and multiple syntax-error messages are reformatted without changing logic or message text.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main fix: a compilation error when a final-line f-string lacks a trailing newline.
Linked Issues check ✅ Passed The PR addresses #3918 by fixing final-line f-string parsing so loop-defined rules compile correctly without a trailing newline.
Out of Scope Changes check ✅ Passed The extra edits are limited to formatting and message rewrapping around the same parser area, with no unrelated feature changes.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/snakemake/parser.py`:
- Around line 149-153: The condition checking for trailing newlines in the
parse_fstring function is inverted. Currently, the `-1` adjustment is applied in
the else branch when the line does NOT end with a newline, but it should be
applied in the if branch when the line DOES end with a newline. Swap the logic
between the two branches: move the `- 1` offset to the branch that checks `if
t1.line.endswith("\n")`, and remove it from the else branch that handles EOF
lines without newlines.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f2583655-3ddf-4543-8233-d08a34d0f49e

📥 Commits

Reviewing files that changed from the base of the PR and between 3d933e6 and ac4d2b2.

📒 Files selected for processing (1)
  • src/snakemake/parser.py

Comment thread src/snakemake/parser.py Resolved
@johanneskoester
johanneskoester enabled auto-merge (squash) July 28, 2026 10:37
@johanneskoester
johanneskoester merged commit 9c5e7fe into snakemake:main Jul 28, 2026
84 of 88 checks passed
johanneskoester pushed a commit that referenced this pull request Jul 30, 2026
🤖 I have created a release *beep* *boop*
---


##
[9.24.0](v9.23.1...v9.24.0)
(2026-07-30)


### Features

* allow to combine cases with otherwise in branch function
([#4262](#4262))
([000ff52](000ff52))


### Bug Fixes

* avoid config hash crash for non-serializable values
([#4241](#4241))
([1a27746](1a27746))
* compilation error when final line contains an f-string
([#4234](#4234))
([9c5e7fe](9c5e7fe))
* Group error log formatting
([#4256](#4256))
([bb6dcde](bb6dcde))
* leak of benchmark monitor threads
([#4259](#4259))
([d37852e](d37852e))
* no benchmark warning when a process exits before it can be sampled
([#4260](#4260))
([6738359](6738359))
* persistence starttime unset/defaults
([#4251](#4251))
([56395c0](56395c0))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
@coderabbitai coderabbitai Bot mentioned this pull request Aug 4, 2026
4 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with loop defined rules if final newline character missing

2 participants