Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #35402 -- Fixed crash when DatabaseFeatures.django_test_skips references a class in another test module #18103

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ answer newbie questions, and generally made Django that much better:
Jonathan Buchanan <jonathan.buchanan@gmail.com>
Jonathan Daugherty (cygnus) <http://www.cprogrammer.org/>
Jonathan Feignberg <jdf@pobox.com>
Jonathan McFee <jmcfee@umich.edu>
Jonathan Slenders
Jonny Park <jonnythebard9@gmail.com>
Jordan Bae <qoentlr37@gmail.com>
Expand Down
5 changes: 5 additions & 0 deletions django/utils/module_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def cached_import(module_path, class_name):
and getattr(spec, "_initializing", False) is False
):
module = import_module(module_path)
if not hasattr(module, class_name):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class_name allowed to also be a submodule name is confusing -- since cached_import() doesn't appear to be documented, I think we can take the opportunity to rename to something like submodule_or_class. Not sure of the best name.

An inline comment here would also be helpful IMO. Otherwise it's not clear that getattr'ing a submodule off the parent module only sometimes works (i.e. when submodule already explicitly imported).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could also enhance it to take top-level modules and just skip the getattr call. Then it becomes module_or_class. Or maybe "attribute" is enough?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, with the intended functionality, the names are quite confusing. As this is my first PR, I am not too familiar with Django naming conventions. I think “attribute” would suffice, as that encapsulates all importable items.

try:
import_module(f"{module_path}.{class_name}")
except ImportError:
pass
return getattr(module, class_name)


Expand Down
4 changes: 4 additions & 0 deletions tests/utils_tests/test_module_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def test_import_string(self):
cls = import_string("django.utils.module_loading.import_string")
self.assertEqual(cls, import_string)

import_string("utils_tests.test_module.main_module")
good_module = import_string("utils_tests.test_module.good_module")
self.assertEqual(good_module.content, "Good Module")

# Test exceptions raised
with self.assertRaises(ImportError):
import_string("no_dots_in_path")
Expand Down