Conversation
There was a problem hiding this comment.
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]: |
There was a problem hiding this comment.
| 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
There was a problem hiding this comment.
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 definedi.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
There was a problem hiding this comment.
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.
I'd see two possibilities:
Both seem reasonable approaches to me, the first one maybe a bit simpler. |
- 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.
757c616 to
dd66f3a
Compare
This PR makes following changes:
@_mutmut_mutateddecorator as a trampoline. This allows using *args and **kwargs for default args, whilekeeping dynamic and static type signature the same (fixes Fix default argument mutation #477 )
@staticmethod,@classmethodand enums (always put dictionary outside the class body, and methods inside class body)
source_pathscode (fixes Only count user-code frames formax_stack_depth#378 )The test_mutation_regression.py test shows how the mutation changed. Now it's like this:
becomes
Overall I'm pretty happy with this design, as it works for everything we support currently (normal class methods,
@staticmethod,@classmethodand enum). For the@classmethod, we need to have some special handling s.t. we call the method bound to theclsclass (seeis_classmethodin 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 ❤️