-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ordered duration strings for timedelta #366
Conversation
a754a45
to
e5345ff
Compare
e5345ff
to
e238856
Compare
Thanks for the PR! This is interesting. Do you have a source from which you derived the regex? I'm open to this feature, but it'd be good to have a spec or a source that we can refer to if we need to update it. |
Hi @sloria 👋 Thanks for the quick reply! This PR loosely adopts the GEP-2257 spec (which has been widely adopted by the infra/devops community) to Python timedelta units:
The PR supports only the units which are supported by Python timedelta. So no month / year / nanoseconds. Avoids month/minute ambiguity as well (also, how to convert month to timedelta: 1 month == 30.5 days?) I wrote the pattern myself and added the comments for maintainability. Discrepancies with the GEP-2257 duration strings:
|
Thanks, that's very helpful info 👍 . Above the regex, can you add a link to the spec and add a comment block with the discrepancies you listed? |
@@ -1,6 +1,6 @@ | |||
# Changelog | |||
|
|||
## 11.1.0 (2025-11-11) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch
|
Thank you! Looks great! Is there a good reason to make the regex case-insensitive? It could be more future-proof to keep it case-sensitive. Uppercase characters are probably a typo by the user anyway, right? |
Sure thing, done 💥 |
Thank you! This looks good to go |
Released in 11.2.0 |
This was a breaking change... from environs import Env
env = Env()
toto = env.timedelta("TOTO", default=30.0) Now it takes a string and it can be only an int in it: toto = env.timedelta("TOTO", default=30) # does not works
toto = env.timedelta("TOTO", default=30.0) # does not works
toto = env.timedelta("TOTO", default="30") # works
toto = env.timedelta("TOTO", default="30.") # does not works To be fair, passing a float before was working but in a strange way cause if you were providing |
interesting, marshmallow expects a int for timedelta, and this PR switched to subclassing TimeDelta. maybe there was some lenience that was silently swallowing the float |
I've submitted a PR to propose a fix #368 |
Fix released in 11.2.1. Thanks for the report and the fix! |
Duration strings like
7m7s
are widely used for configs, especially anything kubernetes related (anything go-related really, because of thetime.ParseDuration
builtin).This PR adds a simple yet robust regex parser for such strings.
For timedelta specifically, this also incorporates #297