Skip to content

Apply mutants of overloaded functions to the implementation - #671

Open
mmporong wants to merge 1 commit into
boxed:mainfrom
mmporong:fix-apply-overloaded-function
Open

mmporong wants to merge 1 commit into
boxed:mainfrom
mmporong:fix-apply-overloaded-function

Conversation

@mmporong

@mmporong mmporong commented Dec 3, 2026

Copy link
Copy Markdown

Fixes #665

mutmut apply (and the apply action in mutmut browse) wrote the mutant of an overloaded function or method over its first @overload stub instead of the implementation. The implementation further down was left unchanged, so the applied file behaved like the original, and a mutant that mutmut run reported as killed passed the tests once applied. As @willfrey found in the issue, find_function returns the first definition with the mutant's name in its scope, and in an overload group that is the first stub.

Following the approach suggested in the issue, apply_mutant now passes the mutant's source span to find_function. The meta file records it in source_span_by_key since 3.9.0. When the name is defined more than once in the scope, find_function returns the definition that contains the span. Without a recorded span (a meta file written before 3.9.0), or if no definition contains it, it returns the first definition, as before.

Tests:

  • test_apply_mutant_to_overloaded_function applies a mutant of the issue's example for a top-level function and for a method, with and without the line span index, and checks that only the implementation's return line changed. All 4 cases fail without the fix and pass with it.
  • write_mutants_dir now records the source spans the way mutmut run does. test_apply_mutant_only_changes_the_function_it_belongs_to also runs without them, so the fallback for older meta files stays covered.
  • tests/mutation: 440 passed, 1 skipped. ruff check, ruff format --check and mypy pass.

I also ran the reproducer from the issue end to end (mutmut run, mutmut apply pkg.mod.x_twice__mutmut_1, pytest). Before the fix, the first stub was replaced and pytest passed after the apply. With the fix, only return value * 2 becomes return value / 2 in the implementation and pytest fails (assert 1.0 == 4).

`mutmut apply` replaced the first definition with the mutant's name in
its scope. For a function or method with `@overload` stubs that is the
first stub, so the mutant was written over it and the implementation was
left unchanged.

Pick the definition that contains the mutant's source span, which the
meta file records since 3.9.0. Without a recorded span, or if no
definition contains it, the first definition is used as before.

Fixes boxed#665
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.

mutmut apply on an overloaded function replaces the first @overload stub, not the implementation

1 participant