Skip to content

fix(filter): handle single-line Python docstrings - #4196

Closed
Yubel426 wants to merge 1 commit into
rtk-ai:developfrom
Yubel426:fix/filter-single-line-python-docstrings
Closed

Yubel426 wants to merge 1 commit into
rtk-ai:developfrom
Yubel426:fix/filter-single-line-python-docstrings

Conversation

@Yubel426

Copy link
Copy Markdown

Summary

Fixes #1322.

MinimalFilter enters docstring mode when a Python docstring opens and closes on the same line, causing subsequent comments to remain in the output.

Only toggle the state when entering or leaving a multiline docstring. This keeps single-line docstrings intact while allowing subsequent comments to be stripped.

Adds regression coverage for single-line, empty, Unicode, and multiline docstrings, including a single-line docstring with a trailing comment.

Test plan

  • cargo fmt --all --check && cargo clippy --all-targets --locked && cargo test --locked: 3,922 passed, 8 ignored.
  • All 12 filter tests passed after the final test-text edit.
  • Manual testing: rtk read sample.py --level minimal removes subsequent comments while preserving the docstring; --level none preserves the original file.

Important: All PRs must target the develop branch (not master).
See CONTRIBUTING.md for details.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working filter good first issue Good for newcomers python labels Sep 22, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟢 Risk low

Summary

Fixes MinimalFilter incorrectly entering docstring mode when a Python docstring opens and closes on the same line, which caused subsequent comments to remain unstripped. Adds regression tests covering single-line, empty, Unicode, and multiline docstring cases.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #1322, #2712


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@Protocol-zero-0 Protocol-zero-0 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.

Tested this locally: the new single-line and multiline docstring tests pass (I ran them on this branch merged with #4205, see below).

One interaction worth knowing before it lands. Today, a one-line docstring leaves the filter stuck "inside a docstring" for the rest of the file (#1322), and that stuck state happens to hide a separate bug: a line like s = """ is treated as a one-line block comment and deleted (finding 2 of #3425). With this fix alone, the stuck state goes away and that deletion starts showing up after every one-line docstring:

def f():
    """One-line docstring."""
    # strip me
    s = """
    multi
    """
    return s
$ rtk read d.py -l minimal     # this branch
def f():
    """One-line docstring."""
    multi
    """
    return s

s = """ is gone, so multi reads as code and the lone """ opens a string.

I've opened #4205 for the underlying block-comment detection (#3425). It doesn't touch the branch this PR changes, the two merge cleanly, and with both applied the output is right: docstring kept, # strip me removed, s = """ kept. So there's nothing to change here; landing #4205 before or alongside this one avoids the window where the bug is more visible.

@Yubel426

Copy link
Copy Markdown
Author

@KuSh Could you please review it? Thanks!

@KuSh

KuSh commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Round 1 of 3 — DUPLICATE, closing in favour of #3336

Claim: stop a one-line Python docstring ("""Short.""") from leaving MinimalFilter stuck in docstring mode, so later # comments are stripped (#1322).
Scope: duplicate of #3336 — it fixes the same stuck state as part of a string-aware Python path, and on real code it also removes the deletions this change newly exposes (numbers below). (round 1, frozen)
Ran: rtk read -l minimal (the hook leaves rtk read … -l minimal unchanged) on 9,690 real .py files (CPython 3.14 stdlib + Debian dist-packages), scored against a tokenize reference that drops only comment-only lines outside strings. Binaries: merge-base b748a5f, this head 8622970, head merged into develop, develop + #4205, head + #4205, develop + #3336; six distinct md5s.
Matrix: executed this round.
CI @86229700: never ran (only the CLA check).

Thanks for the fix and the tests. The stuck state is real, and this change clears it: your repro strips both # remove this lines, and files that still show leftover comments drop from 3,073 to 1,172.

The interaction @Protocol-zero-0 described is wider than the s = """ case, though. On develop, a line that contains """ without starting with it is dropped as a one-line "block comment" (#3425): x = r""", an r"""…""" docstring, doc=r"""name""" inside a property(...) call. The stuck state was hiding that for everything after a one-line docstring, and this change removes it:

binary files losing code lines lost files keeping comments
develop 1,562 5,342 3,073
this PR 1,501 5,873 1,172
develop + #3336 2 8 57

Per file, develop → this PR: 153 files that were intact on develop now lose code, and 283 lose more than before (+1,260 lines). The worst is gpg/gpgme.py, which loses 552 lines, whole property(…, doc=r"""…""") definitions included. With #4205 on top the regression mostly goes away, but not entirely: 5 files still lose more than on develop + #4205 alone.

#3336 handles the one-line docstring and the """-inside-a-line case together. No file is worse than on develop, and it passes your repro and @Protocol-zero-0's. With it merged, the Python branch this PR touches is no longer reached, so I'm closing this in favour of #3336. Review or comments on #3336 are welcome. If you find a case that this PR handles and #3336 does not, reply here and I will reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working filter good first issue Good for newcomers python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MinimalFilter: single-line Python docstring ("""...""") leaves in_docstring=true, suppressing subsequent comment stripping

3 participants