From 1ead5c6e8b7e61fb1f2447485e20b340ea42bfdc Mon Sep 17 00:00:00 2001 From: Giovanni Pizzi Date: Tue, 10 Dec 2019 14:25:48 +0100 Subject: [PATCH] Make sure that datetime conversions ignore `None` The functions `datetime_to_isoformat` and `isoformat_to_datetime` assumed to always have a proper type (string or datetime) as input. However, in some cases, they were called with values that could be potentially `None`, like this in aiida/engine/utils.py: ``` timezone.isoformat_to_datetime(manager.get(key).value)) ``` We are now directly returning `None` if `None` is passed as an input (and then it's up to the caller to then decide what to do with the value. Fixes #3336 --- aiida/common/timezone.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/aiida/common/timezone.py b/aiida/common/timezone.py index f940b9f17f..0727e2c68c 100644 --- a/aiida/common/timezone.py +++ b/aiida/common/timezone.py @@ -124,6 +124,8 @@ def datetime_to_isoformat(value): :param value: a datetime object """ + if value is None: + return None return value.isoformat() @@ -132,4 +134,6 @@ def isoformat_to_datetime(value): :param value: a ISO format string representation of a datetime object """ + if value is None: + return None return dateutil.parser.parse(value)