Repository: spyder-ide/spyder

Issue:
Error while checking/unchecking completion providers preferences
## Description

### What steps will reproduce the problem?

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

installed kite and couldnt find how to enable it. even dont see its setting in prefrenece 

### Traceback
```python-traceback

Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/preferences/widgets/configdialog.py", line 127, in accept
    configpage.apply_changes()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/preferences/api.py", line 111, in apply_changes
    self.apply_callback()
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/preferences/api.py", line 143, in <lambda>
    self._apply_settings_tabs(self.changed_options))
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/preferences/api.py", line 171, in _apply_settings_tabs
    self.apply_settings(options)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/api/preferences.py", line 108, in wrapper
    func(opts)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/api/plugins/new_api.py", line 550, in apply_conf
    self.after_configuration_update(list(options_set))
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/completion/plugin.py", line 404, in after_configuration_update
    self.register_statusbar_widget(provider_name)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/completion/plugin.py", line 502, in register_statusbar_widget
    self.statusbar.add_status_widget(currrent_widget)
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/statusbar/plugin.py", line 119, in add_status_widget
    raise SpyderAPIError(f'Status widget `{id_}` already added!')
spyder.api.exceptions.SpyderAPIError: Status widget `kite_status` already added!
```

## Versions

* Spyder version: 5.2.0 
* Python version: 3.9.5
* Qt version: 5.12.10
* PyQt5 version: 5.12.3
* Operating System: Darwin 21.1.0

### Dependencies

```

# Mandatory:
applaunchservices >=0.1.7     :  0.2.1 (OK)
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.3 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.6.0               :  7.29.0 (OK)
jedi >=0.17.2;<0.19.0         :  0.18.1 (OK)
jellyfish >=0.7               :  0.8.9 (OK)
jsonschema >=3.2.0            :  4.2.1 (OK)
keyring >=17.0.0              :  23.2.1 (OK)
nbconvert >=4.0               :  6.3.0 (OK)
numpydoc >=0.6.0              :  1.1.0 (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.11.1 (OK)
pyls_spyder >=0.4.0           :  0.4.0 (OK)
pylsp >=1.3.1;<1.4.0          :  1.3.1 (OK)
pylsp_black >=1.0.0           :  None (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.2.1 (OK)
qtawesome >=1.0.2             :  1.1.0 (OK)
qtconsole >=5.2.1;<5.3.0      :  5.2.1 (OK)
qtpy >=1.5.0                  :  1.11.2 (OK)
rtree >=0.9.7                 :  0.9.7 (OK)
setuptools >=49.6.0           :  59.2.0 (OK)
sphinx >=0.6.6                :  4.3.0 (OK)
spyder_kernels >=2.2.0;<2.3.0 :  2.2.0 (OK)
textdistance >=4.2.0          :  4.2.2 (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.24 (OK)
matplotlib >=2.0.0            :  3.5.0 (OK)
numpy >=1.7                   :  1.21.4 (OK)
pandas >=1.1.1                :  1.3.4 (OK)
scipy >=0.17.0                :  1.7.3 (OK)
sympy >=0.7.3                 :  1.9 (OK)
```



Discussion:
Hi @soni3991,

Unfortunately Kite was sold and their product is down, this made us remove everything that was connected to kite in newer versions of Spyder. For now, please uninstall it and use one of our other linter options
Checking this a little bit seems like is caused due to the handling of preferences being changed in the preferences dialog. Is not only happening for the `Enable kite provider` preference but I was also able to reproduce this with the `Enabled LSP provider` preference:

```
`Traceback (most recent call last):
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\preferences\widgets\configdialog.py", line 127, in accept
    configpage.apply_changes()
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\preferences\api.py", line 111, in apply_changes
    self.apply_callback()
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\preferences\api.py", line 143, in <lambda>
    self._apply_settings_tabs(self.changed_options))
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\preferences\api.py", line 171, in _apply_settings_tabs
    self.apply_settings(options)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\api\preferences.py", line 108, in wrapper
    func(opts)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\api\plugins\new_api.py", line 550, in apply_conf
    self.after_configuration_update(list(options_set))
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\completion\plugin.py", line 404, in after_configuration_update
    self.register_statusbar_widget(provider_name)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\completion\plugin.py", line 502, in register_statusbar_widget
    self.statusbar.add_status_widget(currrent_widget)
  File "C:\Users\dalth\AppData\Local\Programs\Spyder\pkgs\spyder\plugins\statusbar\plugin.py", line 119, in add_status_widget
    raise SpyderAPIError(f'Status widget `{id_}` already added!')
spyder.api.exceptions.SpyderAPIError: Status widget `lsp_status` already added!

```
@soni3991 If you have kite installed try to go to Kite `Settings > Redo Setup` (inside the kite copilot the ⚙️ button) and also try relaunching Spyder. However as @steff456 the kite service is not currently available: https://www.kite.com/kite-is-temporarily-unavailable/?source=download

Just out of curiosity, how did you install Kite @soni3991 ?

Anyhow, if something of the above helps let us know !
@dalthviz, please take a look at this one. I think the problem is we're not removing status bar widgets after a completion provider is turned off.

Resolving patch:
diff --git a/spyder/plugins/completion/container.py b/spyder/plugins/completion/container.py
--- a/spyder/plugins/completion/container.py
+++ b/spyder/plugins/completion/container.py
@@ -55,7 +55,7 @@ def remove_statusbar_widget(self, status_key):
 
     def get_provider_statusbar_keys(self, provider_name):
         """Get the list of statusbar keys for the given provider."""
-        return self.provider_statusbars[provider_name]
+        return self.provider_statusbars.get(provider_name, [])
 
     def statusbar_rpc(self, status_key: str, method: str, args: tuple,
                       kwargs: dict):
diff --git a/spyder/plugins/completion/plugin.py b/spyder/plugins/completion/plugin.py
--- a/spyder/plugins/completion/plugin.py
+++ b/spyder/plugins/completion/plugin.py
@@ -451,8 +451,12 @@ def register_statusbar_widget(self, provider_name, plugin_loaded=True):
             provider.STATUS_BAR_CLASSES, provider_name)
         if plugin_loaded:
             for id_ in widgets_ids:
-                currrent_widget = container.statusbar_widgets[id_]
-                self.statusbar.add_status_widget(currrent_widget)
+                # Validation to check for status bar registration before
+                # adding a widget.
+                # See spyder-ide/spyder#16977
+                if id_ not in container.statusbar_widgets:
+                    current_widget = container.statusbar_widgets[id_]
+                    self.statusbar.add_status_widget(current_widget)
 
     def unregister_statusbar(self, provider_name):
         """
@@ -463,11 +467,16 @@ def unregister_statusbar(self, provider_name):
         provider_name: str
             Name of the provider that is going to delete statusbar widgets.
         """
+        container = self.get_container()
         provider_keys = self.get_container().get_provider_statusbar_keys(
             provider_name)
         for id_ in provider_keys:
-            self.get_container().remove_statusbar_widget(id_)
-            self.statusbar.remove_status_widget(id_)
+            # Validation to check for status bar registration before trying
+            # to remove a widget.
+            # See spyder-ide/spyder#16977
+            if id_ in container.statusbar_widgets[id_]:
+                self.get_container().remove_statusbar_widget(id_)
+                self.statusbar.remove_status_widget(id_)
 
     # -------- Completion provider initialization redefinition wrappers -------
     def gather_providers_and_configtabs(self):


Test patch: