Run pyupgrade to clean up Python 2 syntaxes

Update all .py source files by
 $ pyupgrade --py3-only $(git ls-files | grep ".py$")
to modernize the code according to Python 3 syntaxes.

Also add the pyupgrade hook to pre-commit to avoid merging additional
Python 2 syntaxes.

Change-Id: I9fe7d07fe6184833e1fbb228efc7d7308430782a
This commit is contained in:
Takashi Kajinami 2024-10-19 23:29:47 +09:00
parent 704e5713df
commit 8f15fe900f
13 changed files with 25 additions and 22 deletions

View File

@ -23,3 +23,8 @@ repos:
hooks:
- id: bandit
args: ['-x', 'tests']
- repo: https://github.com/asottile/pyupgrade
rev: v3.18.0
hooks:
- id: pyupgrade
args: [--py3-only]

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2020 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -32,7 +32,7 @@ __all__ = [
CONTEXT_SEPARATOR = _message.CONTEXT_SEPARATOR
class TranslatorFactory(object):
class TranslatorFactory:
"Create translator functions"
def __init__(self, domain, localedir=None):
@ -110,7 +110,7 @@ class TranslatorFactory(object):
return _message.Message(msgid, domain=domain,
has_contextual_form=True)
msgctx = "%s%s%s" % (ctx, CONTEXT_SEPARATOR, msg)
msgctx = "{}{}{}".format(ctx, CONTEXT_SEPARATOR, msg)
s = m(msgctx)
if CONTEXT_SEPARATOR in s:
# Translation not found

View File

@ -58,7 +58,7 @@ class Message(str):
msgtext = Message._translate_msgid(msgid, domain)
# We want to initialize the parent unicode with the actual object that
# would have been plain unicode if 'Message' was not enabled.
msg = super(Message, cls).__new__(cls, msgtext)
msg = super().__new__(cls, msgtext)
msg.msgid = msgid
msg.domain = domain
msg.params = params
@ -127,7 +127,7 @@ class Message(str):
(msgctx, msgtxt) = msgid
translator = lang.gettext
msg_with_ctx = "%s%s%s" % (msgctx, CONTEXT_SEPARATOR, msgtxt)
msg_with_ctx = "{}{}{}".format(msgctx, CONTEXT_SEPARATOR, msgtxt)
translated_message = translator(msg_with_ctx)
if CONTEXT_SEPARATOR in translated_message:

View File

@ -65,7 +65,7 @@ def translate_args(args, desired_locale=None):
if isinstance(args, tuple):
return tuple(translate(v, desired_locale) for v in args)
if isinstance(args, dict):
translated_dict = dict((key, translate(value, desired_locale))
for key, value in args.items())
translated_dict = {key: translate(value, desired_locale)
for key, value in args.items()}
return translated_dict
return translate(args, desired_locale)

View File

@ -77,12 +77,12 @@ class ToggleLazy(fixtures.Fixture):
lazy translation, passed to :func:`~oslo_i18n.enable_lazy`.
:type enabled: bool
"""
super(ToggleLazy, self).__init__()
super().__init__()
self._enabled = enabled
self._original_value = _lazy.USE_LAZY
def setUp(self):
super(ToggleLazy, self).setUp()
super().setUp()
self.addCleanup(self._restore_original)
_lazy.enable_lazy(self._enabled)
@ -145,12 +145,12 @@ class PrefixLazyTranslation(fixtures.Fixture):
_DEFAULT_LANG = 'en_US'
def __init__(self, languages=None, locale=None):
super(PrefixLazyTranslation, self).__init__()
super().__init__()
self.languages = languages or [PrefixLazyTranslation._DEFAULT_LANG]
self.locale = locale
def setUp(self):
super(PrefixLazyTranslation, self).setUp()
super().setUp()
self.useFixture(ToggleLazy(True))
self.useFixture(fixtures.MonkeyPatch(
'oslo_i18n._gettextutils.get_available_languages',

View File

@ -29,14 +29,14 @@ CONTEXT_SEPARATOR = _message.CONTEXT_SEPARATOR
class TranslatorFactoryTest(test_base.BaseTestCase):
def setUp(self):
super(TranslatorFactoryTest, self).setUp()
super().setUp()
# remember so we can reset to it later in case it changes
self._USE_LAZY = _lazy.USE_LAZY
def tearDown(self):
# reset to value before test
_lazy.USE_LAZY = self._USE_LAZY
super(TranslatorFactoryTest, self).tearDown()
super().tearDown()
def test_lazy(self):
_lazy.enable_lazy(True)

View File

@ -26,7 +26,7 @@ from oslo_i18n import fixture
class TranslationFixtureTest(test_base.BaseTestCase):
def setUp(self):
super(TranslationFixtureTest, self).setUp()
super().setUp()
self.trans_fixture = self.useFixture(fixture.Translation())
def test_lazy(self):

View File

@ -33,7 +33,7 @@ LOG = logging.getLogger(__name__)
class GettextTest(test_base.BaseTestCase):
def setUp(self):
super(GettextTest, self).setUp()
super().setUp()
# remember so we can reset to it later in case it changes
self._USE_LAZY = _lazy.USE_LAZY
self.t = _factory.TranslatorFactory('oslo_i18n.test')
@ -41,7 +41,7 @@ class GettextTest(test_base.BaseTestCase):
def tearDown(self):
# reset to value before test
_lazy.USE_LAZY = self._USE_LAZY
super(GettextTest, self).tearDown()
super().tearDown()
def test_gettext_does_not_blow_up(self):
LOG.info(self.t.primary('test'))

View File

@ -30,7 +30,7 @@ LOG = logging.getLogger(__name__)
class TranslationHandlerTestCase(test_base.BaseTestCase):
def setUp(self):
super(TranslationHandlerTestCase, self).setUp()
super().setUp()
self.stream = io.StringIO()
self.destination_handler = logging.StreamHandler(self.stream)

View File

@ -22,12 +22,12 @@ from oslo_i18n import _lazy
class LazyTest(test_base.BaseTestCase):
def setUp(self):
super(LazyTest, self).setUp()
super().setUp()
self._USE_LAZY = _lazy.USE_LAZY
def tearDown(self):
_lazy.USE_LAZY = self._USE_LAZY
super(LazyTest, self).tearDown()
super().tearDown()
def test_enable_lazy(self):
_lazy.USE_LAZY = False

View File

@ -13,7 +13,7 @@
# under the License.
class SomeObject(object):
class SomeObject:
def __init__(self, message):
self.message = message
@ -22,7 +22,7 @@ class SomeObject(object):
return self.message
class NoDeepCopyObject(object):
class NoDeepCopyObject:
def __init__(self, value):
self.value = value

View File

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at