pytest-dev / pytest-dev/pytest

Using stdlib `runpy.run_module()` causes `AttributeError: 'AssertionRewritingHook' object has no attribute 'get_code'`

Open
#9,007 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: rewrite
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_module using the test's module as parameter will still yield the same error.
  • Calling runpy::run_module passing any module other than the test's very own module will yield no error.
  • Using runpy::run_path instead of run_module, passing the test's very own file path, will yield no error.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.