Skip to content
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

Closed

Conversation

JeanArhancet
Copy link
Contributor

@JeanArhancet JeanArhancet commented Oct 22, 2021

Change Summary

There is an issue with scientific notations and timedelta

Related issue number

fix #3315

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@JeanArhancet
Copy link
Contributor Author

please review

pydantic/datetime_parse.py Outdated Show resolved Hide resolved
Copy link

@ehiggs ehiggs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Copy link
Member

@samuelcolvin samuelcolvin left a 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.

@@ -226,7 +226,14 @@ def parse_duration(value: StrBytesIntFloat) -> timedelta:
value = str(value)
elif isinstance(value, bytes):
value = value.decode()


if isinstance(value, (str)):
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please review

changes/3315-JeanArhancet.md Outdated Show resolved Hide resolved
@samuelcolvin
Copy link
Member

please update

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
@JeanArhancet
Copy link
Contributor Author

please review

Copy link
Member

@samuelcolvin samuelcolvin left a 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.

@@ -54,6 +54,9 @@
r'$'
)

# Support scientific notation
scientific_notation = re.compile(r'^(?P<sign>[-+]?)' r'(?P<scientific_notation>\d+(.\d+)?[eE][+\-]?\d+)?' r'$')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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+)?$')

@@ -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('.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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 ?

@samuelcolvin
Copy link
Member

please update.

@JeanArhancet
Copy link
Contributor Author

please review

@JeanArhancet
Copy link
Contributor Author

@samuelcolvin this pull request still valid?

@samuelcolvin
Copy link
Member

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.

@samuelcolvin
Copy link
Member

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.

@samuelcolvin samuelcolvin mentioned this pull request Aug 4, 2022
14 tasks
@samuelcolvin
Copy link
Member

samuelcolvin commented Aug 5, 2022

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.

@JeanArhancet JeanArhancet deleted the feat/scientific_notation branch August 5, 2022 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serialization -> de-serialisation fails for small timedelta (< 100 microseconds)
4 participants