Repository: spyder-ide/spyder

Issue:
The ( bracket does not always appear from input in IPython console
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

When using functions in the Ipython console, the open bracket does not appear from keyboard input, i.e. ( is ignored/skipped.

Hence, using my_func one ends up with something like my_func*args)

This usually occurs once large data structures have been created and memory becomes low.

### Traceback
```python-traceback
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/control.py", line 58, in keyPressEvent
    self._key_paren_left(text)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/control.py", line 50, in _key_paren_left
    self.show_object_info(last_obj)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/widgets/mixins.py", line 1356, in show_object_info
    self.help.set_object_text(text, ignore_unknown=False)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/plugins/help/plugin.py", line 504, in set_object_text
    found = self.show_help(text, ignore_unknown=ignore_unknown)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/plugins/help/plugin.py", line 682, in show_help
    if not shell.is_defined(obj_text):
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/widgets/help.py", line 135, in is_defined
    ).is_defined(objtxt, force_import=force_import)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 542, in __call__
    call_dict, call_data, self._comm_id)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder/plugins/ipythonconsole/comms/kernelcomm.py", line 114, in _get_call_return_value
    call_dict, call_data, comm_id)
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 420, in _get_call_return_value
    return self._sync_error(reply['value'])
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 480, in _sync_error
    error_wrapper.raise_error()
  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 90, in raise_error
    raise self.etype(self)
Exception in comms call is_defined:

  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 342, in _handle_remote_call
    buffer['call_kwargs'])

  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/comms/commbase.py", line 353, in _remote_callback
    *call_args, **call_kwargs)

  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/console/kernel.py", line 326, in is_defined
    return isdefined(obj, force_import=force_import, namespace=ns)

  File "/home/dylan/anaconda3/lib/python3.7/site-packages/spyder_kernels/utils/dochelpers.py", line 317, in isdefined
    attr_not_found = not hasattr(eval(base, namespace), attr)

TypeError: idx2value() missing 1 required positional argument: 'idx'

```

## Versions

* Spyder version: 4.0.1 
* Python version: 3.7.6
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Linux 5.11.0-44-generic

### Dependencies

```
atomicwrites >=1.2.0         :  1.4.0 (OK)
chardet >=2.0.0              :  4.0.0 (OK)
cloudpickle >=0.5.0          :  2.0.0 (OK)
diff_match_patch >=20181111  :  20200713 (OK)
intervaltree                 :  None (OK)
IPython >=4.0                :  7.22.0 (OK)
jedi =0.14.1                 :  0.14.1 (OK)
keyring                      :  None (OK)
nbconvert >=4.0              :  6.1.0 (OK)
numpydoc >=0.6.0             :  1.1.0 (OK)
pexpect >=4.4.0              :  4.8.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
psutil >=0.3                 :  5.8.0 (OK)
pygments >=2.0               :  2.10.0 (OK)
pylint >=0.25                :  2.9.6 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.7 (OK)
xdg >=0.26                   :  0.27 (OK)
zmq >=17                     :  22.2.1 (OK)
qdarkstyle >=2.7             :  3.0.2 (OK)
qtawesome >=0.5.7            :  1.0.2 (OK)
qtconsole >=4.6.0            :  5.1.1 (OK)
qtpy >=1.5.0                 :  1.10.0 (OK)
rtree >=0.8.3                :  0.9.7 (OK)
sphinx >=0.6.6               :  4.2.0 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.8.1 (OK)
watchdog                     :  None (OK)
cython >=0.21                :  0.29.24 (OK)
matplotlib >=2.0.0           :  3.4.2 (OK)
numpy >=1.7                  :  1.19.2 (OK)
pandas >=0.13.1              :  1.3.3 (OK)
scipy >=0.17.0               :  1.6.2 (OK)
sympy >=0.7.3                :  1.8 (OK)
```



Discussion:
Hey @dylansolms, thanks for reporting. We'll fix this problem in our next version (5.2.2), to be released in a couple of weeks.

Resolving patch:
diff --git a/external-deps/spyder-kernels/spyder_kernels/comms/commbase.py b/external-deps/spyder-kernels/spyder_kernels/comms/commbase.py
--- a/external-deps/spyder-kernels/spyder_kernels/comms/commbase.py
+++ b/external-deps/spyder-kernels/spyder_kernels/comms/commbase.py
@@ -170,8 +170,11 @@ def close(self, comm_id=None):
         id_list = self.get_comm_id_list(comm_id)
 
         for comm_id in id_list:
-            self._comms[comm_id]['comm'].close()
-            del self._comms[comm_id]
+            try:
+                self._comms[comm_id]['comm'].close()
+                del self._comms[comm_id]
+            except KeyError:
+                pass
 
     def is_open(self, comm_id=None):
         """Check to see if the comm is open."""
diff --git a/external-deps/spyder-kernels/spyder_kernels/utils/dochelpers.py b/external-deps/spyder-kernels/spyder_kernels/utils/dochelpers.py
--- a/external-deps/spyder-kernels/spyder_kernels/utils/dochelpers.py
+++ b/external-deps/spyder-kernels/spyder_kernels/utils/dochelpers.py
@@ -315,7 +315,7 @@ def isdefined(obj, force_import=False, namespace=None):
     for attr in attr_list:
         try:
             attr_not_found = not hasattr(eval(base, namespace), attr)
-        except (SyntaxError, AttributeError):
+        except (AttributeError, SyntaxError, TypeError):
             return False
         if attr_not_found:
             if force_import:


Test patch: