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: Comparsion datetime objects with None
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: schmir, shura_zam, terry.reedy
Priority: normal Keywords: patch

Created on 2008-05-08 04:17 by shura_zam, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
datetimemodule.diff shura_zam, 2008-05-08 04:17 Patch for correct from rev62827
Messages (2)
msg66402 - (view) Author: Alexandr Zamaraev (shura_zam) Date: 2008-05-08 04:17
When comparing any standard objects with None returned True or False.
When comparing the object of the module datetime with None raised
exception TypeError.
Example Python session:
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime as dt
>>> import decimal as dc
>>> None == 1
False
>>> None == "s"
False
>>> None == [1, 2, 3]
False
>>> None == (1, 2, 3)
False
>>> None == dc.Decimal()
False
>>> None == dt.date.today()
False
>>> None == dt.datetime.today()
False
>>> None == dt.time()
False
>>> None == dt.timedelta()
False
>>> None < 1
True
>>> None < "s"
True
>>> None < [1, 2, 3]
True
>>> None < (1, 2, 3)
True
>>> None < dc.Decimal()
True
>>> None < dt.date.today()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare datetime.date to NoneType
>>> None < dt.datetime.today()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare datetime.datetime to NoneType
>>> None < dt.time()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare datetime.time to NoneType
>>> None < dt.timedelta()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't compare datetime.timedelta to NoneType
msg66508 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-05-10 03:03
You actually have it backwards ;-)  The arbitrary responses to
nonsensical cross-class order comparisons found in 1.x and 2.x are now
considered a design bug.  They are disappearing in 3.0.

Datetime is a fairly recent module (new in 2.3) written to the new
standard.  The number 1, for instance, in the same comparisons, raises
the same exceptions.  And the manual specifies this.  For instance, the
time class section on comparison explicitly says: "In order to stop
mixed-type comparisons from falling back to the default comparison by
object address, when a time object is compared to an object of a
different type, TypeError is raised unless the comparison is == or !=."

Closing as invalid.

As indicated in the quote, equality comparisons are different from order
comparisons.  None really is not equal to anything else.  This is still
true in 3.0.
History
Date User Action Args
2022-04-11 14:56:34adminsetgithub: 47038
2008-05-10 03:03:24terry.reedysetstatus: open -> closed
resolution: not a bug
messages: + msg66508
nosy: + terry.reedy
2008-05-09 09:08:12schmirsetnosy: + schmir
2008-05-08 04:17:16shura_zamcreate