Skip to content

Don't produce equivalent case mutants inside string escape sequences - #611

Merged
boxed merged 2 commits into
boxed:mainfrom
by-ae:fix/escape-sequence-case-mutants
Oct 23, 2026
Merged

boxed merged 2 commits into
boxed:mainfrom
by-ae:fix/escape-sequence-case-mutants

Conversation

@by-ae

@by-ae by-ae commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

String mutations that case-flip characters were also changing the hex digits inside escape sequences like \u00a0, \xHH, \UHHHHHHHH, and the names inside \N{NAME}. Python treats those hex digits and unicode names case-insensitively, so mutants like "\u00A0" are equivalent to the original "\u00a0" and can never be killed by tests.

What changed

Modified the NON_ESCAPE_SEQUENCE regex in src/mutmut/mutation/mutators.py to match and preserve complete escape sequences with case-insensitive hex digits or names (\xHH, \uHHHH, \UHHHHHHHH, \N{NAME}), while still allowing case transformations for regular text and for raw strings (where escape sequences are literal and case changes are not equivalent).

Updated the existing test expectation for \N{ghost} \U11223344 to reflect that these are now preserved, and adjusted the e2e snapshot (4 mutants instead of 5, dropping the equivalent one).

How it was tested

  • Added test cases covering \u, \U, \x, \N{...} escapes and mixed content
  • Added a test for raw strings (r"..."), where case changes are not equivalent and still happen
  • Full test suite passes: 467 passed, 4 skipped
  • All pre-commit hooks pass (ruff, mypy, formatting)

An AI coding assistant helped with the implementation.

Fixes #609

by-ae and others added 2 commits October 23, 2026 10:16
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
@boxed
boxed merged commit 09dc5dd into boxed:main Oct 23, 2026
7 checks passed
@boxed

boxed commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Thanks!

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.

Using "\u" escape sequence can produce equivalent mutations.

2 participants