diff --git a/Lib/_pyrepl/fancycompleter.py b/Lib/_pyrepl/fancycompleter.py index 9b779d79a7ffcb4..6bc49f774f27b6f 100644 --- a/Lib/_pyrepl/fancycompleter.py +++ b/Lib/_pyrepl/fancycompleter.py @@ -4,6 +4,7 @@ # All Rights Reserved """Colorful tab completion for Python prompt""" from _colorize import ANSIColors, get_colors, get_theme +import builtins import rlcompleter import keyword import types @@ -112,10 +113,8 @@ def global_matches(self, text): if keyword.iskeyword(clean_name) or keyword.issoftkeyword(clean_name): values.append(None) else: - try: - values.append(eval(name, self.namespace)) - except Exception: - values.append(None) + values.append(self.namespace.get( + clean_name, builtins.__dict__.get(clean_name))) if self.use_colors and names: return self.colorize_matches(names, values) return names diff --git a/Lib/test/test_pyrepl/test_fancycompleter.py b/Lib/test/test_pyrepl/test_fancycompleter.py index 0ffc1ed97b557ae..e648c2ff2a1ad41 100644 --- a/Lib/test/test_pyrepl/test_fancycompleter.py +++ b/Lib/test/test_pyrepl/test_fancycompleter.py @@ -142,6 +142,13 @@ def test_module_attributes_do_not_reify_lazy_imports(self): self.assertEqual(compl.attr_matches(f"{name}.j"), [f"{name}.json"]) self.assertIs(type(module.__dict__["json"]), types.LazyImportType) + def test_globals_do_not_reify_lazy_imports(self): + namespace = {} + exec("lazy import json\nlazy import jsonz_missing\n", namespace) + compl = Completer(namespace, use_colors=True) + self.assertEqual(len(compl.global_matches("json")), 2) + self.assertIs(type(namespace["json"]), types.LazyImportType) + def test_complete_colored_single_match(self): """No coloring, via commonprefix.""" compl = Completer({'foobar': 42}, use_colors=True) diff --git a/Misc/NEWS.d/next/Library/2026-10-12-21-30-26.gh-issue-159266.YNpnaG.rst b/Misc/NEWS.d/next/Library/2026-10-12-21-30-26.gh-issue-159266.YNpnaG.rst new file mode 100644 index 000000000000000..0bbcbb6d40d8955 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-12-21-30-26.gh-issue-159266.YNpnaG.rst @@ -0,0 +1,2 @@ +Tab completion of global names in the :term:`REPL` no longer imports +lazily imported modules.