Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions astroid/nodes/_base_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ def do_import_module(self, modname: str | None = None) -> nodes.Module:
# If the module ImportNode is importing is a module with the same name
# as the file that contains the ImportNode we don't want to use the cache
# to make sure we use the import system to get the correct module.
# pylint: disable-next=no-member # pylint doesn't recognize type of mymodule
if mymodule.relative_to_absolute_name(modname, level) == mymodule.name:
if (
modname
# pylint: disable-next=no-member # pylint doesn't recognize type of mymodule
and mymodule.relative_to_absolute_name(modname, level) == mymodule.name
):
use_cache = False
else:
use_cache = True
Expand Down
11 changes: 11 additions & 0 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Uninferable,
arguments,
helpers,
manager,
nodes,
objects,
test_utils,
Expand Down Expand Up @@ -991,6 +992,16 @@ def test_import_as(self) -> None:
self.assertIsInstance(inferred[0], nodes.FunctionDef)
self.assertEqual(inferred[0].name, "exists")

def test_do_import_module_performance(self) -> None:
import_node = extract_node("import importlib")
import_node.modname = ""
import_node.do_import_module()
# calling file_from_module_name() indicates we didn't hit the cache
with unittest.mock.patch.object(
manager.AstroidManager, "file_from_module_name", side_effect=AssertionError
):
import_node.do_import_module()

def _test_const_inferred(self, node: nodes.AssignName, value: float | str) -> None:
inferred = list(node.infer())
self.assertEqual(len(inferred), 1)
Expand Down