You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
equality not symmetric for subclasses of datetime.date and datetime.datetime #49766
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:
assignee='https://github.com/abalkin'closed_at=Nonecreated_at=<Date2009-03-19.00:51:23.421>labels= ['3.7', 'type-bug', 'library']
title='equality not symmetric for subclasses of datetime.date and datetime.datetime'updated_at=<Date2018-07-05.15:58:15.282>user='https://bugs.python.org/jessaustin'
changed the title [-]equality not reflixive for subclasses of datetime.date and datetime.datetime[/-][+]equality not reflexive for subclasses of datetime.date and datetime.datetime[/+]on Mar 19, 2009
changed the title [-]equality not reflexive for subclasses of datetime.date and datetime.datetime[/-][+]equality not symmetric for subclasses of datetime.date and datetime.datetime[/+]on Mar 19, 2009
jessaustin commented on Mar 23, 2009
jessaustinmannequin
MannequinAuthor
More actions
The attached patch fixes this issue, and updates the tests. Contrary to
my initial impression, it seems that a previous developer knew of this
behavior and thought it correct; see the comment of the test I deleted.
I left memcmp() in.
Patch and tests work for me. Uploaded a patch that is identical except
the file paths are fixed.
Was the old behavior stable across compilers anyway? It memcmpared two
different structs and IIRC only the first item of each struct is
guaranteed to be at the start of the memory location. No?
With this patch only same-struct objects are memcmpared.
There is another inconsistency that this patch does not seem to cure. With patch applied and D and DT defined as in OP,
>>> D(1900,1,1) > DT(1900,1,1)
True
but
>>> DT(1900,1,1) < D(1900,1,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare DT to D
and
>>> date(1900,1,1) < datetime(1900,1,1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare datetime.datetime to datetime.date
Note that without the patch,
>>> D(1900,1,1) > DT(1900,1,1)
False
but both behaviors seem to be wrong.
AlexanderBelopolsky commented on Apr 21, 2010
AlexanderBelopolskymannequin
Mannequin
More actions
Upon further reflection I am -1 on this patch. First, as implemented this patch changes behavior of an explicit invocation of date.__eq__. The patch proposes to remove the following tests:
# Neverthelss, comparison should work with the base-class (date)
Second, the patch introduces dependence of the baseclass method (date_richcompare) on a particular subclass (PyDateTime_Check). This is against OOP principles.
I am not sure how the "equality not symmetric" issue can be fixed. In my opinion current datetime implementation is fighting OOP and violating the substitution principle in an attempt to prevent date to datetime comparison. I would prefer seeing one of two things: either datetime not inheriting from date or making datetime to date comparison compare date part alone. Once you stop fighting OOP principles, symmetry of equality for subclasses will follow from that for the base class automatically.
Given that either of these solutions means a major change, I think it is best to leave the things as they are now.
jessaustin commented on Apr 21, 2010
jessaustinmannequin
MannequinAuthor
More actions
To be systematic, without the patch:
>>> D(1900, 1, 1) > DT(1900, 1, 1)
False
>>> D(1900, 1, 1) < DT(1900, 1, 1)
False
>>> DT(1900, 1, 1) > D(1900, 1, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare DT to D
>>> DT(1900, 1, 1) < D(1900, 1, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare DT to D
withthepatch:
>>> D(1900, 1, 1) > DT(1900, 1, 1)
True
>>> D(1900, 1, 1) < DT(1900, 1, 1)
False
>>> DT(1900, 1, 1) > D(1900, 1, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare DT to D
>>> DT(1900, 1, 1) < D(1900, 1, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare DT to D
It might seem like the latter behavior is marginally better, but really this is just a mess, since a date-datetime comparison TypeErrors in all directions. I appreciate Alexander's more experienced perspective, but it's not obvious to me that this problem is insoluble simply due to OOP algebra. I'm going to keep tinkering with this to see if there isn't a way to satisfy his concerns AND fix these bugs WITHOUT breaking the established (and admittedly anti-OOP) behavior that dates are not equal to datetimes.
(Incidentally, the test I removed still seems to be an arbitrary ad-hoc requirement that subclasses of date behave differently than date itself. I don't see how one could rely on this given the other inconsistencies with date subclasses, and so violating this in order to fix more serious problems seems acceptable.)
I'm reminded of the set and frozenset situation, which seems almost dual to this one. set and frozenset don't inherit from each other, but they do compare. This seems to bite you only when you try to redefine comparison in subclasses.
It might seem like the latter behavior is marginally better, but really this is just a mess, since a date-datetime comparison TypeErrors in all
directions. I appreciate Alexander's more experienced perspective, but it's not obvious to me that this problem is insoluble simply due to OOP
algebra. I'm going to keep tinkering with this to see if there isn't a way to satisfy his concerns AND fix these bugs WITHOUT breaking the
established (and admittedly anti-OOP) behavior that dates are not equal to datetimes.
I certainly don't have a proof that this is impossible, so best of
luck. Note, however that the problematic behavior is due to D/DT
classes implementor's choice not to derive DT from D. Whether
resulting violation of the symmetry of equality is a bug in python or
D/DT implementation is at least an open question.
Python 3.13.0a0 (heads/main:ecc05e23a1, Jul 28 2023, 20:21:40) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import date, datetime, time
>>> d = date.today()
>>> dt = datetime.combine(d, time())
>>> d == dt
False
>>> dt == d
False
>>> class D(date): pass
...
>>> class DT(datetime): pass
...
>>> d = D.today()
>>> dt = DT.combine(d, time())
>>> d == dt
True
>>> dt == d
False
>>>
@ncoghlan, @ericsnowcurrently, you participated in this discussion, can you take a look at #114760? It does not only fixes this issue, it makes the code simpler.
Activity
jessaustin commentedon Mar 19, 2009
While the datetime.date and datetime.datetime classes consistently
handle mixed-type comparison, their subclasses do not:
I think this is due to the premature "optimization" of using memcmp() in
date_richcompare().
[-]equality not reflixive for subclasses of datetime.date and datetime.datetime[/-][+]equality not reflexive for subclasses of datetime.date and datetime.datetime[/+][-]equality not reflexive for subclasses of datetime.date and datetime.datetime[/-][+]equality not symmetric for subclasses of datetime.date and datetime.datetime[/+]jessaustin commentedon Mar 23, 2009
The attached patch fixes this issue, and updates the tests. Contrary to
my initial impression, it seems that a previous developer knew of this
behavior and thought it correct; see the comment of the test I deleted.
I left memcmp() in.
jackdied commentedon Mar 26, 2009
+1
Patch and tests work for me. Uploaded a patch that is identical except
the file paths are fixed.
Was the old behavior stable across compilers anyway? It memcmpared two
different structs and IIRC only the first item of each struct is
guaranteed to be at the start of the memory location. No?
With this patch only same-struct objects are memcmpared.
AlexanderBelopolsky commentedon Apr 21, 2010
There is another inconsistency that this patch does not seem to cure. With patch applied and D and DT defined as in OP,
but
and
Note that without the patch,
but both behaviors seem to be wrong.
AlexanderBelopolsky commentedon Apr 21, 2010
Upon further reflection I am -1 on this patch. First, as implemented this patch changes behavior of an explicit invocation of date.__eq__. The patch proposes to remove the following tests:
Second, the patch introduces dependence of the baseclass method (date_richcompare) on a particular subclass (PyDateTime_Check). This is against OOP principles.
I am not sure how the "equality not symmetric" issue can be fixed. In my opinion current datetime implementation is fighting OOP and violating the substitution principle in an attempt to prevent date to datetime comparison. I would prefer seeing one of two things: either datetime not inheriting from date or making datetime to date comparison compare date part alone. Once you stop fighting OOP principles, symmetry of equality for subclasses will follow from that for the base class automatically.
Given that either of these solutions means a major change, I think it is best to leave the things as they are now.
jessaustin commentedon Apr 21, 2010
To be systematic, without the patch:
It might seem like the latter behavior is marginally better, but really this is just a mess, since a date-datetime comparison TypeErrors in all directions. I appreciate Alexander's more experienced perspective, but it's not obvious to me that this problem is insoluble simply due to OOP algebra. I'm going to keep tinkering with this to see if there isn't a way to satisfy his concerns AND fix these bugs WITHOUT breaking the established (and admittedly anti-OOP) behavior that dates are not equal to datetimes.
(Incidentally, the test I removed still seems to be an arbitrary ad-hoc requirement that subclasses of date behave differently than date itself. I don't see how one could rely on this given the other inconsistencies with date subclasses, and so violating this in order to fix more serious problems seems acceptable.)
I'm reminded of the set and frozenset situation, which seems almost dual to this one. set and frozenset don't inherit from each other, but they do compare. This seems to bite you only when you try to redefine comparison in subclasses.
AlexanderBelopolsky commentedon Apr 21, 2010
On Wed, Apr 21, 2010 at 2:33 PM, Jess Austin <report@bugs.python.org> wrote:
..
I certainly don't have a proof that this is impossible, so best of
luck. Note, however that the problematic behavior is due to D/DT
classes implementor's choice not to derive DT from D. Whether
resulting violation of the symmetry of equality is a bug in python or
D/DT implementation is at least an open question.
ncoghlan commentedon Apr 28, 2010
I think I'll concur with the "this is a mess" assessment.
Given that state of affairs, punting on this until 3.2 (at the earliest).
abalkin commentedon May 26, 2010
I am leaning towards "won't fix". Any objections?
13 remaining items
iritkatriel commentedon Jul 29, 2023
Reproduced on 3.13:
pythongh-49766: Make date-datetime comparison more symmetric and flex…
serhiy-storchaka commentedon Feb 3, 2024
@ncoghlan, @ericsnowcurrently, you participated in this discussion, can you take a look at #114760? It does not only fixes this issue, it makes the code simpler.
gh-49766: Make date-datetime comparison more symmetric and flexible (G…
serhiy-storchaka commentedon Feb 11, 2024
It may be too serious change for backporting.
pythongh-49766: Make date-datetime comparison more symmetric and flex…