Skip to content

Commit

Permalink
Improve handling of str values for Duration
Browse files Browse the repository at this point in the history
Passing a str to `Duration.xmlvalue(value)` will result
in a properly formatted iso duration.
  • Loading branch information
backbord authored and mvantellingen committed Aug 15, 2021
1 parent aedbb06 commit 221d923
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/zeep/xsd/types/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_xsd_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 221d923

Please sign in to comment.