Skip to content

fix: skip fmt-directive and indent processing for inline comments (#305)#307

Open
mbhall88 wants to merge 2 commits into
snakemake:masterfrom
mbhall88:fix/#305
Open

fix: skip fmt-directive and indent processing for inline comments (#305)#307
mbhall88 wants to merge 2 commits into
snakemake:masterfrom
mbhall88:fix/#305

Conversation

@mbhall88

@mbhall88 mbhall88 commented Jun 23, 2026

Copy link
Copy Markdown
Member

Inline comments (e.g. """ # comment) were being passed through the same fmt-directive and indentation logic as standalone comments, causing incorrect context exits and indentation changes. Detect inline comments by checking that the previous token is on the same line, and bypass block-level comment handling for them.
closes #305

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced comment handling to distinguish between inline and standalone comments, improving formatting of multiline docstrings with same-line comments while preserving correct indentation.

…akemake#305)

Inline comments (e.g. `""" # comment`) were being passed through the
same fmt-directive and indentation logic as standalone comments, causing
incorrect context exits and indentation changes. Detect inline comments
by checking that the previous token is on the same line, and bypass
block-level comment handling for them.
closes snakemake#305
@mbhall88 mbhall88 requested a review from Hocnonsense June 23, 2026 00:59
@coderabbitai

coderabbitai Bot commented Jun 23, 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

Run ID: 33805e3e-9681-4160-98f4-a145e8bce8f7

📥 Commits

Reviewing files that changed from the base of the PR and between e8fb67d and 11454e5.

📒 Files selected for processing (2)
  • snakefmt/parser/parser.py
  • tests/test_formatter.py

📝 Walkthrough

Walkthrough

In get_next_queriable, the COMMENT token branch is updated to classify comments as inline (same line as the preceding non-structural token) versus standalone. Inline comments bypass the # fmt: directive detection and indentation-snapping logic. A regression test is added for a multiline rule docstring whose closing triple-quotes share a line with a comment.

Changes

Inline comment classification and regression test

Layer / File(s) Summary
Inline vs standalone comment classification
snakefmt/parser/parser.py
get_next_queriable now checks if a COMMENT token is on the same line as the preceding non-newline/indent/dedent token; inline comments skip the # fmt: directive and _determine_comment_indent indentation-snapping path, preventing closing triple-quote duplication when a comment follows a docstring on the same line. The standalone path retains the early # fmt: return, effective-indent early return, and the consolidated fmt: off[next]/fmt: off[sort]/global fmt: off predicate.
Regression test
tests/test_formatter.py
Adds test_multiline_docstring_with_same_line_comment_inside_rule to assert correct formatting output for a rule docstring whose closing """ shares a line with a # comment.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • Hocnonsense
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately describes the main change: fixing comment handling to skip fmt-directive and indent processing for inline comments, which is the core of the changeset.
Linked Issues check ✅ Passed The PR addresses issue #305 by fixing the parser's comment token handling to distinguish inline comments from standalone comments, preventing incorrect processing and token duplication that caused InvalidPython errors.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the reported issue: parser comment handling changes and a test case validating the fix for inline comments in docstrings.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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.

@codecov

codecov Bot commented Jun 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.87%. Comparing base (e8fb67d) to head (c14f684).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #307   +/-   ##
=======================================
  Coverage   96.87%   96.87%           
=======================================
  Files          13       13           
  Lines        1662     1664    +2     
  Branches      343      344    +1     
=======================================
+ Hits         1610     1612    +2     
  Misses         24       24           
  Partials       28       28           
Flag Coverage Δ
unittests 96.81% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
snakefmt/parser/parser.py 94.72% <100.00%> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Bug: docstring with a trailing comment on the closing-quote line raises InvalidPython

1 participant