Skip to content

Fix: The discovery hypothesis (missing look-ahead in... - #364

Open
M001N wants to merge 1 commit into
PyCQA:mainfrom
M001N:oss-engine/37797d4a-2968aafe
Open

Fix: The discovery hypothesis (missing look-ahead in...#364
M001N wants to merge 1 commit into
PyCQA:mainfrom
M001N:oss-engine/37797d4a-2968aafe

Conversation

@M001N

@M001N M001N commented Aug 16, 2026

Copy link
Copy Markdown

Summary

Replaced the strict start_row - 1 == last_pass_row adjacency check in useless_pass_line_numbers() with a small state flag (pass_pending_leading_check) that stays set across COMMENT/NL/NEWLINE/INDENT/DEDENT tokens (i.e. blank lines and comments) following a pass, and is cleared as soon as any other real code token is seen. This makes the 'pass immediately followed by another statement in the same block' look-ahead tolerant of intervening comments and blank lines, while preserving all existing semantics (indentation must still match, and a pass that truly is the sole statement in its block is left alone).

Problem

PyCQA/autoflake issue reference: #82

Root Cause

The discovery hypothesis (missing look-ahead in filter_unused_variable/filter_code) was incorrect. filter_unused_variable() always turns a literal/name-RHS assignment into 'pass' unconditionally -- that part is by design. The actual clean-up happens in a separate post-processing pass, useless_pass_line_numbers() (autoflake.py ~line 809), which tokenizes the whole file and detects a 'leading pass' (a pass immediately followed by another statement at the same indentation) so it can be stripped out again as redundant. That detection used raw token-row arithmetic: start_row - 1 == last_pass_row. A comment-only line or a blank line between the pass and the next statement emits tokenize.COMMENT/NL tokens that are not tracked, so the row gap becomes 2+ instead of 1 and the equality check silently fails -- the redundant 'pass' is therefore never removed, even though the block clearly is not empty.

Testing

PASS - all 179 tests in test_autoflake.py pass, including the 5 new regression tests covering both of the issue's exact reproducers, the low-level tokenizer function, and the pre-existing sole-statement-gets-pass behavior (unregressed).

Related Issue

#82

… more statements

useless_pass_line_numbers() detected a redundant leading 'pass' by
comparing token row numbers with strict adjacency (start_row - 1 ==
last_pass_row). This broke whenever a comment or blank line separated
the pass from the following statement, since those lines shift the row
gap to more than 1 without the check accounting for them. As a result,
filter_unused_variable()'s 'pass' placeholder for a removed literal/name
assignment was never cleaned up in that case, even though the block
still had a following statement.

Replace the row-arithmetic check with a small state machine that
tracks whether only comments/blank lines have been seen since the
last pass, so the leading-pass look-ahead now correctly spans
intervening comments and blank lines.

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

1 participant