pytest-dev / pytest-dev/pytest
Presence of empty `__init__.py` breaks test discovery in hard-to-diagnose ways
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
Hey all
I'm trying to add another package to conda-forge, and ran into collection problems when attempting to run the upstream test suite:
==================================== ERRORS ====================================
______ ERROR collecting tensorflow_addons/callbacks/tests/run_all_test.py ______
import file mismatch:
imported module 'run_all_test' has this __file__ attribute:
$SRC_DIR/tensorflow_addons/activations/tests/run_all_test.py
which is not the same as the test file we want to collect:
$SRC_DIR/tensorflow_addons/callbacks/tests/run_all_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
The first thing that caught my eye were the weird kind of test wrappers that tf-addons is using (my guess from searching their repo is that this is for bazel compatibility, and hence not so relevant due to the relative rarity of that in python; edit: beyond the convoluted definition, the underlying issue for these is #9301), but removing those still left me with similarly cryptic errors:
==================================== ERRORS ====================================
_ ERROR collecting tensorflow_addons/callbacks/tests/avg_model_checkpoint_test.py _
ImportError while importing test module '$SRC_DIR/tensorflow_addons/callbacks/tests/avg_model_checkpoint_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../[...]/lib/python3.9/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
E ModuleNotFoundError: No module named 'tests.avg_model_checkpoint_test'
I then got curious about No module named 'tests.avg_model_checkpoint_test', especially because the upstream test modules (an example) looked alright, and that module that was supposedly not being found didn't appeared anywhere.
By chance, I noticed that the test folders contain empty __init__.py files, and due to some dim awareness of the issues around imports, I decided on a whim to see what happens if I remove them - turns out collection succeeds then! 🥳
From looking around the issue tracker (and said awareness of import troubles in python), I believe this issue is probably quite thorny, so the underlying problem is probably captured in other issues already: #3396 #5147 #8332
However, I'm hoping that a possible fix for empty __init__.py might be easier than that, and at the very least, I think the warning should be improved, because it's really not obvious what's causing the problem.
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
Reproduce collection failures with empty init.py files in the test folders, using the tensorflow-addons test paths and errors described here. Compare collection after removing those files, then inspect pytest's test discovery and import handling; done means the failure is fixed or the warning clearly identifies the cause.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100