Repository: spyder-ide/spyder

Issue:
Error in variable explorer for dicts with mixed-type keys
## Description of your problem

**What steps will reproduce the problem?**

1. Create a dict with `str` and `int` keys: `d = {'1': 1, 2: 2}`
2. Open it in the variable explorer
3. Click ok to close the window: error pops-out

```python
  File "...\spyder\widgets\variableexplorer\collectionseditor.py", line 527, in <lambda>
    lambda eid=id(editor): self.editor_accepted(eid))
  File "...\spyder\widgets\variableexplorer\collectionseditor.py", line 549, in editor_accepted
    self.set_value(index, conv_func(value))
  File "...\spyder\widgets\variableexplorer\collectionseditor.py", line 1370, in set_value
    self.set_value_func(name, value)
  File "...\spyder\widgets\variableexplorer\namespacebrowser.py", line 304, in set_value
    value = serialize_object(value)
  File "...\ipykernel\serialize.py", line 85, in serialize_object
    for k in sorted(obj):
TypeError: '<' not supported between instances of 'int' and 'str'
```

## Versions and main components

* Spyder Version: 3.2.3
* Python Version: 3.6

## Dependencies
IPython >=4.0    :  5.3.0 (OK)
cython >=0.21    :  0.25.2 (OK)
jedi >=0.9.0     :  0.10.2 (OK)
nbconvert >=4.0  :  5.1.1 (OK)
numpy >=1.7      :  1.12.1 (OK)
pandas >=0.13.1  :  0.20.1 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pyflakes >=0.6.0 :  1.5.0 (OK)
pygments >=2.0   :  2.2.0 (OK)
pylint >=0.25    :  1.6.4 (OK)
qtconsole >=4.2.0:  4.3.0 (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
sphinx >=0.6.6   :  1.5.6 (OK)
sympy >=0.7.3    :  1.0 (OK)



Discussion:
Actually I think the error comes from [`ipykernel`](https://github.com/ipython/ipykernel/blob/master/ipykernel/serialize.py#L58). I do not think there is something we can do within spyder. I will open an [issue ](https://github.com/ipython/ipykernel/issues/269) there.
@Prikers, we need to deal with this error in our side because it's a failure to serialize an object so that it can be saved by Spyder to the current namespace.
@ccordoba12, ok noted! How can we handle that?
#5341 trades the use of IPython-parallel's protocol extensions for an equivalent Spyder-controlled extension, and in doing so using `cloudpickle` for serialization, which should fix this particular bug.
@Prikers, Min's PR is a great improvement for Spyder 4. For now, we should simply catch the error and skip setting a new value for the dict.

Resolving patch:
diff --git a/spyder/widgets/variableexplorer/namespacebrowser.py b/spyder/widgets/variableexplorer/namespacebrowser.py
--- a/spyder/widgets/variableexplorer/namespacebrowser.py
+++ b/spyder/widgets/variableexplorer/namespacebrowser.py
@@ -301,8 +301,12 @@ def get_value(self, name):
 
     def set_value(self, name, value):
         """Set value for a variable."""
-        value = serialize_object(value)
-        self.shellwidget.set_value(name, value)
+        try:
+            value = serialize_object(value)
+            self.shellwidget.set_value(name, value)
+        except TypeError as e:
+            QMessageBox.critical(self, _("Error"),
+                                 "TypeError: %s" % to_text_string(e))
         self.refresh_table()
         
     def remove_values(self, names):


Test patch: