Skip to content

Detect commented-out code that spans several lines - #64

Open
Eljees wants to merge 1 commit into
PyCQA:masterfrom
Eljees:fix/36-multiline-commented-out-code
Open

Eljees wants to merge 1 commit into
PyCQA:masterfrom
Eljees:fix/36-multiline-commented-out-code

Conversation

@Eljees

@Eljees Eljees commented Aug 14, 2026

Copy link
Copy Markdown

Fixes #36.

commented_out_code_line_numbers tests each comment line on its own, and the test ends in compile(). A call that was wrapped when it was commented out never compiles line by line — foo(a, is not a statement — so nothing is reported:

x = 1
# foo(a,
#     b)
y = 2

The module already handles continuation lines, but that path only covers an assignment, not a call.

Consecutive comment lines are now grouped into bracket-balanced fragments and compiled as a whole. A group is only formed when a line is left unfinished by its brackets, so ordinary prose — balanced line by line — never forms one.

Measured

python -m unittest test_eradicate: 30 passed before and after.

input before after
# foo(a, / # b) not reported reported
# run(timeout=5, / # retries=3) not reported reported
# xs = [1, / # 2] not reported reported
# Hello / # World not reported not reported
# We do this (see / # the docs) not reported not reported
# Note that x (the / # counter) is reset not reported not reported
# As shown in (Smith / # 2019), this works not reported not reported
# noqa: E501 ( / # continued line 2 only line 2 only
# TODO: foo( / # bar) not reported not reported

Two phrases do change: # Hello (world / # and more) and # items (a, b, / # c, d) become reported. Both compile as calls, and the current release already reports them when the same phrase is written on one line# items (a, b, c, d) is reported today. So this makes the wrapped and unwrapped spellings agree rather than adding a new class of match.

The whitelist protecting noqa and TODO comments moved into a shared helper. It used to sit inside the single-line branch only, so without the move a protected comment could have been swallowed as part of a group.

AI-assisted (LLM used for drafting); the runs above are mine.

commented_out_code_line_numbers tests each comment line on its own and the
test ends in compile(), so a call that was wrapped when it was commented out
is never recognised: 'foo(a,' is not a statement by itself. The module
already has a mechanism for continuation lines, but it only covers an
assignment, not a call.

Consecutive comment lines are now grouped into bracket-balanced fragments and
compiled as a whole. A group is only formed when a line is left unfinished by
its brackets, so ordinary prose, which is balanced line by line, is untouched.

The whitelist that protects 'noqa' and 'TODO' comments moved into a shared
helper; it used to sit inside the single-line branch only, which would have
let a protected comment be swallowed as part of a group.

Fixes PyCQA#36
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.

Commented out code not detected when code block is two lines long

1 participant