Skip to content

Add mutation for ternary operators #196

Closed

Description

@ryanfreckleton

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" if count % 2 else "o"

mutation into:

mark = "x" if count % 2 and False else "o"

and

mark = "x" if count % 2 or True else "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.

Activity

  1. boxed commented on Jan 9, 2021

    @boxed
    Owner

    Seems reasonable. Would you like to give it a shot to implement this?

  2. ryanfreckleton commented on Jan 22, 2021

    @ryanfreckleton
    Author

    Sure! Apologies for the late response.

    I’ll post right before I start implementing, in case anyone else wants to take it on.

  3. brody2consult commented on Feb 19, 2024

    @brody2consult

    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.

  4. Otto-AA commented on Feb 17, 2026

    @Otto-AA
    Collaborator

    If someone wants to implement this, here are some pointers:

  5. espressolee commented on Aug 2, 2026

    @espressolee
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions