Opened 9 minutes ago

#37504 new Bug

Avoid circular import when importing django.db.backends.sqlite3.features directly

Reported by: alexisromtz-lab Owned by:
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: sqlite3 circular-import
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Importing django.db.backends.sqlite3.features directly before django.db.backends.sqlite3.base raises an ImportError due to a circular import on main (tested on CPython 3.12.13 with asgiref installed):

$ python -c "import django.db.backends.sqlite3.features"
ImportError: cannot import name 'DatabaseFeatures' from partially initialized module 'django.db.backends.sqlite3.features' (most likely due to a circular import)

Root cause

  • django/db/backends/sqlite3/features.py (line 10) executes from .base import Database.
  • django/db/backends/sqlite3/base.py imports DatabaseFeatures from .features and already aliases from sqlite3 import dbapi2 as Database (line 11).

Normal startup via django/db/utils.py hides the cycle because .base is loaded first, whereas modules that import sqlite3.features directly (such as django/contrib/gis/db/backends/spatialite/features.py) hit the circular import.

Proposed fix

In django/db/backends/sqlite3/features.py, import Database directly from sqlite3 instead of .base:

from sqlite3 import dbapi2 as Database

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top