Skip to content

Whitelist rsplit #389

Closed
Closed

Description

@fabi321

str.rsplit behaves exactly like str.split, if maxsplit is not provided, so in that case there should be no mutation.

Activity

  1. fabi321 commented on May 30, 2025

    @fabi321
    ContributorAuthor

    Upon looking mutations, I also fund another false positive: I have assert False in my code, in order to cover branches that are not intended to be hit. Changing them to true doesn't make much sense.
    It also tried to mutate the string of the assert, which also does not make too much sense.

    In general, I don't feel like it's that useful to test for mutations in asserts, as they are mostly a defensive measure (they can be disabled, after all).

  2. Otto-AA commented on Jun 1, 2025

    @Otto-AA
    Collaborator

    str.rsplit behaves exactly like str.split, if maxsplit is not provided, so in that case there should be no mutation.

    Yes, without the maxsplit argument this mutation cannot be detected by the tests. We should change not mutate in this case. If you want to take a shot at this, I would suggest to add another if m.matches(...) to the operator_string_methods_swap method (outside of the loop) and remove this dict entry.

    Upon looking mutations, I also fund another false positive: I have assert False in my code, in order to cover branches that are not intended to be hit. Changing them to true doesn't make much sense.
    It also tried to mutate the string of the assert, which also does not make too much sense.

    I also do not think mutating assert false is useful. We could change this, but I think the change would not be trivial. Currently most (or all?) mutation operators do not rely on the context, so the mutation of False to True currently does not know if it is the operand of an assert statement. We could considert to add the parent metadata as additional context for operators.

    So I'm not sure if this would be worth the effort. The mutation for assert False should only show up as a mutation that was not executed (similar to uncovered code with e.g. coverage.py).

    In general, I don't feel like it's that useful to test for mutations in asserts, as they are mostly a defensive measure (they can be disabled, after all).

    I guess people could use assert statements for quick and dirty input validation, for instance:

    def save_user(username, age):
      assert 0 <= age <= 150, 'Please provide an age between 0 and 150'

    While this is not best practice, I could imagine code like this. For these cases, mutating assert statements seems reasonable to me. If this is really uncommon, we could consider not mutating assert statements, which would be rather simple to implement in mutmut.

  3. fabi321 commented on Jun 1, 2025

    @fabi321
    ContributorAuthor

    So I'm not sure if this would be worth the effort. The mutation for assert False should only show up as a mutation that was not executed (similar to uncovered code with e.g. coverage.py).

    That assertion comes up as fail. I noticed it due to that.

  4. Otto-AA commented on Jun 1, 2025

    @Otto-AA
    Collaborator

    Maybe I misunderstood something, do you have a reproducible example?

    My thinking is, that if the assert False is on unreachable code, then the mutation (assert True) should never be executed and thus reported as "no tests" (with the 🫥 emoji). And if it is reachable and executed in the tests, it probably makes sense to mutate it to assert True and check if a test is influenced by this.

  5. fabi321 commented on Jun 1, 2025

    @fabi321
    ContributorAuthor

    I don't have any minimal example, but mutmut marks https://github.com/stormworks-utils/tumfl/blob/main/tumfl/parser.py#L490 as fail, and coverage reports that that line is never hit.

    (Or rather, it would mark it, twice (due to the string mut), if it didn't have the no mut)

  6. CHB-0r1s commented on Jun 13, 2025

    @CHB-0r1s
    Contributor

    @fabi321 @Otto-AA Hi! Can you explain your point: split = rsplit without args, so we need check here?

    As string mutations was my add-on, I try to help with fix it, if it may be helpful

  7. Otto-AA commented on Jun 20, 2025

    @Otto-AA
    Collaborator

    The split issue is fixed with #394 .

    For the issue regarding assert statements, the problem is that we only track coverage per function rather than per line. I have created a separate issue: #395

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions