Repository navigation
Closed
Labels
topic: collectionrelated to the collection phase
Description
Likely related to #12112, discovered in jaraco/keyring#676, a new regression appears in pytest 8.1 relating to collection. Consider this minimal example:
draft @ cat > test_something.py
import foo
from foo.bar import baz
foo.bar.baz
draft @ mkdir -p foo/bar/baz
draft @ touch foo/__init__.py
draft @ touch foo/bar/__init__.py
draft @ touch foo/bar/baz/__init__.py
draft @ pip-run pytest -- -m pytest --import-mode importlib --doctest-modules
============================================================== test session starts ===============================================================
platform darwin -- Python 3.12.2, pytest-8.1.1, pluggy-1.4.0
rootdir: /Users/jaraco/draft
collected 0 items / 1 error
===================================================================== ERRORS =====================================================================
_______________________________________________________ ERROR collecting test_something.py _______________________________________________________
test_something.py:3: in <module>
foo.bar.baz
E AttributeError: module 'foo' has no attribute 'bar'
============================================================ short test summary info =============================================================
ERROR test_something.py - AttributeError: module 'foo' has no attribute 'bar'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================ 1 error in 0.03s ================================================================
It seems that once the foo/bar/__init__.py has been collected for doctesting, the collection of the tests in test_something will start to fail when foo has no attribute bar. Any of the following tweaks will bypass the failure:
- disable doctests
- pin to pytest<8.1
- use a different
import-mode
Something about the pytest discovery and import machinery is breaking the Python convention that importing a submodule causes the submodule to be added as an attribute of the parent module.
Metadata
Metadata
Assignees
Labels
topic: collectionrelated to the collection phase
Activity
jaraco commentedon Apr 8, 2024
It occurred to me that the fix in #12169 might also address this issue, but it does not:
nicoddemus commentedon Apr 8, 2024
Thanks @jaraco, I can reproduce the issue (also verified that #12169 does not fix this).
Will investigate which incantation is necessary for this to work.
Currently we import modules in
--importmode=importlibusing this snippet:pytest/src/_pytest/pathlib.py
Lines 635 to 641 in 74f0564
flying-sheep commentedon Apr 9, 2024
I found an issue that might be related. If not, please direct me in the right direction.
My problem so far is that I haven’t figured out a minimal reproducer yet: Somehow the
_frozen_importlib_external.PathFinderfinds the a namespace module for our real life example but not in the test suite.Minimal (not yet) reproducer:
Now running a pytest test that imports
foo.barleads to this traceback (simplified, oldest to newest)where
meta_importeris a_frozen_importlib_external.PathFinderinstance, and the module created from the spec returned by the last expression isinstead of
The documentation of
MetaPathFinder.find_specsays:and since
_import_module_using_specsays it “imports a module by its canonical name”, that meanspathshould be none and you should callright?
jaraco commentedon Apr 10, 2024
Thanks for the tip. I did find that adding this patch works around the issue (at least in the repro):
I wonder if there's similar logic in CPython's importlib logic that should be modeled.
jaraco commentedon Apr 10, 2024
Here's where that happens in CPython. Doesn't seem to be particularly re-usable, but at least shows how that behavior is implemented there.
flying-sheep commentedon Apr 11, 2024
My issue is fixed with #12169, but I found that the change in #12207 avoids the wrong loader altogether.
@jaraco does that one fix your problem?
importlib: set module as attribute in its parent module
jaraco commentedon Apr 13, 2024
Just checked and it does not.
Pin against pytest 8.1.x due to pytest-dev/pytest#12194.
importlib: set module as attribute in its parent module
importlib: set children as attribute of parent modules (#12208)
pypi-jaraco.functools: Autospec creation for update from version 4.0.…
pypi-inflect: Autospec creation for update from version 7.2.0 to vers…
pypi-keyring: Autospec creation for update from version 25.1.0 to ver…
pypi-zipp: Autospec creation for update from version 3.18.1 to versio…
pypi-jaraco.text: Autospec creation for update from version 3.12.0 to…
pypi-tempora: Autospec creation for update from version 5.5.1 to vers…
pypi-setuptools: Autospec creation for update from version 70.1.1 to …
pypi-importlib_metadata: Autospec creation for update from version 7.…
pypi-configparser: Autospec creation for update from version 7.0.0 to…
pypi-jaraco.context: Autospec creation for update from version 5.3.0 …
pypi-jaraco.collections: Autospec creation for update from version 5.…
Pin against pytest 8.1.x due to pytest-dev/pytest#12194.