3636 AllDayEvent , Event , MaybeEvent , Session , SessionEvent , Timestamp ,
3737)
3838
39+ try :
40+ import yaml
41+ HAS_YAML = True
42+ except ImportError :
43+ HAS_YAML = False
44+
3945# These tests use the EAT (Eastern Africa Time) and ICT (Indochina Time)
4046# who don't have Daylight Saving Time, so we can represent them easily
4147# with FixedOffset, and use them directly as tzinfo in the constructors.
@@ -662,9 +668,10 @@ class SerializationTests(SimpleTestCase):
662668
663669 # Backend-specific notes:
664670 # - JSON supports only milliseconds, microseconds will be truncated.
665- # - PyYAML dumps the UTC offset correctly for timezone-aware datetimes,
666- # but when it loads this representation, it subtracts the offset and
667- # returns a naive datetime object in UTC. See ticket #18867.
671+ # - PyYAML dumps the UTC offset correctly for timezone-aware datetimes.
672+ # When PyYAML < 5.3 loads this representation, it subtracts the offset
673+ # and returns a naive datetime object in UTC. PyYAML 5.3+ loads timezones
674+ # correctly.
668675 # Tests are adapted to take these quirks into account.
669676
670677 def assert_python_contains_datetime (self , objects , dt ):
@@ -751,7 +758,10 @@ def test_aware_datetime_with_microsecond(self):
751758 data = serializers .serialize ('yaml' , [Event (dt = dt )], default_flow_style = None )
752759 self .assert_yaml_contains_datetime (data , "2011-09-01 17:20:30.405060+07:00" )
753760 obj = next (serializers .deserialize ('yaml' , data )).object
754- self .assertEqual (obj .dt .replace (tzinfo = UTC ), dt )
761+ if HAS_YAML and yaml .__version__ < '5.3' :
762+ self .assertEqual (obj .dt .replace (tzinfo = UTC ), dt )
763+ else :
764+ self .assertEqual (obj .dt , dt )
755765
756766 def test_aware_datetime_in_utc (self ):
757767 dt = datetime .datetime (2011 , 9 , 1 , 10 , 20 , 30 , tzinfo = UTC )
@@ -799,7 +809,10 @@ def test_aware_datetime_in_local_timezone(self):
799809 data = serializers .serialize ('yaml' , [Event (dt = dt )], default_flow_style = None )
800810 self .assert_yaml_contains_datetime (data , "2011-09-01 13:20:30+03:00" )
801811 obj = next (serializers .deserialize ('yaml' , data )).object
802- self .assertEqual (obj .dt .replace (tzinfo = UTC ), dt )
812+ if HAS_YAML and yaml .__version__ < '5.3' :
813+ self .assertEqual (obj .dt .replace (tzinfo = UTC ), dt )
814+ else :
815+ self .assertEqual (obj .dt , dt )
803816
804817 def test_aware_datetime_in_other_timezone (self ):
805818 dt = datetime .datetime (2011 , 9 , 1 , 17 , 20 , 30 , tzinfo = ICT )
@@ -823,7 +836,10 @@ def test_aware_datetime_in_other_timezone(self):
823836 data = serializers .serialize ('yaml' , [Event (dt = dt )], default_flow_style = None )
824837 self .assert_yaml_contains_datetime (data , "2011-09-01 17:20:30+07:00" )
825838 obj = next (serializers .deserialize ('yaml' , data )).object
826- self .assertEqual (obj .dt .replace (tzinfo = UTC ), dt )
839+ if HAS_YAML and yaml .__version__ < '5.3' :
840+ self .assertEqual (obj .dt .replace (tzinfo = UTC ), dt )
841+ else :
842+ self .assertEqual (obj .dt , dt )
827843
828844
829845@override_settings (DATETIME_FORMAT = 'c' , TIME_ZONE = 'Africa/Nairobi' , USE_L10N = False , USE_TZ = True )
0 commit comments