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

bpo-30302 Make timedelta.__repr__ more informative. #1493

Merged
merged 26 commits into from Jul 25, 2017

Conversation

Projects
None yet
7 participants
@musically-ut
Contributor

musically-ut commented May 7, 2017

Currently, the default implementation of datetime.datetime.repr (the default output string produced at the console/IPython) gives a rather cryptic output:

from datetime import datetime as D
D.fromtimestamp(1390953543.1) - D.fromtimestamp(1121871596)
# datetime.timedelta(3114, 28747, 100000)

For the uninitiated, it is not obvious that the numeric values here are days, seconds and microsecond respectively.

Would there be any pushback against changing this to:

# datetime.timedelta(days=3114, seconds=28747, microseconds=100000)

?

https://bugs.python.org/issue30302

@the-knights-who-say-ni

This comment has been minimized.

Show comment
Hide comment
@the-knights-who-say-ni

the-knights-who-say-ni May 7, 2017

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

the-knights-who-say-ni commented May 7, 2017

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately our records indicate you have not signed the CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

Thanks again to your contribution and we look forward to looking at it!

@mention-bot

This comment has been minimized.

Show comment
Hide comment
@mention-bot

mention-bot May 7, 2017

@musically-ut, thanks for your PR! By analyzing the history of the files in this pull request, we identified @tim-one, @abalkin and @serhiy-storchaka to be potential reviewers.

mention-bot commented May 7, 2017

@musically-ut, thanks for your PR! By analyzing the history of the files in this pull request, we identified @tim-one, @abalkin and @serhiy-storchaka to be potential reviewers.

@vadmium

This comment has been minimized.

Show comment
Hide comment
@vadmium

vadmium May 7, 2017

Member

As well as tests, the documentation needs reviewing. It actually specifies the repr return value.

Member

vadmium commented May 7, 2017

As well as tests, the documentation needs reviewing. It actually specifies the repr return value.

@musically-ut

This comment has been minimized.

Show comment
Hide comment
@musically-ut

musically-ut May 7, 2017

Contributor

@vadmium Good point. I'm assuming that the documentation is not limited to only the doc strings (actually, I couldn't find an instance there on a cursory look).

Is there an easy way to check that I have caught all cases in the extended documentation?

Contributor

musically-ut commented May 7, 2017

@vadmium Good point. I'm assuming that the documentation is not limited to only the doc strings (actually, I couldn't find an instance there on a cursory look).

Is there an easy way to check that I have caught all cases in the extended documentation?

@musically-ut

This comment has been minimized.

Show comment
Hide comment
@musically-ut

musically-ut May 8, 2017

Contributor

@vadmium I see what you meant by:

It actually specifies the repr return value.

here: https://github.com/python/cpython/blob/3.6/Doc/library/datetime.rst

So we'll have to consider the risk of someone's code breaking because it depends on parsing the repr representation of timedelta?


Update: Related mail thread addresses the question somewhat: https://marc.info/?l=python-dev&m=145065097122111&w=2

Contributor

musically-ut commented May 8, 2017

@vadmium I see what you meant by:

It actually specifies the repr return value.

here: https://github.com/python/cpython/blob/3.6/Doc/library/datetime.rst

So we'll have to consider the risk of someone's code breaking because it depends on parsing the repr representation of timedelta?


Update: Related mail thread addresses the question somewhat: https://marc.info/?l=python-dev&m=145065097122111&w=2

Show outdated Hide outdated Lib/datetime.py
Show outdated Hide outdated Lib/test/datetimetester.py
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
@serhiy-storchaka

The result of C API function calls always should be checked for error.

There are memory leaks.

Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Lib/datetime.py
Show outdated Hide outdated Lib/test/test_datetime.py
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
@musically-ut

This comment has been minimized.

Show comment
Hide comment
@musically-ut

musically-ut Jun 27, 2017

Contributor

@serhiy-storchaka Thanks for the suggestions! The code looks much cleaner and more concise now. Have a look!

Contributor

musically-ut commented Jun 27, 2017

@serhiy-storchaka Thanks for the suggestions! The code looks much cleaner and more concise now. Have a look!

Show outdated Hide outdated Doc/library/datetime.rst
self.assertEqual(repr(self.theclass(days=1, microseconds=100)),
"%s(days=1, microseconds=100)" % name)
self.assertEqual(repr(self.theclass(seconds=1, microseconds=100)),
"%s(seconds=1, microseconds=100)" % name)

This comment has been minimized.

@vstinner

vstinner Jun 29, 2017

Member

I don't think that it's worth it to add so many test cases. Just keep 3 examples, 1 negative value, the timedelta(0), and you're done no?

@vstinner

vstinner Jun 29, 2017

Member

I don't think that it's worth it to add so many test cases. Just keep 3 examples, 1 negative value, the timedelta(0), and you're done no?

This comment has been minimized.

@musically-ut

musically-ut Jun 29, 2017

Contributor

Some of the tests were added to differentiate the current implementation from the old one: i.e. no leading days=0 or seconds=0 and to highlight the current handling of negative values (i.e. the normalization of the attributes, as described in the docs). I'll remove the ones which don't test anything unique not covered elsewhere.

@musically-ut

musically-ut Jun 29, 2017

Contributor

Some of the tests were added to differentiate the current implementation from the old one: i.e. no leading days=0 or seconds=0 and to highlight the current handling of negative values (i.e. the normalization of the attributes, as described in the docs). I'll remove the ones which don't test anything unique not covered elsewhere.

Show outdated Hide outdated Lib/test/test_datetime.py
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Lib/datetime.py
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Doc/library/datetime.rst
Show outdated Hide outdated Lib/datetime.py
Show outdated Hide outdated Lib/datetime.py
Show outdated Hide outdated Lib/test/test_datetime.py
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
@vstinner

LGTM, but I would like to see a review of @serhiy-storchaka and/or @vadmium since we had some disagreement on the repr() output.

@serhiy-storchaka

The C code looks correct to me, but a little cumbersome. See my comment on the tracker.

Please add your name in Misc/ACKS if it still is not added and an entry in Misc/NEWS.d/.

Show outdated Hide outdated Lib/test/test_datetime.py
Show outdated Hide outdated Doc/library/datetime.rst
@vstinner

Still LGTM with the new C code ;-)

Show outdated Hide outdated Lib/test/test_datetime.py
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Modules/_datetimemodule.c
Show outdated Hide outdated Misc/ACKS

@musically-ut musically-ut referenced this pull request Jul 1, 2017

Merged

bpo-30822: Fix testing of datetime module. #2530

2 of 2 tasks complete
@musically-ut

This comment has been minimized.

Show comment
Hide comment
@musically-ut

musically-ut Jul 25, 2017

Contributor

I've rebased the PR on the current master.

Contributor

musically-ut commented Jul 25, 2017

I've rebased the PR on the current master.

@vstinner vstinner merged commit cc5a65c into python:master Jul 25, 2017

3 checks passed

bedevere/issue-number Issue number 30302 found
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

musically-ut added a commit to musically-ut/cpython that referenced this pull request Sep 21, 2017

Update documentation containing timedeltas repr.
This is a fix to the documentation to reflect the changes merged in as
part of #1493 (bpo-30302).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment