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 manual adjustments. Also add the pyupgrade hook to pre-commit to avoid merging additional Python 2 syntaxes. Change-Id: I14cd7c88346bdfb5d4b2afe2b4a71f84833f3688
This commit is contained in:
parent
a9ac427304
commit
de05ceca5f
@ -1,6 +1,6 @@
|
||||
repos:
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.5.0
|
||||
rev: v5.0.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
# Replaces or checks mixed line ending
|
||||
@ -19,12 +19,17 @@ repos:
|
||||
- id: check-yaml
|
||||
files: .*\.(yaml|yml)$
|
||||
- repo: https://opendev.org/openstack/hacking
|
||||
rev: 6.1.0
|
||||
rev: 7.0.0
|
||||
hooks:
|
||||
- id: hacking
|
||||
additional_dependencies: []
|
||||
- repo: https://github.com/PyCQA/bandit
|
||||
rev: 1.7.6
|
||||
rev: 1.7.10
|
||||
hooks:
|
||||
- id: bandit
|
||||
args: ['-x', 'tests']
|
||||
- repo: https://github.com/asottile/pyupgrade
|
||||
rev: v3.18.0
|
||||
hooks:
|
||||
- id: pyupgrade
|
||||
args: [--py3-only]
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2020 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2020 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -159,9 +159,9 @@ class Cache:
|
||||
filename = os.path.join(self._dir, digest)
|
||||
try:
|
||||
log.debug('reading %s', filename)
|
||||
with open(filename, 'r') as f:
|
||||
with open(filename) as f:
|
||||
data = json.load(f)
|
||||
except (IOError, json.JSONDecodeError):
|
||||
except (OSError, json.JSONDecodeError):
|
||||
data = _build_cacheable_data()
|
||||
data['path_values'] = path_values
|
||||
if not self._disable_caching:
|
||||
@ -170,7 +170,7 @@ class Cache:
|
||||
os.makedirs(self._dir, exist_ok=True)
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(data, f)
|
||||
except (IOError, OSError):
|
||||
except OSError:
|
||||
# Could not create cache dir or write file.
|
||||
pass
|
||||
|
||||
|
@ -157,7 +157,7 @@ class NameDispatchExtensionManager(DispatchExtensionManager):
|
||||
propagate_map_exceptions=False,
|
||||
on_load_failure_callback=None,
|
||||
verify_requirements=False):
|
||||
super(NameDispatchExtensionManager, self).__init__(
|
||||
super().__init__(
|
||||
namespace=namespace,
|
||||
check_func=check_func,
|
||||
invoke_on_load=invoke_on_load,
|
||||
@ -169,8 +169,8 @@ class NameDispatchExtensionManager(DispatchExtensionManager):
|
||||
)
|
||||
|
||||
def _init_plugins(self, extensions):
|
||||
super(NameDispatchExtensionManager, self)._init_plugins(extensions)
|
||||
self.by_name = dict((e.name, e) for e in self.extensions)
|
||||
super()._init_plugins(extensions)
|
||||
self.by_name = {e.name: e for e in self.extensions}
|
||||
|
||||
def map(self, names, func, *args, **kwds):
|
||||
"""Iterate over the extensions invoking func() for any where
|
||||
|
@ -51,7 +51,7 @@ class DriverManager(NamedExtensionManager):
|
||||
warn_on_missing_entrypoint=True):
|
||||
on_load_failure_callback = on_load_failure_callback \
|
||||
or self._default_on_load_failure
|
||||
super(DriverManager, self).__init__(
|
||||
super().__init__(
|
||||
namespace=namespace,
|
||||
names=[name],
|
||||
invoke_on_load=invoke_on_load,
|
||||
@ -98,7 +98,7 @@ class DriverManager(NamedExtensionManager):
|
||||
|
||||
"""
|
||||
|
||||
o = super(DriverManager, cls).make_test_instance(
|
||||
o = super().make_test_instance(
|
||||
[extension], namespace=namespace,
|
||||
propagate_map_exceptions=propagate_map_exceptions,
|
||||
on_load_failure_callback=on_load_failure_callback,
|
||||
@ -106,7 +106,7 @@ class DriverManager(NamedExtensionManager):
|
||||
return o
|
||||
|
||||
def _init_plugins(self, extensions):
|
||||
super(DriverManager, self)._init_plugins(extensions)
|
||||
super()._init_plugins(extensions)
|
||||
|
||||
if not self.extensions:
|
||||
name = self._names[0]
|
||||
|
@ -62,7 +62,7 @@ class EnabledExtensionManager(ExtensionManager):
|
||||
on_load_failure_callback=None,
|
||||
verify_requirements=False,):
|
||||
self.check_func = check_func
|
||||
super(EnabledExtensionManager, self).__init__(
|
||||
super().__init__(
|
||||
namespace,
|
||||
invoke_on_load=invoke_on_load,
|
||||
invoke_args=invoke_args,
|
||||
@ -74,7 +74,7 @@ class EnabledExtensionManager(ExtensionManager):
|
||||
|
||||
def _load_one_plugin(self, ep, invoke_on_load, invoke_args, invoke_kwds,
|
||||
verify_requirements):
|
||||
ext = super(EnabledExtensionManager, self)._load_one_plugin(
|
||||
ext = super()._load_one_plugin(
|
||||
ep, invoke_on_load, invoke_args, invoke_kwds,
|
||||
verify_requirements,
|
||||
)
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2020 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -45,7 +45,7 @@ if __name__ == '__main__':
|
||||
results = mgr.map(format_data, data)
|
||||
|
||||
for name, result in results:
|
||||
print('Formatter: {0}'.format(name))
|
||||
print('Formatter: {}'.format(name))
|
||||
for chunk in result:
|
||||
print(chunk, end='')
|
||||
print('')
|
||||
|
@ -1,4 +1,3 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (C) 2020 Red Hat, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -22,7 +22,7 @@ from .exception import NoMatches
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Extension(object):
|
||||
class Extension:
|
||||
"""Book-keeping object for tracking extensions.
|
||||
|
||||
The arguments passed to the constructor are saved as attributes of
|
||||
@ -74,7 +74,7 @@ class Extension(object):
|
||||
return self.entry_point.value
|
||||
|
||||
|
||||
class ExtensionManager(object):
|
||||
class ExtensionManager:
|
||||
"""Base class for all of the other managers.
|
||||
|
||||
:param namespace: The namespace for the entry points.
|
||||
|
@ -56,7 +56,7 @@ class HookManager(NamedExtensionManager):
|
||||
# base class because for hooks it is less likely to
|
||||
# be an error to have no entry points present.
|
||||
warn_on_missing_entrypoint=False):
|
||||
super(HookManager, self).__init__(
|
||||
super().__init__(
|
||||
namespace,
|
||||
[name],
|
||||
invoke_on_load=invoke_on_load,
|
||||
@ -71,7 +71,7 @@ class HookManager(NamedExtensionManager):
|
||||
def _init_attributes(self, namespace, names, name_order=False,
|
||||
propagate_map_exceptions=False,
|
||||
on_load_failure_callback=None):
|
||||
super(HookManager, self)._init_attributes(
|
||||
super()._init_attributes(
|
||||
namespace, names,
|
||||
propagate_map_exceptions=propagate_map_exceptions,
|
||||
on_load_failure_callback=on_load_failure_callback)
|
||||
|
@ -79,7 +79,7 @@ class NamedExtensionManager(ExtensionManager):
|
||||
invoke_args,
|
||||
invoke_kwds,
|
||||
verify_requirements)
|
||||
self._missing_names = set(names) - set([e.name for e in extensions])
|
||||
self._missing_names = set(names) - {e.name for e in extensions}
|
||||
if self._missing_names:
|
||||
if on_missing_entrypoints_callback:
|
||||
on_missing_entrypoints_callback(self._missing_names)
|
||||
@ -131,7 +131,7 @@ class NamedExtensionManager(ExtensionManager):
|
||||
def _init_attributes(self, namespace, names, name_order=False,
|
||||
propagate_map_exceptions=False,
|
||||
on_load_failure_callback=None):
|
||||
super(NamedExtensionManager, self)._init_attributes(
|
||||
super()._init_attributes(
|
||||
namespace, propagate_map_exceptions=propagate_map_exceptions,
|
||||
on_load_failure_callback=on_load_failure_callback)
|
||||
|
||||
@ -140,7 +140,7 @@ class NamedExtensionManager(ExtensionManager):
|
||||
self._name_order = name_order
|
||||
|
||||
def _init_plugins(self, extensions):
|
||||
super(NamedExtensionManager, self)._init_plugins(extensions)
|
||||
super()._init_plugins(extensions)
|
||||
|
||||
if self._name_order:
|
||||
self.extensions = [self[n] for n in self._names
|
||||
@ -153,7 +153,7 @@ class NamedExtensionManager(ExtensionManager):
|
||||
# going to use it.
|
||||
if ep.name not in self._names:
|
||||
return None
|
||||
return super(NamedExtensionManager, self)._load_one_plugin(
|
||||
return super()._load_one_plugin(
|
||||
ep, invoke_on_load, invoke_args, invoke_kwds,
|
||||
verify_requirements,
|
||||
)
|
||||
|
@ -33,7 +33,7 @@ def _simple_list(mgr):
|
||||
ext = mgr[name]
|
||||
doc = _get_docstring(ext.plugin) or '\n'
|
||||
summary = doc.splitlines()[0].strip()
|
||||
yield ('* %s -- %s' % (ext.name, summary),
|
||||
yield ('* {} -- {}'.format(ext.name, summary),
|
||||
ext.module_name)
|
||||
|
||||
|
||||
@ -82,7 +82,7 @@ class ListPluginsDirective(rst.Directive):
|
||||
underline_style = self.options.get('underline-style', '=')
|
||||
|
||||
def report_load_failure(mgr, ep, err):
|
||||
LOG.warning(u'Failed to load %s: %s' % (ep.module, err))
|
||||
LOG.warning('Failed to load {}: {}'.format(ep.module, err))
|
||||
|
||||
mgr = extension.ExtensionManager(
|
||||
namespace,
|
||||
|
@ -53,11 +53,11 @@ class TestExtensionManager(extension.ExtensionManager):
|
||||
invoke_on_load=False,
|
||||
invoke_args=(),
|
||||
invoke_kwds={}):
|
||||
super(TestExtensionManager, self).__init__(namespace,
|
||||
invoke_on_load,
|
||||
invoke_args,
|
||||
invoke_kwds,
|
||||
)
|
||||
super().__init__(namespace,
|
||||
invoke_on_load,
|
||||
invoke_args,
|
||||
invoke_kwds,
|
||||
)
|
||||
self.extensions = extensions
|
||||
warnings.warn(
|
||||
'TestExtesionManager has been replaced by make_test_instance()',
|
||||
|
@ -34,7 +34,7 @@ class TestDispatch(utils.TestCase):
|
||||
invoke_kwds={'b': 'B'},
|
||||
)
|
||||
self.assertEqual(len(em.extensions), 2)
|
||||
self.assertEqual(set(em.names()), set(['t1', 't2']))
|
||||
self.assertEqual(set(em.names()), {'t1', 't2'})
|
||||
|
||||
results = em.map(check_dispatch,
|
||||
invoke,
|
||||
@ -67,7 +67,7 @@ class TestDispatch(utils.TestCase):
|
||||
invoke_kwds={'b': 'B'},
|
||||
)
|
||||
self.assertEqual(len(em.extensions), 2)
|
||||
self.assertEqual(set(em.names()), set(['t1', 't2']))
|
||||
self.assertEqual(set(em.names()), {'t1', 't2'})
|
||||
|
||||
results = em.map(['t2'], invoke, 'first', named='named value',)
|
||||
expected = [('t2', ('first',), {'named': 'named value'})]
|
||||
|
@ -26,7 +26,7 @@ ALL_NAMES = ['e1', 't1', 't2']
|
||||
WORKING_NAMES = ['t1', 't2']
|
||||
|
||||
|
||||
class FauxExtension(object):
|
||||
class FauxExtension:
|
||||
def __init__(self, *args, **kwds):
|
||||
self.args = args
|
||||
self.kwds = kwds
|
||||
@ -35,9 +35,9 @@ class FauxExtension(object):
|
||||
return self.args, self.kwds, data
|
||||
|
||||
|
||||
class BrokenExtension(object):
|
||||
class BrokenExtension:
|
||||
def __init__(self, *args, **kwds):
|
||||
raise IOError("Did not create")
|
||||
raise OSError("Did not create")
|
||||
|
||||
|
||||
class TestCallback(utils.TestCase):
|
||||
@ -54,14 +54,14 @@ class TestCallback(utils.TestCase):
|
||||
def test_list_entry_points(self):
|
||||
em = extension.ExtensionManager('stevedore.test.extension')
|
||||
n = em.list_entry_points()
|
||||
self.assertEqual(set(['e1', 'e2', 't1', 't2']),
|
||||
self.assertEqual({'e1', 'e2', 't1', 't2'},
|
||||
set(map(operator.attrgetter("name"), n)))
|
||||
self.assertEqual(4, len(n))
|
||||
|
||||
def test_list_entry_points_names(self):
|
||||
em = extension.ExtensionManager('stevedore.test.extension')
|
||||
names = em.entry_points_names()
|
||||
self.assertEqual(set(['e1', 'e2', 't1', 't2']), set(names))
|
||||
self.assertEqual({'e1', 'e2', 't1', 't2'}, set(names))
|
||||
self.assertEqual(4, len(names))
|
||||
|
||||
def test_contains_by_name(self):
|
||||
@ -196,11 +196,11 @@ class TestCallback(utils.TestCase):
|
||||
)
|
||||
|
||||
result = em.map_method('get_args_and_data', 42)
|
||||
self.assertEqual(set(r[2] for r in result), set([42]))
|
||||
self.assertEqual({r[2] for r in result}, {42})
|
||||
|
||||
def test_items(self):
|
||||
em = extension.ExtensionManager('stevedore.test.extension')
|
||||
expected_output = set([(name, em[name]) for name in ALL_NAMES])
|
||||
expected_output = {(name, em[name]) for name in ALL_NAMES}
|
||||
self.assertEqual(expected_output, set(em.items()))
|
||||
|
||||
|
||||
@ -208,7 +208,7 @@ class TestLoadRequirementsNewSetuptools(utils.TestCase):
|
||||
# setuptools 11.3 and later
|
||||
|
||||
def setUp(self):
|
||||
super(TestLoadRequirementsNewSetuptools, self).setUp()
|
||||
super().setUp()
|
||||
self.mock_ep = mock.Mock(spec=['require', 'resolve', 'load', 'name'])
|
||||
self.em = extension.ExtensionManager.make_test_instance([])
|
||||
|
||||
@ -229,7 +229,7 @@ class TestLoadRequirementsOldSetuptools(utils.TestCase):
|
||||
# Before setuptools 11.3
|
||||
|
||||
def setUp(self):
|
||||
super(TestLoadRequirementsOldSetuptools, self).setUp()
|
||||
super().setUp()
|
||||
self.mock_ep = mock.Mock(spec=['load', 'name'])
|
||||
self.em = extension.ExtensionManager.make_test_instance([])
|
||||
|
||||
|
@ -33,7 +33,7 @@ def _make_ext(name, docstring):
|
||||
class TestSphinxExt(utils.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestSphinxExt, self).setUp()
|
||||
super().setUp()
|
||||
self.exts = [
|
||||
_make_ext('test1', 'One-line docstring'),
|
||||
_make_ext('test2', 'Multi-line docstring\n\nAnother para'),
|
||||
|
Loading…
x
Reference in New Issue
Block a user