From 221d9236c038d3a6fad3d06b157c1f2b627eac24 Mon Sep 17 00:00:00 2001 From: backbord Date: Tue, 17 Nov 2020 11:26:10 +0100 Subject: [PATCH] Improve handling of str values for Duration Passing a str to `Duration.xmlvalue(value)` will result in a properly formatted iso duration. --- src/zeep/xsd/types/builtins.py | 4 +++- tests/test_xsd_builtins.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/zeep/xsd/types/builtins.py b/src/zeep/xsd/types/builtins.py index e3e05c65..d57068ee 100644 --- a/src/zeep/xsd/types/builtins.py +++ b/src/zeep/xsd/types/builtins.py @@ -124,10 +124,12 @@ def pythonvalue(self, value): class Duration(BuiltinType): _default_qname = xsd_ns("duration") - accepted_types = [isodate.duration.Duration, str] + accepted_types = [isodate.duration.Duration, datetime.timedelta, str] @check_no_collection def xmlvalue(self, value): + if isinstance(value, str): + value = isodate.parse_duration(value) return isodate.duration_isoformat(value) @treat_whitespace("collapse") diff --git a/tests/test_xsd_builtins.py b/tests/test_xsd_builtins.py index a34ef8b1..9d76dea5 100644 --- a/tests/test_xsd_builtins.py +++ b/tests/test_xsd_builtins.py @@ -139,6 +139,10 @@ def test_xmlvalue(self): instance = builtins.Duration() value = isodate.parse_duration("P0Y1347M0D") assert instance.xmlvalue(value) == "P1347M" + assert instance.xmlvalue("P0Y1347M0D") == "P1347M" + assert instance.xmlvalue(datetime.timedelta(days=1347)) == "P1347D" + with pytest.raises(ValueError): + instance.xmlvalue("P15T") def test_pythonvalue(self): instance = builtins.Duration()