Repository: spyder-ide/spyder

Issue:
Variable explorer should list datetime.timedelta variables
Define a variable via

```
import datetime
x = datetime.timedelta()
```

Now  the variable should be listed in the variable explorer,
similar to `x = datetime.datetime(2016, 10, 28)`.

(My current environment is described in #3562.)



Discussion:

Resolving patch:
diff --git a/spyder/config/base.py b/spyder/config/base.py
--- a/spyder/config/base.py
+++ b/spyder/config/base.py
@@ -377,8 +377,8 @@ def get_supported_types():
     Note:
     If you update this list, don't forget to update doc/variablexplorer.rst
     """
-    from datetime import date
-    editable_types = [int, float, complex, list, dict, tuple, date
+    from datetime import date, timedelta
+    editable_types = [int, float, complex, list, dict, tuple, date, timedelta
                       ] + list(TEXT_TYPES) + list(INT_TYPES)
     try:
         from numpy import ndarray, matrix, generic
diff --git a/spyder/widgets/variableexplorer/utils.py b/spyder/widgets/variableexplorer/utils.py
--- a/spyder/widgets/variableexplorer/utils.py
+++ b/spyder/widgets/variableexplorer/utils.py
@@ -174,6 +174,34 @@ def datestr_to_datetime(value):
     return v
 
 
+def str_to_timedelta(value):
+    """Convert a string to a datetime.timedelta value.
+
+    The following strings are accepted:
+
+        - 'datetime.timedelta(1, 5, 12345)'
+        - 'timedelta(1, 5, 12345)'
+        - '(1, 5, 12345)'
+        - '1, 5, 12345'
+        - '1'
+
+    if there are less then three parameters, the missing parameters are
+    assumed to be 0. Variations in the spacing of the parameters are allowed.
+
+    Raises:
+        ValueError for strings not matching the above criterion.
+
+    """
+    m = re.match('^(?:(?:datetime\.)?timedelta)?'
+                 '\(?'
+                 '([^)]*)'
+                 '\)?$', value)
+    if not m:
+        raise ValueError('Invalid string for datetime.timedelta')
+    args = [int(a.strip()) for a in m.group(1).split(',')]
+    return datetime.timedelta(*args)
+
+
 #==============================================================================
 # Background colors for supported types
 #==============================================================================
@@ -194,6 +222,7 @@ def datestr_to_datetime(value):
            DatetimeIndex):    ARRAY_COLOR,
           Image:              "#008000",
           datetime.date:      "#808000",
+          datetime.timedelta: "#808000",
           }
 CUSTOM_TYPE_COLOR = "#7755aa"
 UNSUPPORTED_COLOR = "#ffffff"
@@ -394,6 +423,7 @@ def value_to_display(value, minmax=False, level=0):
                 display = u"'" + display + u"'"
         elif (isinstance(value, NUMERIC_TYPES) or isinstance(value, bool) or
               isinstance(value, datetime.date) or
+              isinstance(value, datetime.timedelta) or
               isinstance(value, numeric_numpy_types)):
             display = repr(value)
         else:
@@ -451,6 +481,8 @@ def display_to_value(value, default_value, ignore_errors=True):
             value = datestr_to_datetime(value)
         elif isinstance(default_value, datetime.date):
             value = datestr_to_datetime(value).date()
+        elif isinstance(default_value, datetime.timedelta):
+            value = str_to_timedelta(value)
         elif ignore_errors:
             value = try_to_eval(value)
         else:


Test patch: