Repository navigation
AttributeError: Test modules cannot use parent module's attributes under --import-mode=importlib #10337
Description
Activity
Have the required fix lined up in #10338. Please let me know if it looks good or I am missing something.
Thanks!
- addedtype: bugproblem that needs to be addressedproblem that needs to be addressedtopic: collectionrelated to the collection phaserelated to the collection phase
on Oct 16, 2022 any thoughts on this ? @nicoddemus / @RonnyPfannschmidt / @bluetech
This issue also affects pmxbot.
I created an even smaller repro with one namespace package, one submodule, and one test:
draft @ tree . ├── pkg │ └── mod.py └── tests └── test_something.py 3 directories, 2 files draft @ cat pkg/mod.py draft @ cat tests/test_something.py import pkg.mod def test_mod(): pkg.moddraft @ pip-run pytest -- -m pytest --doctest-modules --import-mode importlib ========================================================================= test session starts ========================================================================== platform darwin -- Python 3.11.4, pytest-7.4.0, pluggy-1.2.0 rootdir: /Users/jaraco/draft plugins: anyio-3.6.2 collected 1 item tests/test_something.py F [100%] =============================================================================== FAILURES =============================================================================== _______________________________________________________________________________ test_mod _______________________________________________________________________________ def test_mod(): > pkg.mod E AttributeError: module 'pkg' has no attribute 'mod' tests/test_something.py:5: AttributeError ======================================================================= short test summary info ======================================================================== FAILED tests/test_something.py::test_mod - AttributeError: module 'pkg' has no attribute 'mod' ========================================================================== 1 failed in 0.02s ===========================================================================Remove
--doctest-modulesor--import-mode importlibor movetests/test_something.pyto.and the test passes.Also confirmed that the bugfix proposed in #10338 fixes the issue:
draft @ pip-run 'pytest@git+https://github.com/akhilramkee/pytest@importlib-parent' -- -m pytest --import-mode importlib --doctest-modules ========================================================================= test session starts ========================================================================== platform darwin -- Python 3.11.4, pytest-7.2.0.dev265+g9118b95f, pluggy-1.2.0 rootdir: /Users/jaraco/draft plugins: anyio-3.6.2 collected 1 item tests/test_something.py . [100%] ========================================================================== 1 passed in 0.01s ===========================================================================I've also confirmed that with this fix, pmxbot is able to adopt native namespace packages without the test suite failing (pmxbot/pmxbot#110).
Thanks Akhil for the report and fix.
Reacted by Akhilesh Ramakrishnanany thoughts on this ? @nicoddemus / @RonnyPfannschmidt / @bluetech
Really sorry, I seem to have missed this.
Yeah looks great, it is only missing a changelog entry which I will push up shortly.
Would you like to have your name added to AUTHORS as well @akhilramkee ?
- added a commit that references this issue
on Jul 1, 2023 Yeah looks great, it is only missing a changelog entry which I will push up shortly.
Thanks @nicoddemus for taking care of the missing changelog.
Would you like to have your name added to AUTHORS as well @akhilramkee ?
Sure, I would like that. I can add myself to the AUTHORS in a separate PR if that works.
Reacted by Bruno Oliveira- added a commit that references this issue
on Dec 2, 2023 - added 2 commits that reference this issue
on Apr 18, 2026
pip listfrom the virtual environment you are usingDescription of the bug:
Pytest when executed under
--import-mode=importlibdoesn't try to retain the child module as parent module's attribute if parent module is already insys.path. This started happening in pytest 7.1.3 after enabling doctest to respectimport-modein #10088.Minimal Example
File structure
temp.py
test_temp.py
Output - MacOS 12.6
pytest7.1.3 without importlib
pytest-7.1.3 with importlib
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
This passes, because doctest-modules were using
prependinstead ofimportlibbefore 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_moduledoes.