Opened 3 weeks ago

Closed 3 weeks ago

Last modified 3 weeks ago

#37504 closed Bug (needsinfo)

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

Reported by: Alexis Román Owned by: Alexis Román
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: sqlite3 circular-import
Cc: Triage Stage: Unreviewed
Has patch: yes 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 (7)

comment:1 by S Sidharth, 3 weeks ago

Hi,

I'm interested in working on this issue as my first Django code contribution.

I would like to reproduce the circular import issue, investigate the proposed fix, and add a regression test.

Could a triager please review and accept this ticket if the issue is valid? I'd be happy to work on the implementation once it's accepted.

Thanks!

comment:2 by Alexis Román, 3 weeks ago

Owner: set to Alexis Román
Status: new → assigned

comment:3 by Tim Graham, 3 weeks ago

How did you run into the error? In other words, what is your use case?

This pattern is used elsewhere, and it's not obvious to me that changing it is really needed.

oracle/operations.py:from .base import Database
oracle/utils.py:from .base import Database
sqlite3/operations.py:from .base import Database
sqlite3/features.py:from .base import Database

comment:4 by Alexis Román, 3 weeks ago

Has patch: set

comment:5 by Simon Charette, 3 weeks ago

Resolution: → fixed
Status: assigned → closed

I'm also curious of the use case here as Tim requested feedback about.

It's not sustainable for the project to ensure (and test) that any of it's internal modules are importable directly before others as the combinatory space is too large.

Unless you have a clear use case in mind this seems like a wontfix to me.

comment:6 by Simon Charette, 3 weeks ago

Resolution: fixed → needsinfo

in reply to:  6 comment:7 by Alexis Román, 3 weeks ago

Replying to Simon Charette:
Thanks Tim and Simon — you're both right, and I don't have the use case.

I hit it while checking the import order of the backend modules, not from a
project of my own. So the honest answer is: there isn't one. I went looking
for a real consumer before replying, and the closest one I found
(cloudflare/workers-py, django_cf/db/base_engine.py) imports .base before
.features, so it never reaches the cycle.

Simon's point settles it for me: guaranteeing that internal modules are
importable in any order is a combinatorial space nobody can test, and
features.py follows the same pattern as oracle/operations.py, oracle/utils.py
and sqlite3/operations.py — my patch singled out one of four without a reason to.

Please close as wontfix. Thanks for the review.

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