Skip to content

Commit

Permalink
Fixed #35402 -- Fixed crash when DatabaseFeatures.django_test_skips r…
Browse files Browse the repository at this point in the history
…eferences a class in another test module

Previously, if a submodule skipped test classes in an adjacent submodule (same
parent module), only the running submodule was imported and an error was thrown
because getattr would not find the submodule of the to-be-skipped class. Updated
cached_import in django/utils/module_loading.py to include a check for the skipped
submodule, and import it if it is not there.

Thanks to Tim Graham for reporting this bug
  • Loading branch information
jonmcfee03 committed Apr 26, 2024
1 parent 195d885 commit a2e3eed
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
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
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):
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
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

0 comments on commit a2e3eed

Please sign in to comment.