-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
fromutc does not respect datetime subclasses #76598
Comments
When preparing some tests for how subclasses of date and datetime react as part of a fix for bpo-32403, I noticed a fairly big example of where subclass is not preserved - from datetime import datetime, timezone
class DateTimeSubclass(datetime):
pass
dt = DateTimeSubclass(2012, 1, 1)
dt2 = dt.astimezone(timezone.utc)
print(type(dt))
print(type(dt2))
This also affects And either way, it's quite inconsistent to have There is probably a somewhat inelegant way to get the alternate constructors working properly (ignore the type of the argument up until the last return and then construct the subclass from the components of the datetime), but I think it might be better to fix the behavior of tzinfo.fromutc. Somewhat related to bpo-32404 and 31222, in that this concerns which operations preserve type in subclasses. |
I've noticed that there's another complicating factor here, which is that addition of |
Since the C code is only a few lines, what do you think of also fixing Python 2.7? |
I am somewhat uneasy about backporting this to Python 2.7 because changing the return type of If it is decided that this is just a bugfix, I'm OK with creating a backport for 2.7 (provided that there's nothing so different about 2.7 that the fix becomes much bigger). |
You asked to backport up to Python 3.6. The change is either fine to be backported to 2.7 and 3.6, or should not be backported. I prefer to have the same policy for stable branches... but I also understand that 2.7 requires even more stability. |
Ah, that's my mistake. I have always been under the impression that "Versions" meant "versions affected", not "versions that this needs to be fixed for". I usually just selected the ones where I had verified that it's a problem. I do not think this should be backported to 3.6. From the discussion in the datetime-SIG mailing list, we have realized that this change will *also* break anyone whose default constructor does not support the same signature as the base datetime. I think this is probably not a major problem (many other alternate constructors assume that the constructor accepts arguments as datetime does), but it's not something that I think we should be changing in a patch version. |
What's the use case for subclassing DateTime? These classes were not designed with subclassing as a use case in mind. |
There are several reasons for doing it, of various levels of legitimacy. The overall theme is that people want different behaviors from their datetime classes and they want to maintain drop-in compatibility with datetime so that you don't need to re-build the whole world of datetime-handling libraries if you want to adopt one of these alternative datetime providers. Ideally, you would tell people to just write API-compatible code and use duck-typing, but there's a lot of code in the standard library that uses Two popular datetime frameworks arrow and pendulum, both use datetime subclasses. A lot of what they are providing is convenience methods that could easily be free functions, but they also need to be subclasses so that they can change things like the semantics of arithmetic. For example, one motivation for the creation of pendulum was that the creator wanted this invariant to hold true:
This is basically due to the fact that in Python's datetime library, no distinction is made between "absolute deltas" (the absolute time between two events) and "calendar deltas", which makes subtraction or addition across DST boundaries ambiguous and occasionally lossy. Arithmetic semantics are one of the things about datetime I'd most love to change but for backwards compatibility reasons it's just not feasible. Another reason I've seen for subclassing datetime is that this is how dateutil provides its backport of PEP-495 (ambiguous datetime support). We have a datetime subclass called _DatetimeWithFold that supports the One last place I've seen datetime subclasses used is when you have a thin wrapper used for dispatch or other purposes where you are mapping between types. For example, at work we had to create mappings between python types and the types specified by a standard (developed for another language), but that standard specified both a datetime type (with millisecond precision) and a datetimeus type (with microsecond precision). The solution was a thin wrapper around datetime called DatetimeUs: https://github.com/bloomberg/python-comdb2/blob/master/comdb2/_cdb2_types.py#L62 Preventing operations from reverting to datetime was a bit of a pain, which is why we have a bunch of tests to check that the subclass survives basic operations: https://github.com/bloomberg/python-comdb2/blob/master/tests/test_cdb2_datetimeus.py#L95 Although it was not originally *designed* to be subclassed, support for datetime subclasses is already quite good. This timedelta issue is one of the last major issues to fix to make them truly subclass-friendly. I'll note also that for the past 9 years, the test suite has run all datetime tests against a "thin wrapper" subclass of datetime: https://github.com/python/cpython/blame/028f0ef4f3111d2b3fc5b971642e337ba7990873/Lib/test/datetimetester.py#L2802 |
OK. |
Hm, when I made the "What's new" issue, it added the same PR to the "Pull requests" 4 times instead of once, and in the history it seems like it *tried* to actually add PR 11790, 11791, 11792 and 11793 (only the first one exists at the moment). Not sure why that happened and where I'd report that bug. |
Ah, sorry for the noise, this is a known issue: python/bugs.python.org#12 |
This issue broke a date subclass in the calendra project (jaraco/calendra#11). I acknowledge this change was a known breakage, but I mention it here and link the downstream issue for your information. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: