Skip to content

Comments

swev-id: pytest-dev__pytest-6197 Fix: avoid importing non-test __init__.py during collection; restore 5.2.2 behavior#25

Open
casey-brooks wants to merge 1 commit intopytest-dev__pytest-6197from
work-pytest-6197-fix
Open

swev-id: pytest-dev__pytest-6197 Fix: avoid importing non-test __init__.py during collection; restore 5.2.2 behavior#25
casey-brooks wants to merge 1 commit intopytest-dev__pytest-6197from
work-pytest-6197-fix

Conversation

@casey-brooks
Copy link

Summary

  • prevent package collectors from importing __init__.py unless the file matches python_files or was explicitly requested
  • keep package-level setup/teardown hooks guarded behind the same check to avoid importing non-test packages during setup
  • add regression coverage for default, python_files opt-in, src/ layout, and namespace packages

Testing

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest testing/python/collect.py -k "package_init_not_imported_when_not_a_test_module or package_init_collected_when_enabled_via_python_files or src_layout_package_init_not_imported or namespace_package_without_init_collects_normally" -q
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest testing/test_collection.py -q

Fixes #19

Root cause

Package.collect unconditionally imported the package via _mount_obj_if_needed(), which executes __init__.py even when the file is not considered a test module. This regressed after the package-scoped fixture work in 5.2.3.

Fix details

  • introduce _should_import_init() to decide when __init__.py should be mounted/collected (matches python_files or the file was explicitly targeted)
  • guard both collect() and setup() behind this check so that boundary-level hooks remain intact without importing non-test packages prematurely
  • adjust collection tests to reflect the explicit-selection behavior and cover the regression scenarios (default layout, python_files opt-in, src layout, namespace package)

Reproduction steps and observed failure

  1. Create pkg/__init__.py with assert False and place unrelated tests elsewhere (e.g. tests/test_ok.py).
  2. Run pytest from the project root under 5.2.3.
  3. Collection fails with AssertionError while importing pkg/__init__.py even though it is not a test module.

Compatibility

  • python_files opt-in still collects package __init__.py containing tests.
  • namespace packages (no __init__.py) remain unaffected.
  • src/ layouts avoid importing application packages during discovery.
  • explicit selection (pytest pkg/__init__.py) continues to work via the initialpaths guard.

@casey-brooks casey-brooks requested a review from a team December 25, 2025 20:51
@casey-brooks
Copy link
Author

Local Verification

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest testing/python/collect.py -k "package_init_not_imported_when_not_a_test_module or package_init_collected_when_enabled_via_python_files or src_layout_package_init_not_imported or namespace_package_without_init_collects_normally" -q
    • Result: 4 passed (74 deselected)
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 .venv/bin/pytest testing/test_collection.py -q
    • Result: 71 passed, 1 xfailed
  • .venv/bin/flake8 src/_pytest/python.py testing/python/collect.py testing/test_collection.py
    • Result: no issues

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good: guarding package init import behind python_files/explicit selection restores expected behavior, and regression tests cover the updated scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants