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
feat(timedelta): add scientific notation #3346
feat(timedelta): add scientific notation #3346
Conversation
d889680
to
34ff7d5
Compare
please review |
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.
lgtm!
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, but I think we can implement in a cleaner way.
pydantic/datetime_parse.py
Outdated
@@ -226,7 +226,14 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta: | |||
value = str(value) | |||
elif isinstance(value, bytes): | |||
value = value.decode() | |||
|
|||
|
|||
if isinstance(value, (str)): |
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.
Instead of adding this into the logic for all cases, I think you can match all the cases described in your tests with another regex, which can be appended to the current regexes checked and therefore have no effect on performance if it's not called.
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.
Thanks for you feedback.
The regex in the commit 88ffae7 is good for you?
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.
please review
please update |
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
please review |
7e0fa8e
to
c49de2e
Compare
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.
otherwlise I think looking good.
pydantic/datetime_parse.py
Outdated
@@ -54,6 +54,9 @@ | |||
r'$' | |||
) | |||
|
|||
# Support scientific notation | |||
scientific_notation = re.compile(r'^(?P<sign>[-+]?)' r'(?P<scientific_notation>\d+(.\d+)?[eE][+\-]?\d+)?' r'$') |
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.
scientific_notation = re.compile(r'^(?P<sign>[-+]?)' r'(?P<scientific_notation>\d+(.\d+)?[eE][+\-]?\d+)?' r'$') | |
scientific_notation = re.compile(r'^(?P<sign>[-+]?)(?P<scientific_notation>\d+(.\d+)?[eE][+\-]?\d+)?$') |
pydantic/datetime_parse.py
Outdated
@@ -243,6 +248,9 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta: | |||
if kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-'): | |||
kw['microseconds'] = '-' + kw['microseconds'] | |||
|
|||
if kw.get('scientific_notation'): | |||
kw['seconds'] = ("%.17s" % kw.pop('scientific_notation')).rstrip('0').rstrip('.') |
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.
kw['seconds'] = ("%.17s" % kw.pop('scientific_notation')).rstrip('0').rstrip('.') | |
kw['seconds'] = ("%.17s" % kw.pop('scientific_notation')).rstrip('0').rstrip('.') |
this needs a comment to explain what's going on, also no need to use percent subsitution, you can use an f-string or format.
Also, I'm not quite clear what you're trying to do, but I would guess you can do it with standard string formatting, no need for rstrip
.
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.
I've changed, What do you think ?
please update. |
4b47805
to
ce48134
Compare
please review |
@samuelcolvin this pull request still valid? |
Yes, hopefully this can be included in v1.10. No idea what's going to happen to this on V2, but I guess we cross that bridge when we get there. |
I'm really not sure about this, we don't support this in V2 and adding it to speedate wouldn't be trivial. We can fix the original issue in #3315 by serialising timedeltas better. |
replaced by #4329 which i think is simpler and also avoids adding now features which would not be compatible with V2. Thanks for the contribution, it was useful in working towards a fix. |
Change Summary
There is an issue with scientific notations and timedelta
Related issue number
fix #3315
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)