This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: date-datetime comparison
Type: Stage:
Components: Library (Lib) Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: tim.peters Nosy List: donnal, tim.peters
Priority: normal Keywords:

Created on 2004-09-15 02:00 by donnal, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg22447 - (view) Author: Donnal Walter (donnal) Date: 2004-09-15 02:00
> I was surprised to discover that
> 
> >>> import datetime
> >>> x = datetime.date(2004, 9, 14)
> >>> y = datetime.datetime(2004, 9, 14, 6, 43, 15)
> >>> print x == y
> True
> 
> How can these two objects be considered equal?

They should not be.  Please open a bug report.  The 
problem is due to that datetime.datetime is a subclass 
of datetime.date:

>>> isinstance(y, datetime.date) True
>>>

and date's comparison implementation believes that 
instances of date subclasses can be compared as if 
they *were* dates.  Indeed, since a datetime.datetime 
is-a datetime.date, it's a bit hard to see why that 
shouldn't be allowed, and offhand I don't know of a 
principled way to fix this without breaking existing code 
that compares instances of user-defined subclasses of 
datetime.date to instances of datetime.date.
msg22448 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-09-16 01:32
Logged In: YES 
user_id=31435

Thanks for the report.  Comparing a date to a datetime now 
acts like a mixed-type comparison; user-defined subclasses 
aren't affected; see NEWS for details.

Lib/test/test_datetime.py 1.49
Misc/NEWS 1.1135
Modules/datetimemodule.c 1.75
History
Date User Action Args
2022-04-11 14:56:07adminsetgithub: 40911
2004-09-15 02:00:13donnalcreate