You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Ternary operator errors of not covering both branches can't be detected by coverage, so it would be useful to mutate these to enable/disable each branch. E.g.
mark="x"ifcount%2else"o"
mutation into:
mark="x"ifcount%2andFalseelse"o"
and
mark="x"ifcount%2orTrueelse"o"
This types of mutations aren't as useful for normal if statements, because branch coverage can detect when one of the alternatives isn't covered.
I would love to see this get done. My workaround is to keep a special git stash of changes to replace all ternaries with expanded if/else blocks. And mutation testing with this stash of changes did give me a surviving mutant in some private code I was working on. (I actually use my own fork with some extra hackage to raise a RuntimeError in case it finds any ternaries in the code to mutate.)
When I tried hacking in the existing code, it looks like the code in __init__.py can handle multiple mutations of single tokens such as operators, but not for anything bigger at this time. I am sure it should be pretty straightforward to update the code to support multiple mutations of larger pieces of code, unfortunately I have not yet succeeded with this.
Picking this up — PR incoming. Credit to @ryanfreckleton for #478, which is where I started; the tree has moved since (node_mutation.py is now src/mutmut/mutation/mutators.py) so it is a fresh implementation rather than a rebase.
One thing from the review on #478 I could not follow, with the measurement rather than an opinion. @Otto-AA asked to drop the precedence wrapping and use node.test directly. That holds for or True but not for and False, because and binds tighter than a top-level or:
a if b or c else d, over all 16 assignments of (a, b, c, d):
(b or c) and False differs from the original on 6 / 16
b or c and False differs on 2 / 16, and only when b is falsy
(b or c) or True differs on 2 / 16
b or c or True differs on 2 / 16 (identical to the wrapped form)
Unparenthesised, b or c and False becomes b or (c and False), which still takes the true branch whenever b is truthy — a mutant tests mostly cannot kill. So the PR wraps both: or True does not need it, but one rule is simpler than two and it costs nothing.
Happy to drop the wrapping on the or True half if you would rather have the asymmetry.
Activity
boxed commentedon Jan 9, 2021
Seems reasonable. Would you like to give it a shot to implement this?
ryanfreckleton commentedon Jan 22, 2021
Sure! Apologies for the late response.
I’ll post right before I start implementing, in case anyone else wants to take it on.
brody2consult commentedon Feb 19, 2024
I would love to see this get done. My workaround is to keep a special git stash of changes to replace all ternaries with expanded if/else blocks. And mutation testing with this stash of changes did give me a surviving mutant in some private code I was working on. (I actually use my own fork with some extra hackage to raise a RuntimeError in case it finds any ternaries in the code to mutate.)
When I tried hacking in the existing code, it looks like the code in
__init__.pycan handle multiple mutations of single tokens such as operators, but not for anything bigger at this time. I am sure it should be pretty straightforward to update the code to support multiple mutations of larger pieces of code, unfortunately I have not yet succeeded with this.Otto-AA commentedon Feb 17, 2026
If someone wants to implement this, here are some pointers:
node_mutation.py(see e.g.operator_number)a if foo else bis https://libcst.readthedocs.io/en/latest/nodes.html#libcst.IfExpespressolee commentedon Aug 2, 2026
Picking this up — PR incoming. Credit to @ryanfreckleton for #478, which is where I started; the tree has moved since (
node_mutation.pyis nowsrc/mutmut/mutation/mutators.py) so it is a fresh implementation rather than a rebase.One thing from the review on #478 I could not follow, with the measurement rather than an opinion. @Otto-AA asked to drop the precedence wrapping and use
node.testdirectly. That holds foror Truebut not forand False, becauseandbinds tighter than a top-levelor:Unparenthesised,
b or c and Falsebecomesb or (c and False), which still takes the true branch wheneverbis truthy — a mutant tests mostly cannot kill. So the PR wraps both:or Truedoes not need it, but one rule is simpler than two and it costs nothing.Happy to drop the wrapping on the
or Truehalf if you would rather have the asymmetry.