Skip to content

Refactor trampoline setup#514

Merged
Otto-AA merged 6 commits intomainfrom
refactor-trampoline-default-args
May 9, 2026
Merged

Refactor trampoline setup#514
Otto-AA merged 6 commits intomainfrom
refactor-trampoline-default-args

Conversation

@Otto-AA
Copy link
Copy Markdown
Collaborator

@Otto-AA Otto-AA commented May 3, 2026

This PR makes following changes:

  • use a @_mutmut_mutated decorator as a trampoline. This allows using *args and **kwargs for default args, while
    keeping dynamic and static type signature the same (fixes Fix default argument mutation #477 )
  • instead of generating the trampoline as a string, we now import it from mutmut
  • unified handling of normal class methods, @staticmethod, @classmethod
    and enums (always put dictionary outside the class body, and methods inside class body)
  • stack frame counting only includes source_paths code (fixes Only count user-code frames for max_stack_depth #378 )

The test_mutation_regression.py test shows how the mutation changed. Now it's like this:

class Adder:
  def add(self, value):
    return self.amount+ value

becomes

from mutmut.mutation.trampoline import wrap_in_trampoline as _mutmut_mutated, MutantDict

mutants_xǁAdderǁadd__mutmut: MutantDict = {}  # type: ignore

class Adder:
    @_mutmut_mutated(mutants_xǁAdderǁadd__mutmut)
    def add(self, value):
        return self.amount + value # <- this implementation here is not used; only kept for type checking

    def xǁAdderǁadd__mutmut_orig(self, value):
        return self.amount + value

    def xǁAdderǁadd__mutmut_1(self, value):
        return self.amount - value

mutants_xǁAdderǁadd__mutmut['_mutmut_orig'] = Adder.xǁAdderǁadd__mutmut_orig # type: ignore # mutmut generated
mutants_xǁAdderǁadd__mutmut['xǁAdderǁadd__mutmut_1'] = Adder.xǁAdderǁadd__mutmut_1 # type: ignore # mutmut generated

Overall I'm pretty happy with this design, as it works for everything we support currently (normal class methods, @staticmethod, @classmethod and enum). For the @classmethod, we need to have some special handling s.t. we call the method bound to the cls class (see is_classmethod in trampoline.py). Apart from that the trampoline only passes the args and kwargs to the appropriate original/mutated method.

@nicklafleur As you already know this part of the code, happy for feedback. Also if something you want to build on top of this would not work with this design, let me know :)

And the runtime tests are pretty handy ❤️

Copy link
Copy Markdown
Collaborator

@nicklafleur nicklafleur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one extremely minor nit, otherwise I really really like the design of this, reads much more clean than what I came up with!

It's not immediately obvious to me where the stack tracking will fit in for the cache invalidation, but I'm sure that there's some options as far as reflection goes in the decorator that we can work with so it should be more than possible just a matter of rebasing and giving it a shot (I should have time to try it out this week).

trampoline templates are resilient to forward references when using the external trampoline
pattern.
"""
async def async_get_all() -> AsyncGenerator["Color", None]:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def async_get_all() -> AsyncGenerator["Color", None]:
async def async_get_all() -> AsyncGenerator[Color, None]:

The unquoted Color here is a meaningful test signal for handling unquoted py3.10 forward references

Copy link
Copy Markdown
Collaborator Author

@Otto-AA Otto-AA May 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only Python 3.14+ supports unquoted forward references, so I don't think we need to support this for Python 3.10? Previously, running mutmut with this code worked, because we manually quoted the references and never executed the unquoted forward references. But running pytest in the e2e_projects/my_lib on main with python 3.10 raises following error:

tests/test_my_lib.py:3: in <module>
    from my_lib import *
src/my_lib/__init__.py:152: in <module>
    class Color(Enum):
src/my_lib/__init__.py:162: in Color
    async def async_get_all() -> AsyncGenerator[Color, None]:
E   NameError: name 'Color' is not defined

i.e. it's invalid code for python 3.10, only mutmut made it valid while mutating the code.

But it's a good point that we should test for forward references in classes, I've added them for the python 3.14+ tests: c720999

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll merge it as I think the py 3.14 tests should cover this. If I missed something, feel free to ping me or add the test case afterwards.

@Otto-AA
Copy link
Copy Markdown
Collaborator Author

Otto-AA commented May 9, 2026

It's not immediately obvious to me where the stack tracking will fit in for the cache invalidation, but I'm sure that there's some options as far as reflection goes in the decorator that we can work with so it should be more than possible just a matter of rebasing and giving it a shot (I should have time to try it out this week).

I'd see two possibilities:

Both seem reasonable approaches to me, the first one maybe a bit simpler.

Otto-AA added 6 commits May 9, 2026 14:04
- decorator allows using *args and **kwargs for default args, while
  keeping dynamic and static type signature the same
- moved trampoline inside of mutmut code, instead of generating it as
  string
- unified handling of normal class methods, @staticmethod, @classmethod
  and enums (always dictionary outside, and methods inside class body)
- ignore mutmut trampoline
- ignore 3rd party library frames
The PR #513 used os.path.realpath for this.
I think pathlib.Path.resolve() should also work fine,
and we can keep using pathlib.Path.

Using strict=True, as all files/directories should exist.
@Otto-AA Otto-AA force-pushed the refactor-trampoline-default-args branch from 757c616 to dd66f3a Compare May 9, 2026 12:05
@Otto-AA Otto-AA merged commit 4b61167 into main May 9, 2026
10 checks passed
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.

Fix default argument mutation Only count user-code frames for max_stack_depth

2 participants