@@ -3151,6 +3151,48 @@ def fromutc(self, dt):
31513151 fstart += HOUR
31523152
31533153
3154+ #############################################################################
3155+ # oddballs
3156+
3157+ class Oddballs (unittest .TestCase ):
3158+
3159+ def test_bug_1028306 (self ):
3160+ # Trying to compare a date to a datetime should act like a mixed-
3161+ # type comparison, despite that datetime is a subclass of date.
3162+ as_date = date .today ()
3163+ as_datetime = datetime .combine (as_date , time ())
3164+ self .assert_ (as_date != as_datetime )
3165+ self .assert_ (as_datetime != as_date )
3166+ self .assert_ (not as_date == as_datetime )
3167+ self .assert_ (not as_datetime == as_date )
3168+ self .assertRaises (TypeError , lambda : as_date < as_datetime )
3169+ self .assertRaises (TypeError , lambda : as_datetime < as_date )
3170+ self .assertRaises (TypeError , lambda : as_date <= as_datetime )
3171+ self .assertRaises (TypeError , lambda : as_datetime <= as_date )
3172+ self .assertRaises (TypeError , lambda : as_date > as_datetime )
3173+ self .assertRaises (TypeError , lambda : as_datetime > as_date )
3174+ self .assertRaises (TypeError , lambda : as_date >= as_datetime )
3175+ self .assertRaises (TypeError , lambda : as_datetime >= as_date )
3176+
3177+ # Neverthelss, comparison should work with the base-class (date)
3178+ # projection if use of a date method is forced.
3179+ self .assert_ (as_date .__eq__ (as_datetime ))
3180+ different_day = (as_date .day + 1 ) % 20 + 1
3181+ self .assert_ (not as_date .__eq__ (as_datetime .replace (day =
3182+ different_day )))
3183+
3184+ # And date should compare with other subclasses of date. If a
3185+ # subclass wants to stop this, it's up to the subclass to do so.
3186+ date_sc = SubclassDate (as_date .year , as_date .month , as_date .day )
3187+ self .assertEqual (as_date , date_sc )
3188+ self .assertEqual (date_sc , as_date )
3189+
3190+ # Ditto for datetimes.
3191+ datetime_sc = SubclassDatetime (as_datetime .year , as_datetime .month ,
3192+ as_date .day , 0 , 0 , 0 )
3193+ self .assertEqual (as_datetime , datetime_sc )
3194+ self .assertEqual (datetime_sc , as_datetime )
3195+
31543196def test_suite ():
31553197 allsuites = [unittest .makeSuite (klass , 'test' )
31563198 for klass in (TestModule ,
@@ -3163,6 +3205,7 @@ def test_suite():
31633205 TestTimeTZ ,
31643206 TestDateTimeTZ ,
31653207 TestTimezoneConversions ,
3208+ Oddballs ,
31663209 )
31673210 ]
31683211 return unittest .TestSuite (allsuites )
0 commit comments