Skip to content

Fix issue 427#570

Merged
pganssle merged 1 commit into
dateutil:masterfrom
m-dz:fix_issue_427
Dec 7, 2017
Merged

Fix issue 427#570
pganssle merged 1 commit into
dateutil:masterfrom
m-dz:fix_issue_427

Conversation

@m-dz

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

Copy link
Copy Markdown

Fixes issue #427 with all tests passing

@pganssle

pganssle commented Dec 7, 2017

Copy link
Copy Markdown
Member

@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

m-dz commented Dec 7, 2017

Copy link
Copy Markdown
Author

@pganssle , will do it, thanks!

Comment thread dateutil/parser/_parser.py Outdated
import six
from six import binary_type, integer_types, text_type

from decimal import Decimal, getcontext

Copy link
Copy Markdown
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

Comment thread dateutil/parser/_parser.py Outdated

# 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
Copy Markdown
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
Copy Markdown
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.

Comment thread dateutil/test/test_parser.py Outdated
dtstr = '0003-03-04'
assert parse(dtstr) == datetime(3, 3, 4)

def test_rounding_float_like_strings(self):

Copy link
Copy Markdown
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

m-dz commented Dec 7, 2017

Copy link
Copy Markdown
Author

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

@m-dz

m-dz commented Dec 7, 2017

Copy link
Copy Markdown
Author

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

@pganssle

pganssle commented Dec 7, 2017

Copy link
Copy Markdown
Member

@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

m-dz commented Dec 7, 2017

Copy link
Copy Markdown
Author

@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