Skip to content

Fix "pragma: no mutate" being ignored on the last line of a multi-line statement - #629

Open
musharna wants to merge 1 commit into
boxed:mainfrom
musharna:fix-pragma-closing-line
Open

musharna wants to merge 1 commit into
boxed:mainfrom
musharna:fix-pragma-closing-line

Conversation

@musharna

@musharna musharna commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

A # pragma: no mutate after the closing bracket of a multi-line statement is ignored, so that line is still mutated:

def closing(a, b):
    return max(
        a + 1,
        b * 2)  # pragma: no mutate

The comment is the statement's trailing comment, but visit_SimpleStatementLine records only the line the statement starts on (return max(), not the line the comment is on. #614 listed this as left alone. README:375 says the bare pragma disables mutation "on this single line", so this marks the comment's own line too. The start line stays marked, so nothing suppressed today becomes mutated.

Real run (mutmut run on a small package with the function above and a single-line return max(a + 1, b * 2) # pragma: no mutate as a control):

  • main: 4 mutants in closing, 2 of them on b * 2; the control has 0
  • this branch: 2 mutants in closing, both on a + 1; the control has 0

Tests: a PragmaVisitor case and an end-to-end case in test_mutation.py. With pragma_handling.py reverted to main, both fail ({3} == {3, 5}; b / 2 is generated). uv run pytest: 473 passed, 4 skipped; two tests/workers/test_timeout.py cases failed under load (that file fails 1–2 of 8 on main here too) and it passes 8/8 when run alone. pre-commit run --all-files is clean.

Not changed: when ) sits on a line of its own, its pragma covers that line and the first line only, so the arguments are still mutated. # pragma: no mutate start/end covers a whole statement.

AI assistance: drafted with Claude Opus 5.5 (claude-opus-5-5); I ran the checks above.

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