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-32417: Make timedelta arithmetic respect subclasses #10902

Merged
merged 7 commits into from Feb 4, 2019

Conversation

Projects
None yet
6 participants
@pganssle
Copy link
Contributor

pganssle commented Dec 4, 2018

This fixes both bpo-35364 and bpo-32417. See this comment for a detailed rationale.

By changing timedelta over to returning the subclass it was added to, fromutc now also respects the subclass of the datetime being converted, and consequently so do all the tz=... forms of the alternate constructors.

This will fix many major headaches for people implementing datetime and date subclasses.

https://bugs.python.org/issue32417
https://bugs.python.org/issue35364

https://bugs.python.org/issue32417

Show resolved Hide resolved Lib/test/datetimetester.py Outdated
@vstinner
Copy link
Member

vstinner left a comment

LGTM, but I would prefer to see a review from a second core dev. @serhiy-storchaka, @abalkin, @pablogsal maybe?

@pganssle pganssle force-pushed the pganssle:timedelta_return_datetime_subclass branch from 627d4cb to 8c784f8 Jan 2, 2019

@pganssle

This comment has been minimized.

Copy link
Contributor Author

pganssle commented Jan 2, 2019

@vstinner Thanks for your comments, sorry for the delay in addressing them - I put it off for a bit because this change likely needs wider discussion and I haven't had time to have that discussion with python-dev before now.

I've addressed your style concerns, rebased against master and started a thread on python-dev for discussion.

pganssle added some commits Dec 4, 2018

Make timedelta return subclass types
Previously timedelta would always return the `date` and `datetime`
types, regardless of what it is added to. This makes it return
an object of the type it was added to.
Add news entry.
Fixes:
bpo-32417
bpo-35364
More descriptive variable names in tests
Addresses Victor's comments

@pganssle pganssle force-pushed the pganssle:timedelta_return_datetime_subclass branch from 8c784f8 to 598b665 Feb 4, 2019

@gvanrossum
Copy link
Member

gvanrossum left a comment

What about the C code?

@pganssle

This comment has been minimized.

Copy link
Contributor Author

pganssle commented Feb 4, 2019

@gvanrossum That's the first commit, it's here (at the bottom of the diff). It's just a matter of switching to the C function that constructs either a datetime or subclass, depending on the type of self, which is why the diff is so small. This is done in a wrapper function because it's useful in several other places (and also is subject to optimization).

@gvanrossum
Copy link
Member

gvanrossum left a comment

Sorry I missed the C code at first! Reviewing on a phone is not ideal. All looks good. Go ahead and merge!

@abalkin abalkin self-assigned this Feb 4, 2019

@abalkin abalkin merged commit 89427cd into python:master Feb 4, 2019

5 checks passed

Azure Pipelines PR #20190204.7 succeeded
Details
bedevere/issue-number Issue number 32417 found
Details
bedevere/news News entry found in Misc/NEWS.d
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@bedevere-bot

This comment has been minimized.

Copy link

bedevere-bot commented Feb 4, 2019

@abalkin: Please replace # with GH- in the commit message next time. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment