Repository: spyder-ide/spyder

Issue:
RuntimeError when opening a new editor window
## Description

### What steps will reproduce the problem?

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

open a new editor window

close the new editor window

undock the original editor window




### Traceback
```python-traceback
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
Traceback (most recent call last):
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1123, in wheelEvent
    self.hide_completion_widget(focus_to_parent=False)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\base.py", line 1004, in hide_completion_widget
    self.completion_widget.hide(focus_to_parent=focus_to_parent)
  File "C:\Users\josea\anaconda3\lib\site-packages\spyder\plugins\editor\widgets\completion.py", line 293, in hide
    self.clear()
RuntimeError: wrapped C/C++ object of type CompletionWidget has been deleted
```

## Versions

* Spyder version: 5.3.0 
* Python version: 3.8.12
* Qt version: 5.12.9
* PyQt5 version: 5.12.3
* Operating System: Windows 10

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  4.0.0 (OK)
cloudpickle >=0.5.0           :  2.0.0 (OK)
cookiecutter >=1.6.0          :  1.7.2 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.31.1;<8.0.0       :  7.33.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.0 (OK)
jellyfish >=0.7               :  0.9.0 (OK)
jsonschema >=3.2.0            :  3.2.0 (OK)
keyring >=17.0.0              :  23.4.0 (OK)
nbconvert >=4.0               :  6.1.0 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
paramiko >=2.4.0              :  2.8.1 (OK)
parso >=0.7.0;<0.9.0          :  0.8.2 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.8.0 (OK)
pygments >=2.0                :  2.10.0 (OK)
pylint >=2.5.0                :  2.9.6 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.4.1;<1.5.0          :  1.4.1 (OK)
pylsp_black >=1.2.0           :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0     :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.3 (OK)
qtconsole >=5.3.0;<5.4.0      :  5.3.0 (OK)
qtpy >=2.0.1                  :  2.1.0 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  58.0.4 (OK)
sphinx >=0.6.6                :  4.2.0 (OK)
spyder_kernels >=2.3.0;<2.4.0 :  2.3.0 (OK)
textdistance >=4.2.0          :  4.2.1 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3             :  2.1.6 (OK)
zmq >=17                      :  22.3.0 (OK)

# Optional:
cython >=0.21                 :  0.29.25 (OK)
matplotlib >=3.0.0            :  3.5.0 (OK)
numpy >=1.7                   :  1.20.3 (OK)
pandas >=1.1.1                :  1.3.4 (OK)
scipy >=0.17.0                :  1.7.1 (OK)
sympy >=0.7.3                 :  1.9 (OK)
```



Discussion:
Hey @jose-ah, thanks a lot for reporting. We'll solve this problem in our next version (5.3.3), to be released in a couple of weeks.

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/base.py b/spyder/plugins/editor/widgets/base.py
--- a/spyder/plugins/editor/widgets/base.py
+++ b/spyder/plugins/editor/widgets/base.py
@@ -985,7 +985,12 @@ def is_completion_widget_visible(self):
 
     def hide_completion_widget(self, focus_to_parent=True):
         """Hide completion widget and tooltip."""
-        self.completion_widget.hide(focus_to_parent=focus_to_parent)
+        # This is necessary to catch an error when creating new editor windows.
+        # Fixes spyder-ide/spyder#19109
+        try:
+            self.completion_widget.hide(focus_to_parent=focus_to_parent)
+        except RuntimeError:
+            pass
         QToolTip.hideText()
 
     # ------Standard keys
diff --git a/spyder/plugins/editor/widgets/completion.py b/spyder/plugins/editor/widgets/completion.py
--- a/spyder/plugins/editor/widgets/completion.py
+++ b/spyder/plugins/editor/widgets/completion.py
@@ -13,8 +13,7 @@
 # Third psrty imports
 from qtpy.QtCore import QPoint, Qt, Signal, Slot
 from qtpy.QtGui import QFontMetrics, QFocusEvent
-from qtpy.QtWidgets import (QAbstractItemView, QApplication, QListWidget,
-                            QListWidgetItem, QToolTip)
+from qtpy.QtWidgets import QListWidget, QListWidgetItem, QToolTip
 
 # Local imports
 from spyder.utils.icon_manager import ima
@@ -217,7 +216,6 @@ def _get_cached_icon(self, name):
 
     def set_item_display(self, item_widget, item_info, height, width):
         """Set item text & icons using the info available."""
-        item_provider = item_info['provider']
         item_type = self.ITEM_TYPE_MAP.get(item_info['kind'], 'no_match')
         item_label = item_info['label']
         icon_provider = ("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0l"


Test patch: