Repository: xonsh/xonsh

Issue:
The xonsh generates an error during the launch
<!--- Provide a general summary of the issue in the Title above -->
<!--- If you have a question along the lines of "How do I do this Bash command in xonsh"
please first look over the Bash to Xonsh translation guide: http://xon.sh/bash_to_xsh.html
If you don't find an answer there, please do open an issue! -->

## xonfig
<!--- Please post the output of the `xonfig` command (run from inside xonsh) so we know more about your current setup -->
```
+------------------+----------------------+
| xonsh            | 0.7.2                |
| Git SHA          | d9651fe8             |
| Commit Date      | Jul 30 10:58:07 2018 |
| Python           | 3.6.6                |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 1.0.15               |
| shell type       | prompt_toolkit1      |
| pygments         | 2.2.0                |
| on posix         | True                 |
| on linux         | True                 |
| distro           | Ubuntu               |
| on darwin        | False                |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
+------------------+----------------------+
```

## Expected Behavior
<!--- Tell us what should happen -->
The xonsh should be launched without any errors or with warnings.

## Current Behavior
<!--- Tell us what happens instead of the expected behavior -->
<!--- If part of your bug report is a traceback, please first enter debug mode before triggering the error
To enter debug mode, set the environment variable `XONSH_DEBUG=1` _before_ starting `xonsh`.  
On Linux and OSX, an easy way to to do this is to run `env XONSH_DEBUG=1 xonsh` -->
The xonsh generates an error during startup.
## Steps to Reproduce
<!--- Please try to write out a minimal reproducible snippet to trigger the bug, it will help us fix it! -->


Input, bash:
```
$ XONSH_SHOW_TRACEBACK=True XONSH_TRACEBACK_LOGFILE=/tmp/xonsh.log xonsh
```
Output:
```
$ cat /tmp/xonsh.log
Skipping application of 'ls' alias from 'bash' since it shares a name with an existing xonsh alias. Use "--overwrite-alias" option to apply it anyway.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 12365, in wait
    r = self.f(self.args, stdin, stdout, stderr, spec)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 11906, in proxy_two
    return f(args, stdin)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 9306, in xontribs_main
    return _MAIN_XONTRIB_ACTIONS[ns.action](ns)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 9223, in _load
    update_context(name, ctx=ctx)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 9202, in update_context
    modctx = xontrib_context(name)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 9169, in xontrib_context
    m = importlib.import_module(spec.name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.6/dist-packages/xontrib/docker_tabcomplete.xsh", line 3, in <module>
    from docker_tabcomplete import queries
  File "/usr/local/lib/python3.6/dist-packages/docker_tabcomplete/queries.py", line 1, in <module>
    from docker import Client
ImportError: cannot import name 'Client'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/xonsh/__amalgam__.py", line 7481, in _call
    handler(**self._kwargs)
  File "/usr/local/lib/python3.6/dist-packages/xontrib/bashisms.py", line 36, in custom_keybindings
    insert_mode)
  File "/usr/local/lib/python3.6/dist-packages/prompt_toolkit/key_binding/registry.py", line 241, in add_binding
    return self._extra_registry.add_binding(*k, **kw)
  File "/usr/local/lib/python3.6/dist-packages/prompt_toolkit/key_binding/registry.py", line 116, in add_binding
    filter = to_cli_filter(kwargs.pop('filter', True))
  File "/usr/local/lib/python3.6/dist-packages/prompt_toolkit/filters/utils.py", line 34, in to_cli_filter
    raise TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)
TypeError: Expecting a bool or a CLIFilter instance. Got Condition(<function custom_keybindings.<locals>.last_command_exists at 0x7f687e2d4378>)&ViInputMode()|EmacsInsertMode()
Exception raised in event handler; ignored.
```


