Skip to content

Commit

Permalink
python 3.12 changes (#328)
Browse files Browse the repository at this point in the history
* python 3.12 changes

* datetime.UTC to datetime.timezone.utc

* replace(tzinfo=None) to make sure results same as previous
  • Loading branch information
sameer-google authored Oct 23, 2024
1 parent dbffcca commit f0dfa4e
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 348 deletions.
66 changes: 33 additions & 33 deletions apitools/base/protorpclite/descriptor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class MyEnum(messages.Enum):

described = descriptor.describe_enum_value(MyEnum.MY_NAME)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)


class DescribeEnumTest(test_util.TestCase):
Expand All @@ -63,7 +63,7 @@ class EmptyEnum(messages.Enum):

described = descriptor.describe_enum(EmptyEnum)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testNestedEnum(self):
class MyScope(messages.Message):
Expand All @@ -76,7 +76,7 @@ class NestedEnum(messages.Enum):

described = descriptor.describe_enum(MyScope.NestedEnum)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

@unittest.skipIf('PyPy' in platform.python_implementation(),
'todo: reenable this')
Expand Down Expand Up @@ -105,7 +105,7 @@ class EnumWithItems(messages.Enum):

described = descriptor.describe_enum(EnumWithItems)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)


class DescribeFieldTest(test_util.TestCase):
Expand All @@ -127,7 +127,7 @@ def testLabel(self):

described = descriptor.describe_field(field)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testDefault(self):
test_cases = (
Expand All @@ -154,7 +154,7 @@ def testDefault(self):

described = descriptor.describe_field(field)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testDefault_EnumField(self):
class MyEnum(messages.Enum):
Expand All @@ -174,7 +174,7 @@ class MyEnum(messages.Enum):
expected.default_value = '1'

described = descriptor.describe_field(field)
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testMessageField(self):
field = messages.MessageField(descriptor.FieldDescriptor, 10)
Expand All @@ -190,7 +190,7 @@ def testMessageField(self):

described = descriptor.describe_field(field)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testDateTimeField(self):
field = message_types.DateTimeField(20)
Expand All @@ -206,7 +206,7 @@ def testDateTimeField(self):

described = descriptor.describe_field(field)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)


class DescribeMessageTest(test_util.TestCase):
Expand All @@ -220,7 +220,7 @@ class MyMessage(messages.Message):

described = descriptor.describe_message(MyMessage)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testDefinitionWithFields(self):
class MessageWithFields(messages.Message):
Expand All @@ -242,7 +242,7 @@ class MessageWithFields(messages.Message):

described = descriptor.describe_message(MessageWithFields)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testNestedEnum(self):
class MessageWithEnum(messages.Message):
Expand All @@ -265,7 +265,7 @@ class Music(messages.Enum):

described = descriptor.describe_message(MessageWithEnum)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testNestedMessage(self):
class MessageWithMessage(messages.Message):
Expand All @@ -281,7 +281,7 @@ class Nesty(messages.Message):

described = descriptor.describe_message(MessageWithMessage)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)


class DescribeFileTest(test_util.TestCase):
Expand Down Expand Up @@ -309,7 +309,7 @@ def testEmptyModule(self):

described = descriptor.describe_file(module)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testNoPackageName(self):
"""Test describing a module with no module name."""
Expand All @@ -319,7 +319,7 @@ def testNoPackageName(self):

described = descriptor.describe_file(module)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testPackageName(self):
"""Test using the 'package' module attribute."""
Expand All @@ -331,7 +331,7 @@ def testPackageName(self):

described = descriptor.describe_file(module)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testMain(self):
"""Test using the 'package' module attribute."""
Expand All @@ -343,7 +343,7 @@ def testMain(self):

described = descriptor.describe_file(module)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testMessages(self):
"""Test that messages are described."""
Expand All @@ -363,7 +363,7 @@ def testMessages(self):

described = descriptor.describe_file(module)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)

def testEnums(self):
"""Test that enums are described."""
Expand All @@ -383,7 +383,7 @@ def testEnums(self):

described = descriptor.describe_file(module)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)


class DescribeFileSetTest(test_util.TestCase):
Expand All @@ -394,7 +394,7 @@ def testNoModules(self):
described = descriptor.describe_file_set([])
described.check_initialized()
# The described FileSet.files will be None.
self.assertEquals(descriptor.FileSet(), described)
self.assertEqual(descriptor.FileSet(), described)

