﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36956	import_string logic for AttributeError is too broad, can be improved under Python 3.10+	Glenn Matthews	Glenn Matthews	"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."	Cleanup/optimization	assigned	Utilities	6.0	Normal		import_string		Accepted	0	0	0	0	0	0
