Skip to content

Refactor trampoline setup#514

Open
Otto-AA wants to merge 3 commits intomainfrom
refactor-trampoline-default-args
Open

Refactor trampoline setup#514
Otto-AA wants to merge 3 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 ❤️

Otto-AA added 3 commits May 3, 2026 12:41
- 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
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

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