Description
- a detailed description of the bug or problem you are having
- output of
pip list
from the virtual environment you are using - pytest and operating system versions
- minimal example if possible
Description of the bug:
Pytest when executed under --import-mode=importlib
doesn't try to retain the child module as parent module's attribute if parent module is already in sys.path
. This started happening in pytest 7.1.3 after enabling doctest to respect import-mode
in #10088.
Minimal Example
File structure
pytest_importlib_issue/
├── __init__.py
├── temp.py
└── tests/
└── test_temp.py
temp.py
def x():
"""
>>> assert 1==1
"""
return 2
test_temp.py
import pytest_importlib_issue
from pytest_importlib_issue import temp
def test_x():
assert temp.x() == 2
assert pytest_importlib_issue.temp.x() == 2
Output - MacOS 12.6
pytest7.1.3 without importlib
(pytest7.1.3) akhileshr@Akhileshs-MacBook-Air /tmp % pytest pytest_importlib_issue --doctest-modules
============================================================================================================ test session starts =============================================================================================================
platform darwin -- Python 3.9.10, pytest-7.1.3, pluggy-0.13.1
rootdir: /private/tmp
plugins: anyio-3.5.0, xdist-2.2.1, easyread-0.1.0, forked-1.3.0
collected 2 items
pytest_importlib_issue/temp.py . [ 50%]
pytest_importlib_issue/tests/test_temp.py . [100%]
============================================================================================================= 2 passed in 0.03s ==============================================================================================================
pytest-7.1.3 with importlib
(pytest7.1.3) akhileshr@Akhileshs-MacBook-Air /tmp % pytest pytest_importlib_issue --import-mode=importlib --doctest-modules
============================================================================================================ test session starts =============================================================================================================
platform darwin -- Python 3.9.10, pytest-7.1.3, pluggy-0.13.1
rootdir: /private/tmp
plugins: anyio-3.5.0, xdist-2.2.1, easyread-0.1.0, forked-1.3.0
collected 2 items
pytest_importlib_issue/temp.py . [ 50%]
pytest_importlib_issue/tests/test_temp.py F [100%]
================================================================================================================== FAILURES ==================================================================================================================
___________________________________________________________________________________________________________________ test_x ___________________________________________________________________________________________________________________
def test_x():
assert temp.x() == 2
> assert pytest_importlib_issue.temp.x() == 2
E AttributeError: module 'pytest_importlib_issue' has no attribute 'temp'
pytest_importlib_issue/tests/test_temp.py:6: AttributeError
========================================================================================================== short test summary info ===========================================================================================================
FAILED pytest_importlib_issue/tests/test_temp.py::test_x - AttributeError: module 'pytest_importlib_issue' has no attribute 'temp'
======================================================================================================== 1 failed, 1 passed in 0.08s =========================================================================================================
It could be seen that the failure happens when we try to access the child module through parent module.
pytest-7.0.1 with importlib
(pytest7.0.1) akhileshr@Akhileshs-MacBook-Air /tmp % pytest pytest_importlib_issue --import-mode=importlib --doctest-modules
============================================================================================================ test session starts =============================================================================================================
platform darwin -- Python 3.9.10, pytest-7.0.1, pluggy-0.13.1
rootdir: /private/tmp
plugins: anyio-3.5.0, xdist-2.2.1, easyread-0.1.0, forked-1.3.0
collected 2 items
pytest_importlib_issue/temp.py . [ 50%]
pytest_importlib_issue/tests/test_temp.py . [100%]
============================================================================================================= 2 passed in 0.03s ==============================================================================================================
This passes, because doctest-modules were using prepend
instead of importlib
before the PR. ( added this to state this is a recent issue ).
Fix: Child module has to be persisted in Parent module as an attribute similar to how importlib.import_module
does.