pytest-dev / pytest-dev/pytest
Add hooks to allow further AST customization and control pyc caching while assertion rewriting.
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
What's the problem this feature will solve?
SlipCover and soon also Coverage.py modify the AST while loading modules, including test modules, to facilitate measuring branch coverage. However, pytest's assertion rewriting loader overrides that mechanism.
Describe the solution you'd like
I suggest that a hook be added to allow plugins to further modify the AST before it is compiled (or, alternatively, provide an already-modified AST for pytest to assertion rewrite and compile).
Because the modified AST leads to bytecode that only makes sense when also measuring coverage, and in some cases is run specific, I suggest that hooks also be added that allows the plugin to either prevent pyc caching or add to its file name, so that it indicates it has been modified for use with a certain SlipCover and/or Coverage.py version.
Alternative Solutions
I have a workaround for SlipCover that dynamically modifies _pytest.assertion.rewrite to insert hooks, but that could break as that module is modified, making it hard to maintain.
Additional context
I described the mechanism used by SlipCover, and which @nedbat has been working on integrating into Coverage.py, in my recent PyCon'24 talk. I'd be happy to provide further details, or even submit a PR.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the _pytest.assertion.rewrite module and the assertion-rewriting loader described in the issue. Review how the loader modifies and compiles ASTs and handles pyc caching; done means providing stable plugin hooks for AST customization and for preventing or distinguishing cached bytecode.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100