[Django] #36956: import_string logic for AttributeError is too broad, can be improved under Python 3.10+

13 views
Skip to first unread message

Django

unread,
Feb 26, 2026, 1:18:29 PM (8 days ago) Feb 26
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
-------------------------------------+-------------------------------------
Reporter: Glenn Matthews | Type:
| Cleanup/optimization
Status: new | Component: Utilities
Version: 6.0 | Severity: Normal
Keywords: import_string | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
The logic in `import_string` that maps `AttributeError` to `ImportError`
is overly-broad and can be misleading, as ''any'' AttributeError raised
while importing the requested module will cause Django to report (perhaps
misleadingly) that the ''specific'' attribute being requested is not
defined:

{{{
try:
return cached_import(module_path, class_name)
except AttributeError as err:
raise ImportError(
'Module "%s" does not define a "%s" attribute/class'
% (module_path, class_name)
) from err
}}}

In Python 3.10 and later, `AttributeError` has been enhanced
(https://docs.python.org/3/library/exceptions.html#AttributeError) to
include explicit `name` and `obj` parameters, making it easy to tell
exactly which specific attribute the error is attributed to.

My proposal is that the above logic should be enhanced to something like
the following:

{{{
try:
return cached_import(module_path, class_name)
except AttributeError as err:
if err.name and err.name != class_name:
raise
raise ImportError(
'Module "%s" does not define a "%s" attribute/class'
% (module_path, class_name)
) from err
}}}

If accepted, I'll be happy to open a PR as described.
--
Ticket URL: <https://code.djangoproject.com/ticket/36956>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 27, 2026, 7:46:48 AM (8 days ago) Feb 27
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
--------------------------------------+------------------------------------
Reporter: Glenn Matthews | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 6.0
Severity: Normal | Resolution:
Keywords: import_string | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Jacob Walls):

* stage: Unreviewed => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36956#comment:1>

Django

unread,
Feb 28, 2026, 12:00:38 AM (7 days ago) Feb 28
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
--------------------------------------+------------------------------------
Reporter: Glenn Matthews | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 6.0
Severity: Normal | Resolution:
Keywords: import_string | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Comment (by Sriniketh99):

Hi! I’d like to work on this ticket.

I’m currently learning more about Django internals, and this seems like a
good opportunity to understand import_string better. I’ll begin by
reviewing the current implementation and reproducing the behavior under
Python 3.10+, then work on refining the AttributeError handling as
proposed and submit a PR.

Please let me know if there are any additional considerations I should
keep in mind.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/36956#comment:2>

Django

unread,
Feb 28, 2026, 9:55:27 AM (7 days ago) Feb 28
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
-------------------------------------+-------------------------------------
Reporter: Glenn Matthews | Owner: Glenn
Type: | Matthews
Cleanup/optimization | Status: assigned
Component: Utilities | Version: 6.0
Severity: Normal | Resolution:
Keywords: import_string | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: (none) => Glenn Matthews
* status: new => assigned

Comment:

Sriniketh99, Glenn (reporter) already proposed a patch and was interested
in preparing a patch.
--
Ticket URL: <https://code.djangoproject.com/ticket/36956#comment:3>

Django

unread,
Mar 1, 2026, 9:04:04 AM (6 days ago) Mar 1
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
-------------------------------------+-------------------------------------
Reporter: Glenn Matthews | Owner: Glenn
Type: | Matthews
Cleanup/optimization | Status: assigned
Component: Utilities | Version: 6.0
Severity: Normal | Resolution:
Keywords: import_string | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by ANUSHREE-123-S):

I would like to work on this ticket. I plan to implement the proposed
logic improvement for Python 3.10+ and add appropriate tests. Please let
me know if there are any additional considerations.
--
Ticket URL: <https://code.djangoproject.com/ticket/36956#comment:4>

Django

unread,
Mar 1, 2026, 10:29:03 AM (6 days ago) Mar 1
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
-------------------------------------+-------------------------------------
Reporter: Glenn Matthews | Owner: Glenn
Type: | Matthews
Cleanup/optimization | Status: assigned
Component: Utilities | Version: 6.0
Severity: Normal | Resolution:
Keywords: import_string | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by ANUSHREE):

PR opened: https://github.com/django/django/pull/20809
--
Ticket URL: <https://code.djangoproject.com/ticket/36956#comment:5>

Django

unread,
Mar 2, 2026, 5:10:59 PM (4 days ago) Mar 2
to django-...@googlegroups.com
#36956: import_string logic for AttributeError is too broad, can be improved under
Python 3.10+
-------------------------------------+-------------------------------------
Reporter: Glenn Matthews | Owner: Glenn
Type: | Matthews
Cleanup/optimization | Status: assigned
Component: Utilities | Version: 6.0
Severity: Normal | Resolution:
Keywords: import_string | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Glenn Matthews):

Have a test and fix at
https://github.com/django/django/compare/main...glennmatthews:django:ticket_36956.
Working on getting the CLA done before opening a pull request.
--
Ticket URL: <https://code.djangoproject.com/ticket/36956#comment:6>
Reply all
Reply to author
Forward
0 new messages