Repository navigation
Fix "pragma: no mutate" being ignored inside multi-line statements - #614
Merged
Merged
Conversation
Owner
|
Thanks! |
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A
# pragma: no mutateat the end of a line inside brackets is ignored, so that line is still mutated:visit_SimpleStatementLineonly readsnode.trailing_whitespace.comment. A comment that ends a line inside a call, a literal or a parenthesized condition belongs to aParenthesizedWhitespacenode instead, so no visitor sees it. README:375 says the bare pragma disables mutation "on this single line".This adds
visit_ParenthesizedWhitespace, which records that line for a bare pragma. Only that line is skipped; the other lines of the statement are still mutated.Real run (
mutmut runon a small package with the function above, a dict-literal version, and a single-linereturn a + 1 # pragma: no mutateas a control):main: 16 mutants; 4 of 8 ininnerand 4 of 8 in the dict function change the pragma line; the single-line control has 0Tests: three
PragmaVisitorcases (call argument, dict entry, parenthesizedifcondition), a control showing that other comments inside brackets are ignored, and two end-to-end cases intest_mutation.py. Withpragma_handling.pyreverted tomain, exactly the 5 new cases fail and the control passes.uv run pytest: 472 passed, 4 skipped; onetests/workers/test_timeout.pycase failed under heavy load (onmain, a different timeout case failed the same way), and that file passes 8/8 when run alone.pre-commit run --all-filesis clean.Not changed:
)line still maps to the statement's first line (return max(), so the arguments are mutated. It's unclear whether that pragma should cover the whole statement, so I left it alone.block/start/endinside brackets are not handled; only the bare pragma is.AI assistance: this change and its tests were drafted with Claude Opus 5.5 (
claude-opus-5-5). I ran the commands and numbers above locally.