Repository: chrahunt/hlp

Issue:
Autocomplete builtin names
Currently if autocompleting a prompt like

```
hlp |
```

we only look at top-level packages and modules. We should also be including names of builtins.

Discussion:

Patch:
diff --git a/hlp/cli.py b/hlp/cli.py
index c3d2476..33ff11c 100644
--- a/hlp/cli.py
+++ b/hlp/cli.py
@@ -6,7 +6,7 @@ import shlex
 import sys
 from textwrap import dedent
 
-from .compat import iter_modules
+from .compat import builtins, iter_modules
 from .typing import MYPY_RUNNING
 from .util import getattr_recursive, sequences
 
@@ -23,7 +23,9 @@ class NamedObject(object):
     def get_attr(self, name):
         # type: (str) -> NamedObject
         obj = getattr(self.obj, name)
-        return NamedObject("{}.{}".format(self.name, name), obj)
+        if self.name:
+            name = "{}.{}".format(self.name, name)
+        return NamedObject(name, obj)
 
 
 def import_module(spec):
@@ -167,7 +169,9 @@ def autocomplete(current):
             return [context.name]
         options = children_autocomplete_names(context)
     else:
-        options = package_autocomplete_names()
+        options = list(package_autocomplete_names())
+        # Include builtins
+        options.extend(name for name in dir(builtins) if not name.startswith("__"))
 
     return sorted(m for m in options if m.startswith(current))
 
diff --git a/hlp/compat.py b/hlp/compat.py
index 4d0e3b0..f8d0175 100644
--- a/hlp/compat.py
+++ b/hlp/compat.py
@@ -9,6 +9,11 @@ if MYPY_RUNNING:
 
 PY2 = sys.version_info < (3,)
 
+if PY2:
+    import __builtin__ as builtins  # type: ignore
+else:
+    import builtins as builtins  # type: ignore
+
 
 if PY2:
 


Test patch:
diff --git a/tests/test_autocomplete.py b/tests/test_autocomplete.py
index 8cadcc3..7e53f9f 100644
--- a/tests/test_autocomplete.py
+++ b/tests/test_autocomplete.py
@@ -65,7 +65,15 @@ def test_autocomplete_bash_empty(fake_packages, monkeypatch):
     monkeypatch.setenv("COMP_WORDS", "hlp")
     monkeypatch.setenv("COMP_CWORD", "1")
     result = autocomplete_bash()
+    # Builtins
+    assert "open" in result
+    assert "ord" in result
+    # Standard library modules
+    assert "string" in result
+    assert "multiprocessing" in result
+    # Our packages
     assert "pkg_aaa" in result
+    # Only name, no trailing '.'
     assert "pkg_aaa." not in result