def testWithModules(self):
"""Test what happens when no modules provided."""
Expand All @@ -410,32 +410,32 @@ def testWithModules(self):

described = descriptor.describe_file_set(modules)
described.check_initialized()
self.assertEquals(expected, described)
self.assertEqual(expected, described)


class DescribeTest(test_util.TestCase):

def testModule(self):
self.assertEquals(descriptor.describe_file(test_util),
self.assertEqual(descriptor.describe_file(test_util),
descriptor.describe(test_util))

def testField(self):
self.assertEquals(
self.assertEqual(
descriptor.describe_field(test_util.NestedMessage.a_value),
descriptor.describe(test_util.NestedMessage.a_value))

def testEnumValue(self):
self.assertEquals(
self.assertEqual(
descriptor.describe_enum_value(
test_util.OptionalMessage.SimpleEnum.VAL1),
descriptor.describe(test_util.OptionalMessage.SimpleEnum.VAL1))

def testMessage(self):
self.assertEquals(descriptor.describe_message(test_util.NestedMessage),
self.assertEqual(descriptor.describe_message(test_util.NestedMessage),
descriptor.describe(test_util.NestedMessage))

def testEnum(self):
self.assertEquals(
self.assertEqual(
descriptor.describe_enum(test_util.OptionalMessage.SimpleEnum),
descriptor.describe(test_util.OptionalMessage.SimpleEnum))

Expand All @@ -451,25 +451,25 @@ def fn(self):
'string',
1.2,
None):
self.assertEquals(None, descriptor.describe(value))
self.assertEqual(None, descriptor.describe(value))


class ModuleFinderTest(test_util.TestCase):

def testFindMessage(self):
self.assertEquals(
self.assertEqual(
descriptor.describe_message(descriptor.FileSet),
descriptor.import_descriptor_loader(
'apitools.base.protorpclite.descriptor.FileSet'))

def testFindField(self):
self.assertEquals(
self.assertEqual(
descriptor.describe_field(descriptor.FileSet.files),
descriptor.import_descriptor_loader(
'apitools.base.protorpclite.descriptor.FileSet.files'))

def testFindEnumValue(self):
self.assertEquals(
self.assertEqual(
descriptor.describe_enum_value(
test_util.OptionalMessage.SimpleEnum.VAL1),
descriptor.import_descriptor_loader(
Expand All @@ -489,8 +489,8 @@ def setUp(self):
})

def testLookupPackage(self):
self.assertEquals('csv', self.library.lookup_package('csv'))
self.assertEquals(
self.assertEqual('csv', self.library.lookup_package('csv'))
self.assertEqual(
'apitools.base.protorpclite',
self.library.lookup_package('apitools.base.protorpclite'))

Expand All @@ -508,7 +508,7 @@ def testNoPackage(self):
'Could not find definition for not.real',
self.library.lookup_package, 'not.real.Packageless')

self.assertEquals(None, self.library.lookup_package('Packageless'))
self.assertEqual(None, self.library.lookup_package('Packageless'))


if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions apitools/base/protorpclite/message_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def value_from_message(self, message):
"""
message = super(DateTimeField, self).value_from_message(message)
if message.time_zone_offset is None:
return datetime.datetime.utcfromtimestamp(
message.milliseconds / 1000.0)
return datetime.datetime.fromtimestamp(
message.milliseconds / 1000.0, tz=datetime.timezone.utc).replace(tzinfo=None)

# Need to subtract the time zone offset, because when we call
# datetime.fromtimestamp, it will add the time zone offset to the
Expand All @@ -97,7 +97,7 @@ def value_to_message(self, value):
# DateTimeMessage's milliseconds field.
if value.tzinfo is None:
time_zone_offset = 0
local_epoch = datetime.datetime.utcfromtimestamp(0)
local_epoch = datetime.datetime.fromtimestamp(0, tz=datetime.timezone.utc).replace(tzinfo=None)
else:
time_zone_offset = util.total_seconds(
value.tzinfo.utcoffset(value))
Expand Down
Loading

0 comments on commit f0dfa4e

Please sign in to comment.