Discussion:
Thanks @alex-bender! Can you please also run this with `$XONSH_DEBUG=1`, so that we get the line numbers too? Thanks a ton!
Also, it looks like this might be some issue with a couple of xontribs and their keybindings.  I am not sure how dockerpy is getting in there...
I'm pretty sure that `docker_tabcomplete.xsh` is the culprit here
And i'll point out that if you have docker installed, `xonsh` will already pick up the docker bash-completions (at least on OSX) so you might not need that xontrib at all
@scopatz, here is output with `$XONSH_DEBUG=1` being set
```
Skipping application of 'ls' alias from 'bash' since it shares a name with an existing xonsh alias. Use "--overwrite-alias" option to apply it anyway.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/xonsh/proc.py", line 1631, in wait
    r = self.f(self.args, stdin, stdout, stderr, spec)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/proc.py", line 1172, in proxy_two
    return f(args, stdin)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/xontribs.py", line 170, in xontribs_main
    return _MAIN_XONTRIB_ACTIONS[ns.action](ns)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/xontribs.py", line 87, in _load
    update_context(name, ctx=ctx)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/xontribs.py", line 66, in update_context
    modctx = xontrib_context(name)
  File "/usr/local/lib/python3.6/dist-packages/xonsh/xontribs.py", line 33, in xontrib_context
    m = importlib.import_module(spec.name)
  File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/usr/local/lib/python3.6/dist-packages/xontrib/docker_tabcomplete.xsh", line 3, in <module>
    from docker_tabcomplete import queries
  File "/usr/local/lib/python3.6/dist-packages/docker_tabcomplete/queries.py", line 1, in <module>
    from docker import Client
ImportError: cannot import name 'Client'
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/xonsh/events.py", line 217, in _call
    handler(**self._kwargs)
  File "/usr/local/lib/python3.6/dist-packages/xontrib/bashisms.py", line 36, in custom_keybindings
    insert_mode)
  File "/usr/local/lib/python3.6/dist-packages/prompt_toolkit/key_binding/registry.py", line 241, in add_binding
    return self._extra_registry.add_binding(*k, **kw)
  File "/usr/local/lib/python3.6/dist-packages/prompt_toolkit/key_binding/registry.py", line 116, in add_binding
    filter = to_cli_filter(kwargs.pop('filter', True))
  File "/usr/local/lib/python3.6/dist-packages/prompt_toolkit/filters/utils.py", line 34, in to_cli_filter
    raise TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)
TypeError: Expecting a bool or a CLIFilter instance. Got Condition(<function custom_keybindings.<locals>.last_command_exists at 0x7f297689a6a8>)&ViInputMode()|EmacsInsertMode()
Exception raised in event handler; ignored.
```
@gforsyth how can I check that it's really `docker_tabcomplete`'s  culprit?
You can try removing docker_tabcomplete from the xontribs that you are loading and see if the problem still exists
@scopatz how can I uninstall or unload module (the xontrib one)?
@alex-bender -- if it's loaded it's because you have `xontrib load <xontrib_name>` somewhere in your `xonshrc`.  Just comment out that line and then restart your xonsh instance
I'm getting the same error when loading `bashisms`, but with `docket_tabcomplete` not even installed:
```
$ xontrib load bashisms
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 8459, in _call
    handler(**self._kwargs)
  File "/usr/lib/python3/dist-packages/xontrib/bashisms.py", line 35, in custom_keybindings
    @handler(Keys.Escape, '.', filter=last_command_exists & insert_mode)
  File "/usr/lib/python3/dist-packages/prompt_toolkit/key_binding/registry.py", line 241, in add_binding
    return self._extra_registry.add_binding(*k, **kw)
  File "/usr/lib/python3/dist-packages/prompt_toolkit/key_binding/registry.py", line 116, in add_binding
    filter = to_cli_filter(kwargs.pop('filter', True))
  File "/usr/lib/python3/dist-packages/prompt_toolkit/filters/utils.py", line 34, in to_cli_filter
    raise TypeError('Expecting a bool or a CLIFilter instance. Got %r' % bool_or_filter)
TypeError: Expecting a bool or a CLIFilter instance. Got Condition(<function custom_keybindings.<locals>.last_command_exists at 0x7f1f48125a60>)&ViInputMode()|EmacsInsertMode()
Exception raised in event handler; ignored.
```

Resolving patch:
diff --git a/xontrib/bashisms.py b/xontrib/bashisms.py
--- a/xontrib/bashisms.py
+++ b/xontrib/bashisms.py
@@ -47,14 +47,19 @@ def replace_bang(m):
 def custom_keybindings(bindings, **kw):
     if ptk_shell_type() == "prompt_toolkit2":
         handler = bindings.add
+
+        @Condition
+        def last_command_exists():
+            return len(__xonsh__.history) > 0
+
     else:
         handler = bindings.registry.add_binding
 
-    insert_mode = ViInsertMode() | EmacsInsertMode()
+        @Condition
+        def last_command_exists(cli):
+            return len(__xonsh__.history) > 0
 
-    @Condition
-    def last_command_exists():
-        return len(__xonsh__.history) > 0
+    insert_mode = ViInsertMode() | EmacsInsertMode()
 
     @handler(Keys.Escape, ".", filter=last_command_exists & insert_mode)
     def recall_last_arg(event):


Test patch: