Repository: spyder-ide/spyder

Issue:
Date object not editable in variable explorer
## Description

### What steps will reproduce the problem?

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

Importing date module to create a date object but not able to change date from variable explorer. If the object is is string i can edit it but not in date object.

### Traceback
```python-traceback
  File "D:\Anaconda\lib\site-packages\spyder\plugins\variableexplorer\widgets\collectionsdelegate.py", line 201, in createEditor
    value.time()
AttributeError: 'datetime.date' object has no attribute 'time'
```

## Versions

* Spyder version: 4.1.4 
* Python version: 3.7.7
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Windows 10

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0           :  1.4.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.5.0 (OK)
diff_match_patch >=20181111    :  20200713 (OK)
intervaltree                   :  None (OK)
IPython >=4.0                  :  7.16.1 (OK)
jedi =0.17.1                   :  0.17.1 (OK)
nbconvert >=4.0                :  5.6.1 (OK)
numpydoc >=0.6.0               :  1.1.0 (OK)
paramiko >=2.4.0               :  2.7.1 (OK)
parso =0.7.0                   :  0.7.0 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.0 (OK)
pygments >=2.0                 :  2.6.1 (OK)
pylint >=1.0                   :  2.5.3 (OK)
pyls >=0.34.0;<1.0.0           :  0.34.1 (OK)
qdarkstyle >=2.8               :  2.8.1 (OK)
qtawesome >=0.5.7              :  0.7.2 (OK)
qtconsole >=4.6.0              :  4.7.5 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.4 (OK)
sphinx >=0.6.6                 :  3.1.2 (OK)
spyder_kernels >=1.9.2;<1.10.0 :  1.9.2 (OK)
watchdog                       :  None (OK)
zmq >=17                       :  19.0.1 (OK)

# Optional:
cython >=0.21                  :  0.29.21 (OK)
matplotlib >=2.0.0             :  3.2.2 (OK)
numpy >=1.7                    :  1.19.1 (OK)
pandas >=0.13.1                :  1.0.3 (OK)
scipy >=0.17.0                 :  1.5.0 (OK)
sympy >=0.7.3                  :  1.6.1 (OK)
```



Discussion:
Hi @harry552,
I can reproduce this error, we will fix in for our next release of Spyder. Thanks for reporting!
@andfoy please take a look at this one
It seems this may be a bug in the implementation of https://github.com/spyder-ide/spyder/pull/12700

I think maybe moving [this segment ](https://github.com/spyder-ide/spyder/blob/master/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py#L199-L203):
```
try:
    value.time()
except ValueError:
    self.sig_editor_shown.emit()
    return None
````
after you check `isinstance(value, datetime.datetime)` on [line 208](https://github.com/spyder-ide/spyder/blob/master/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py#L208) should solve the problem of `datetime.date` not having a `time()` method
Note: Related with #8329 and PR #12700

Resolving patch:
diff --git a/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py b/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
--- a/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
+++ b/spyder/plugins/variableexplorer/widgets/collectionsdelegate.py
@@ -195,19 +195,19 @@ def createEditor(self, parent, option, index, object_explorer=False):
             return None
         # QDateEdit and QDateTimeEdit for a dates or datetime respectively
         elif isinstance(value, datetime.date) and not object_explorer:
-            # Needed to handle NaT values
-            # See spyder-ide/spyder#8329
-            try:
-                value.time()
-            except ValueError:
-                self.sig_editor_shown.emit()
-                return None
             if readonly:
                 self.sig_editor_shown.emit()
                 return None
             else:
                 if isinstance(value, datetime.datetime):
                     editor = QDateTimeEdit(value, parent=parent)
+                    # Needed to handle NaT values
+                    # See spyder-ide/spyder#8329
+                    try:
+                        value.time()
+                    except ValueError:
+                        self.sig_editor_shown.emit()
+                        return None
                 else:
                     editor = QDateEdit(value, parent=parent)
                 editor.setCalendarPopup(True)


Test patch:
diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py b/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py
--- a/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py
+++ b/spyder/plugins/variableexplorer/widgets/tests/test_collectioneditor.py
@@ -27,7 +27,7 @@
 import pytest
 from flaky import flaky
 from qtpy.QtCore import Qt, QPoint
-from qtpy.QtWidgets import QWidget
+from qtpy.QtWidgets import QWidget, QDateEdit
 
 # Local imports
 from spyder.plugins.variableexplorer.widgets.collectionseditor import (
@@ -479,6 +479,26 @@ def test_rename_and_duplicate_item_in_collection_editor():
             assert editor.source_model.get_data() == coll_copy + [coll_copy[0]]
 
 
+def test_edit_datetime(monkeypatch):
+    """
+    Test datetimes are editable and NaT values are correctly handled.
+
+    Regression test for spyder-ide/spyder#13557 and spyder-ide/spyder#8329
+    """
+    variables = [pandas.NaT, datetime.date.today()]
+    editor_list = CollectionsEditorTableView(None, variables)
+
+    # Test that the NaT value cannot be edited on the variable explorer
+    editor_list_value = editor_list.delegate.createEditor(
+        None, None, editor_list.model.index(0, 3))
+    assert editor_list_value is None
+
+    # Test that a date can be edited on the variable explorer
+    editor_list_value = editor_list.delegate.createEditor(
+        None, None, editor_list.model.index(1, 3))
+    assert isinstance(editor_list_value, QDateEdit)
+
+
 def test_edit_mutable_and_immutable_types(monkeypatch):
     """
     Test that mutable objs/vals are editable in VarExp; immutable ones aren't.