8000 Fix issue 427 by m-dz · Pull Request #570 · dateutil/dateutil · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix issue 427 #570

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

Merged
merged 1 commit into from
Dec 7, 2017
Merged

Fix issue 427 #570

merged 1 commit into from
Dec 7, 2017

Conversation

m-dz
Copy link
@m-dz m-dz commented Dec 7, 2017

Fixes issue #427 with all tests passing

@pganssle
Copy link
Member
pganssle commented Dec 7, 2017

@m-dz Do you want to add yourself to the Authors file, or you can just comment here with the right name / e-mail (optional) to use?

@m-dz
Copy link
Author
m-dz commented Dec 7, 2017

@pganssle , will do it, thanks!

@@ -42,6 +42,8 @@
import six
from six import binary_type, integer_types, text_type

from decimal import Decimal, getcontext
Copy link
Member

Choose a reason for hiding this comment

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

getcontext is not used


# See GH issue #427, fixing float rounding
# Conversion here might be redundant as value is already passed as Decimal()
# but _parse_min_sec() is also called in _parse_numeric_token where it is still a float().
Copy link
Member

Choose a reason for hiding this comment

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

If it's already float, you're not making anything better by converting it to Decimal.

>>> from decimal import Decimal
>>> Decimal(5.6)
Decimal('5.5999999999999996447286321199499070644378662109375')

The problem is to avoid converting it to a float in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

If this is not causing any problems just drop the Decimal call.

@@ -917,6 +917,18 @@ def test_pre_12_year_same_month(self):
dtstr = '0003-03-04'
assert parse(dtstr) == datetime(3, 3, 4)

def test_rounding_float_like_strings(self):
Copy link
Member

Choose a reason for hiding this comment

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

This is a mix of pytest and unittest style tests. For the unittest style tests, you use self.assertEqual and the name would be testRoundingFloatLikeStrings. I'm fine with you de-indenting it so it's not in a class and use pytest, like so:

@pytest.mark.parametrize('dtstr,dt', [
    ('5.6h', datetime(2009, 9, 25, 5, 36)),
    ('5.6m', datetime(2009, 9, 25, 0, 5, 36)),
    ('5.6s', datetime(2009, 9, 25, 0, 0, 5, 600000))
]
def test_rounding_floatlike_strings(dtstr, dt):
    assert parse(dtstr, default=datetime(2003, 9, 25)) == dt

Though actually '5.6s' never had this problem (that's fine, but it might be worth a comment).

@m-dz
Copy link
Author
m-dz commented Dec 7, 2017

@pganssle , just saw your comments, will have a look in the afternoon.

@m-dz
Copy link
Author
m-dz commented Dec 7, 2017

@pganssle , might be better, but I am still not sure where to put the tests.

@pganssle
Copy link
8000
Member
pganssle commented Dec 7, 2017

@m-dz I think where you put it is fine. Thanks for the PR and update!

I'm not sure that these other Decimal calls are necessary per se, but since float is probably the wrong data structure to use in this case anyway, and from brief profiling of the constructor it doesn't seem significantly slower to construct a Decimal than a float, I'm inclined to come down on the side of correctness.

@pganssle pganssle merged commit 324d6d5 into dateutil:master Dec 7, 2017
@pganssle pganssle added this to the 2.7.0 milestone Dec 7, 2017
@m-dz
Copy link
Author
m-dz commented Dec 7, 2017

@pganssle , thanks for help and useful comments! I have changed float() to Decimal() everywhere, where the modulo 1 operation was and I agree they might be redundant, but I can still see someone using e.g. '0h5.6m' as an input.

Ah, it was a pleasure!

@m-dz m-dz deleted the fix_issue_427 branch December 7, 2017 17:02
@pganssle pganssle mentioned this pull request Mar 11, 2018
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.

2 participants
0