Improve handling of str values for Duration #1165
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @mvantellingen,
I really like your project but recently stumbled into #876 when I wanted to specify a duration as
"PT15M"
and got"P%P"
in the generated SOAP message.Here's my attempt to improve the behaviour. Two simple and small test cases are included.
I hope you might consider merging the PR or giving advice if there's another desired way to do things.
In this PR, two things happen:
datetime.timedelta
value is allowed.str
value will result in thestr
being parsed byisodate.parse_duration()
to ensure the provided value is indeed an iso duration.The result of the parsing is then handed over to
isodate.duration_isoformat()
to produce the desired duration string.This round-trip mimics one of your test cases and results in
builtins.Duration().xmlvalue("P0Y1347M0D") == "P1347M"
where the superfluous zeros are eliminated. This should also ensure that the string is a valid duration in isoformat and raise aValueError
if it cannot be parsed as such.Thanks and best regards,
Tim