Skip to content

Commit b5eadf7

Browse files
committed
gh-149167: PyREPL autocomplete imports to only display public members
Adds a mechanism that filters a module's public members for showing on autocomplete.
1 parent 04242c0 commit b5eadf7

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/_pyrepl/_module_completer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,10 @@ def _find_attributes(
221221
if not imported_module:
222222
return [], None, self._get_import_completion_action(path)
223223
try:
224-
module_attributes = dir(imported_module)
224+
if hasattr(imported_module, '__all__'): # Use __all__ if available, otherwise use dir()
225+
module_attributes = imported_module.__all__
226+
else:
227+
module_attributes = dir(imported_module)
225228
except Exception:
226229
module_attributes = []
227230
# Filter out invalid attribute names, such as dashes that cannot be

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import importlib
33
import io
44
import itertools
5+
lazy import json
56
import os
67
import pathlib
78
import pkgutil
@@ -1767,6 +1768,23 @@ def test_colorize_import_completions(self) -> None:
17671768
])
17681769
self.assertIsNone(action)
17691770

1771+
def test_find_attributes_uses_all(self):
1772+
"""Test that _find_attributes respects __all__ when available."""
1773+
completer = ModuleCompleter()
1774+
1775+
# json module has __all__ defined
1776+
attrs, module, _ = completer._find_attributes('json', '')
1777+
1778+
# Should match __all__ contents, not dir() which includes methods
1779+
expected = sorted(json.__all__)
1780+
self.assertEqual(sorted(attrs), expected)
1781+
1782+
# Should NOT contain __all__
1783+
self.assertNotIn('__all__', attrs)
1784+
1785+
# Verify we got the actual module object
1786+
self.assertIs(module, sys.modules.get('json'))
1787+
17701788

17711789
# Audit hook used to check for stdlib modules import side-effects
17721790
# Defined globally to avoid adding one hook per test run (refleak)

0 commit comments

Comments
 (0)