Skip to content

Fix --help/--version when conftest imports models#1275

Open
brianhelba wants to merge 2 commits intopytest-dev:mainfrom
brianhelba:help-conftest
Open

Fix --help/--version when conftest imports models#1275
brianhelba wants to merge 2 commits intopytest-dev:mainfrom
brianhelba:help-conftest

Conversation

@brianhelba
Copy link

Previously, pytest_load_initial_conftests returned early when --help or --version was passed (added by #238), skipping django.setup() entirely. This caused conftest files with top-level Django model imports to fail with AppRegistryNotReady.

Instead of returning early, continue with normal Django initialization, allowing --help/--version to return properly when everything is valid.

If Django is somehow invalid (e.g. incorrect DJANGO_SETTINGS_MODULE), add a guard to still return gracefully if --help/--version is requested. Running real tests would still fail, but these basic informational arguments shouldn't be blocked.

Fixes #1152

@brianhelba
Copy link
Author

@kingbuzzman I was able to fix this myself (and add a test to reproduce the failure). Could you please take a look?

brianhelba added a commit to kitware-resonant/django-auth-style that referenced this pull request Mar 7, 2026
brianhelba added a commit to kitware-resonant/django-auth-style that referenced this pull request Mar 9, 2026
brianhelba added a commit to kitware-resonant/django-auth-style that referenced this pull request Mar 9, 2026
Copy link
Member

@kingbuzzman kingbuzzman left a comment

Choose a reason for hiding this comment

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

This is not a bad addition, please update the changelog.md

Previously, `pytest_load_initial_conftests` returned early when `--help` or
`--version` was passed (added by pytest-dev#238), skipping `django.setup()` entirely.
This caused conftest files with top-level Django model imports to fail
with `AppRegistryNotReady`.

Instead of returning early, continue with normal Django initialization, allowing
`--help`/`--version` to return properly when everything is valid.

If Django is somehow invalid (e.g. incorrect `DJANGO_SETTINGS_MODULE`),
add a guard to still return gracefully if `--help`/`--version` is requested.
Running real tests would still fail, but these basic informational arguments
shouldn't be blocked.

Fixes pytest-dev#1152
@brianhelba
Copy link
Author

@kingbuzzman Updated changelog.md. Ready for final review.

@brianhelba brianhelba requested a review from kingbuzzman March 9, 2026 15:59
^^^^^^^^

* Fixed type hints of assert methods to match actual signature (`PR #1271 <https://github.com/pytest-dev/pytest-django/pull/1271>`__)
* Fix Pytest `--help`/`--version` options when a `conftest.py` file imports Django models (`PR #1275 <https://github.com/pytest-dev/pytest-django/pull/1275>`__).
Copy link
Member

Choose a reason for hiding this comment

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

I thought the issue was when your apps we'rent ready, and you didn't care because you were doing a --help

Copy link
Author

Choose a reason for hiding this comment

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

Right now, if you have any kind of top-level model import in confest.py, running a --help will error out, which what this PR fixes.

Copy link
Author

Choose a reason for hiding this comment

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

I'm open to feedback on how to better describe this.

The PR is essentially a fix to --help and --version. The models are just the trigger for the previous failure.

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.

pytest.PytestConfigWarning: could not load initial conftests with --help or -V but not when running tests

2 participants