Fix admin import without django-stubs runtime monkeypatch#113
Merged
pfouque merged 2 commits intodjango-commons:mainfrom Mar 9, 2026
Merged
Conversation
Keep the Django generic typing in django_fsm.admin under TYPE_CHECKING so the module no longer evaluates ModelForm[...] or ModelAdmin[...] at runtime. This avoids import-time failures in downstream projects that do not call django_stubs_ext.monkeypatch() before Django admin autodiscovery. Add a regression test that imports django_fsm.admin in a fresh subprocess with only minimal Django settings configured, proving the admin module loads without relying on the test suite's django-stubs monkeypatch. Also document the fix under Unreleased in the changelog.
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
Member
|
Thanks for the great fix and the regression test — nice catch. 👍 I only moved the test into a dedicated file for consistency with the rest of the suite. I'll ship this in today's release. ⛵ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix import-time admin crash when Django generic types are not runtime-subscriptable
Description
This PR fixes an import-time crash in
django_fsm.adminintroduced by the 4.2.0 typing/admin integration changes.At module import time,
django_fsm.adminevaluated typed Django generics such asModelForm[...]andModelAdmin[...]. In downstream projects that do not calldjango_stubs_ext.monkeypatch()before Django admin autodiscovery, those classes are not runtime-subscriptable, which raises:This change keeps the precise generic typing under
typing.TYPE_CHECKINGand uses unsubscripted runtime fallbacks instead. That preserves static typing for type checkers while allowingdjango_fsm.adminto import cleanly without requiring a runtime monkeypatch in consumer projects.This PR also adds a regression test that imports
django_fsm.adminin a fresh subprocess with only minimal Django settings configured. The test verifies the module can be imported without inheriting the test suite'sdjango_stubs_ext.monkeypatch()setup.Type of Change
Checklist
uv run pytestorpytest)uv run ruff check .)Testing
Ran the full test suite and lint locally.
Additionally, the new regression coverage is in:
tests/testapp/tests/test_admin.py::test_admin_module_imports_without_django_stubs_monkeypatchThis test spawns a fresh Python process, configures minimal Django settings, and verifies
import django_fsm.adminsucceeds without relying on the test suite'sdjango_stubs_extmonkeypatch.Related Issues
Related to the import-time runtime typing regression in
django_fsm.adminintroduced in 4.2.0 admin integration.