pytest-dev / pytest-dev/pytest
Using stdlib `runpy.run_module()` causes `AttributeError: 'AssertionRewritingHook' object has no attribute 'get_code'`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
When using runpy's run_module function inside a pytest test and pointing to the very module of the test being run the execution will crash reporting AttributeError: 'AssertionRewritingHook' object has no attribute 'get_code'.
Minimum reproducible example:
# tests/dummy_test.py
import runpy
def test():
runpy.run_module("tests.dummy_test")
# requirements.txt
pytest==6.2.4
System info:
- OS: Ubuntu 20.04.2 LTS
- Python Version: 3.8.10
Additional observations:
- Calling an external function from other file in the test which calls
runpy::run_moduleusing the test's module as parameter will still yield the same error. - Calling
runpy::run_modulepassing any module other than the test's very own module will yield no error. - Using
runpy::run_pathinstead ofrun_module, passing the test's very own file path, will yield no error.
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 minimal reproduction in tests/dummy_test.py and run it with pytest==6.2.4 on Python 3.8.10. Trace the import path used by runpy.run_module for the test's own module and the AssertionRewritingHook, then verify that the reproduction completes without the get_code AttributeError while runpy.run_path remains unaffected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100