Issue 2789: Comparsion datetime objects with None (original) (raw)

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 "", line 1, in TypeError: can't compare datetime.date to NoneType None < dt.datetime.today() Traceback (most recent call last): File "", line 1, in TypeError: can't compare datetime.datetime to NoneType None < dt.time() Traceback (most recent call last): File "", line 1, in TypeError: can't compare datetime.time to NoneType None < dt.timedelta() Traceback (most recent call last): File "", line 1, in TypeError: can't compare datetime.timedelta to NoneType

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.