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.

pep8 errors are fixed by
 $ autopep8 --select=E127,E128,E501 --max-line-length 79 -r \
    --in-place os_api_ref

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

Change-Id: I482aa3eb40d7c8cefaba9ea9df5b9d1dcd3827c1
This commit is contained in:
Takashi Kajinami
2025-02-10 22:26:34 +09:00
parent d8731f05eb
commit a378131f87
7 changed files with 24 additions and 18 deletions

View File

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

View File

@@ -86,7 +86,7 @@ def ordered_load(
# numbers) as strings. So that microversion specification of 2.20
# and 2.2 don't get confused.
OrderedLoader.add_constructor(
u'tag:yaml.org,2002:float',
'tag:yaml.org,2002:float',
yaml.constructor.SafeConstructor.construct_yaml_str)
return yaml.load(stream, OrderedLoader)
@@ -198,7 +198,7 @@ class RestMethodDirective(rst.Directive):
# they are still unlikely to occurr and it is way shorter
# than stronger SHAs.
node_hash = hashlib.sha1(str(node).encode('utf-8')).hexdigest()
temp_target = "%s-%s-selector" % (node['target'], node_hash)
temp_target = "{}-{}-selector".format(node['target'], node_hash)
target = nodes.target(ids=[temp_target])
self.state.add_target(temp_target, '', target, lineno)
section += node
@@ -222,9 +222,9 @@ class RestParametersDirective(Table):
lookup = {}
try:
with open(fpath, 'r') as stream:
with open(fpath) as stream:
lookup = ordered_load(stream)
except IOError:
except OSError:
LOG.warning("Parameters file not found, %s", fpath,
location=(self.env.docname, None))
return
@@ -595,10 +595,10 @@ Microversions
def build_mv_item(major, micro, releases):
version = "%d.%d" % (major, micro)
if version in releases:
return '<option value="%s">%s - %s</option>' % (
return '<option value="{}">{} - {}</option>'.format(
version, version, releases[version].capitalize())
else:
return '<option value="%s">%s</option>' % (version, version)
return '<option value="{}">{}</option>'.format(version, version)
def resolve_rest_references(app, doctree):

View File

@@ -41,7 +41,7 @@ class HTTPResponseCodeDirective(Table):
def __init__(self, *args, **kwargs):
self.CODES.update(responses)
super(HTTPResponseCodeDirective, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def _load_status_file(self, fpath):
global HTTP_YAML_CACHE
@@ -50,9 +50,9 @@ class HTTPResponseCodeDirective(Table):
# LOG.info("Fpath: %s" % fpath)
try:
with open(fpath, 'r') as stream:
with open(fpath) as stream:
lookup = yaml.safe_load(stream)
except IOError:
except OSError:
LOG.warning(
"Parameters file %s not found" % fpath,
(self.env.docname, None))
@@ -92,7 +92,8 @@ class HTTPResponseCodeDirective(Table):
if status_type not in self.status_types:
error = self.state_machine.reporter.error(
'Type %s is not one of %s' % (status_type, self.status_types),
'Type {} is not one of {}'.format(
status_type, self.status_types),
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return [error]
@@ -142,7 +143,7 @@ class HTTPResponseCodeDirective(Table):
)
except KeyError:
LOG.warning(
"Could not find %s for code %s" % (reason, code))
"Could not find {} for code {}".format(reason, code))
new_content.append(
(code, self.status_defs[code]['default']))

View File

@@ -62,7 +62,7 @@ class OutputStreamCapture(fixtures.Fixture):
tests.
"""
def setUp(self):
super(OutputStreamCapture, self).setUp()
super().setUp()
if os.environ.get('OS_STDOUT_CAPTURE') in _TRUE_VALUES:
self.out = self.useFixture(fixtures.StringStream('stdout'))
self.useFixture(
@@ -93,7 +93,7 @@ class Timeout(fixtures.Fixture):
"""
def __init__(self, timeout, scaling=1):
super(Timeout, self).__init__()
super().__init__()
try:
self.test_timeout = int(timeout)
except ValueError:
@@ -105,7 +105,7 @@ class Timeout(fixtures.Fixture):
raise ValueError('scaling value must be >= 1')
def setUp(self):
super(Timeout, self).setUp()
super().setUp()
if self.test_timeout > 0:
self.useFixture(fixtures.Timeout(self.test_timeout, gentle=True))
@@ -116,7 +116,7 @@ class TestCase(testtools.TestCase):
def setUp(self):
"""Run before each test method to initialize test environment."""
super(TestCase, self).setUp()
super().setUp()
self.useFixture(Timeout(
os.environ.get('OS_TEST_TIMEOUT', 0)))
self.useFixture(OutputStreamCapture())

View File

@@ -31,7 +31,7 @@ class TestBasicExample(base.TestCase):
@base.with_app(buildername='html', srcdir=base.example_dir('basic'))
def setUp(self, app, status, warning):
super(TestBasicExample, self).setUp()
super().setUp()
self.app = app
self.status = status
self.warning = warning

View File

@@ -32,7 +32,7 @@ class TestMicroversions(base.TestCase):
@base.with_app(buildername='html',
srcdir=base.example_dir('microversions'))
def setUp(self, app, status, warning):
super(TestMicroversions, self).setUp()
super().setUp()
self.app = app
self.app.build()
self.status = status.getvalue()

View File

@@ -33,7 +33,7 @@ class TestWarnings(base.TestCase):
@base.with_app(buildername='html', srcdir=base.example_dir('warnings'))
def setUp(self, app, status, warning):
super(TestWarnings, self).setUp()
super().setUp()
self.app = app
self.app.build()
self.status = status.getvalue()