Skip to content

Commit

Permalink
pre-commit: Migrate pyupgrade to ruff
Browse files Browse the repository at this point in the history
Change-Id: Ic50d2a5e0bc9dcdfe29f382607135cab510cd396
Signed-off-by: Stephen Finucane <[email protected]>
  • Loading branch information
stephenfin committed Sep 12, 2024
1 parent fc6852c commit f98006c
Show file tree
Hide file tree
Showing 79 changed files with 753 additions and 1,190 deletions.
7 changes: 1 addition & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ repos:
- id: check-yaml
files: .*\.(yaml|yml)$
args: ['--unsafe']
- repo: https://github.com/asottile/pyupgrade
rev: v3.17.0
hooks:
- id: pyupgrade
args: ['--py38-plus']
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.2
hooks:
- id: ruff
args: ['--fix']
args: ['--fix', '--unsafe-fixes']
- id: ruff-format
- repo: https://opendev.org/openstack/hacking
rev: 7.0.0
Expand Down
6 changes: 3 additions & 3 deletions examples/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ def run(opts):

# Do some basic testing here
sys.stdout.write("Default run command\n")
sys.stdout.write("Verbose level: %s\n" % opts.verbose_level)
sys.stdout.write("Debug: %s\n" % opts.debug)
sys.stdout.write("dump_stack_trace: %s\n" % dump_stack_trace)
sys.stdout.write(f"Verbose level: {opts.verbose_level}\n")
sys.stdout.write(f"Debug: {opts.debug}\n")
sys.stdout.write(f"dump_stack_trace: {dump_stack_trace}\n")


def setup():
Expand Down
2 changes: 1 addition & 1 deletion examples/object_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def run(opts):
o_list = obj_api.object_list(c_list[0]['name'])
print("\nObject")
for o in o_list:
print("%s" % o)
print(f"{o}")


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions examples/osc-lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ def run(opts):
o_list = client_manager.object_store.object_list(c_list[0]['name'])
print("\nObject")
for o in o_list:
print("%s" % o)
print(f"{o}")

# Look at the compute flavors
flavor_list = client_manager.compute.flavors.list()
print("\nFlavors:")
for f in flavor_list:
print("%s" % f)
print(f"{f}")


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion openstackclient/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def find(
except ks_exceptions.NotFound:
kwargs = {attr: value}
try:
ret = self.find_one("/%s/detail" % (path), **kwargs)
ret = self.find_one(f"/{path}/detail", **kwargs)
except ks_exceptions.NotFound:
msg = _("%s not found") % value
raise exceptions.NotFound(msg)
Expand Down
20 changes: 6 additions & 14 deletions openstackclient/api/object_store_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ def object_create(
# object's name in the container.
object_name_str = name if name else object

full_url = "{}/{}".format(
urllib.parse.quote(container),
urllib.parse.quote(object_name_str),
)
full_url = f"{urllib.parse.quote(container)}/{urllib.parse.quote(object_name_str)}"
with open(object, 'rb') as f:
response = self.create(
full_url,
Expand Down Expand Up @@ -293,8 +290,7 @@ def object_delete(
return

self.delete(
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object))
f"{urllib.parse.quote(container)}/{urllib.parse.quote(object)}"
)

def object_list(
Expand Down Expand Up @@ -395,8 +391,7 @@ def object_save(

response = self._request(
'GET',
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
f"{urllib.parse.quote(container)}/{urllib.parse.quote(object)}",
stream=True,
)
if response.status_code == 200:
Expand Down Expand Up @@ -431,8 +426,7 @@ def object_set(
headers = self._set_properties(properties, 'X-Object-Meta-%s')
if headers:
self.create(
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
f"{urllib.parse.quote(container)}/{urllib.parse.quote(object)}",
headers=headers,
)

Expand All @@ -455,8 +449,7 @@ def object_unset(
headers = self._unset_properties(properties, 'X-Remove-Object-Meta-%s')
if headers:
self.create(
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
f"{urllib.parse.quote(container)}/{urllib.parse.quote(object)}",
headers=headers,
)

Expand All @@ -480,8 +473,7 @@ def object_show(

response = self._request(
'HEAD',
"%s/%s"
% (urllib.parse.quote(container), urllib.parse.quote(object)),
f"{urllib.parse.quote(container)}/{urllib.parse.quote(object)}",
)

data = {
Expand Down
6 changes: 3 additions & 3 deletions openstackclient/common/quota.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ def get_parser(self, prog_name):
)
for k, v, h in self._build_options_list():
parser.add_argument(
'--%s' % v,
metavar='<%s>' % v,
f'--{v}',
metavar=f'<{v}>',
dest=k,
type=int,
help=h,
Expand Down Expand Up @@ -590,7 +590,7 @@ def take_action(self, parsed_args):
parsed_args.volume_type
and k in IMPACT_VOLUME_TYPE_QUOTAS
):
k = k + '_%s' % parsed_args.volume_type
k = k + f'_{parsed_args.volume_type}'
volume_kwargs[k] = value

if self.app.client_manager.is_network_endpoint_enabled():
Expand Down
18 changes: 9 additions & 9 deletions openstackclient/compute/v2/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -2189,7 +2189,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -3198,7 +3198,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -3350,7 +3350,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -3555,7 +3555,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -3816,7 +3816,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -4188,7 +4188,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -4584,7 +4584,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down Expand Up @@ -5181,7 +5181,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down
2 changes: 1 addition & 1 deletion openstackclient/compute/v2/server_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stderr.write('\rProgress: %s' % progress)
self.app.stderr.write(f'\rProgress: {progress}')
self.app.stderr.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down
2 changes: 1 addition & 1 deletion openstackclient/compute/v2/server_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_parser(self, prog_name):
def take_action(self, parsed_args):
def _show_progress(progress):
if progress:
self.app.stdout.write('\rProgress: %s' % progress)
self.app.stdout.write(f'\rProgress: {progress}')
self.app.stdout.flush()

compute_client = self.app.client_manager.sdk_connection.compute
Expand Down
2 changes: 1 addition & 1 deletion openstackclient/compute/v2/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def human_readable(self):

class FloatColumn(cliff_columns.FormattableColumn):
def human_readable(self):
return float("%.2f" % self._value)
return float(f"{self._value:.2f}")


def _formatters(project_cache):
Expand Down
2 changes: 1 addition & 1 deletion openstackclient/identity/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def find_service_in_list(service_list, service_id):
if service.id == service_id:
return service
raise exceptions.CommandError(
"No service with a type, name or ID of '%s' exists." % service_id
f"No service with a type, name or ID of '{service_id}' exists."
)


Expand Down
12 changes: 6 additions & 6 deletions openstackclient/image/v2/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,8 @@ def get_parser(self, prog_name):
identity_common.add_project_domain_option_to_parser(parser)
for deadopt in self.deadopts:
parser.add_argument(
"--%s" % deadopt,
metavar="<%s>" % deadopt,
f"--{deadopt}",
metavar=f"<{deadopt}>",
dest=deadopt.replace('-', '_'),
help=argparse.SUPPRESS,
)
Expand Down Expand Up @@ -488,7 +488,7 @@ def _take_action_image(self, parsed_args):
fp = open(parsed_args.filename, 'rb')
except FileNotFoundError:
raise exceptions.CommandError(
'%r is not a valid file' % parsed_args.filename,
f'{parsed_args.filename!r} is not a valid file',
)
else:
fp = get_data_from_stdin()
Expand Down Expand Up @@ -1209,8 +1209,8 @@ def get_parser(self, prog_name):
identity_common.add_project_domain_option_to_parser(parser)
for deadopt in self.deadopts:
parser.add_argument(
"--%s" % deadopt,
metavar="<%s>" % deadopt,
f"--{deadopt}",
metavar=f"<{deadopt}>",
dest=f"dead_{deadopt.replace('-', '_')}",
help=argparse.SUPPRESS,
)
Expand Down Expand Up @@ -1575,7 +1575,7 @@ def take_action(self, parsed_args):
fp = open(parsed_args.filename, 'rb')
except FileNotFoundError:
raise exceptions.CommandError(
'%r is not a valid file' % parsed_args.filename,
f'{parsed_args.filename!r} is not a valid file',
)
else:
fp = get_data_from_stdin()
Expand Down
15 changes: 8 additions & 7 deletions openstackclient/network/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ def enhance_help_nova_network(self, _help):
@staticmethod
def split_help(network_help, compute_help):
return (
"*%(network_qualifier)s:*\n %(network_help)s\n\n"
"*%(compute_qualifier)s:*\n %(compute_help)s"
% dict(
network_qualifier=_("Network version 2"),
network_help=network_help,
compute_qualifier=_("Compute version 2"),
compute_help=compute_help,
"*{network_qualifier}:*\n {network_help}\n\n"
"*{compute_qualifier}:*\n {compute_help}".format(
**dict(
network_qualifier=_("Network version 2"),
network_help=network_help,
compute_qualifier=_("Compute version 2"),
compute_help=compute_help,
)
)
)

Expand Down
8 changes: 2 additions & 6 deletions openstackclient/network/v2/network_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ def take_action(self, parsed_args):
try:
client.add_dhcp_agent_to_network(agent, network)
except Exception:
msg = 'Failed to add {} to {}'.format(
network.name, agent.agent_type
)
msg = f'Failed to add {network.name} to {agent.agent_type}'
exceptions.CommandError(msg)


Expand Down Expand Up @@ -321,9 +319,7 @@ def take_action(self, parsed_args):
try:
client.remove_dhcp_agent_from_network(agent, network)
except Exception:
msg = 'Failed to remove {} to {}'.format(
network.name, agent.agent_type
)
msg = f'Failed to remove {network.name} to {agent.agent_type}'
exceptions.CommandError(msg)


Expand Down
7 changes: 2 additions & 5 deletions openstackclient/network/v2/network_qos_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,7 @@ def _get_item_properties(item, fields):

def _rule_action_call(client, action, rule_type):
rule_type = rule_type.replace('-', '_')
func_name = '{action}_qos_{rule_type}_rule'.format(
action=action,
rule_type=rule_type,
)
func_name = f'{action}_qos_{rule_type}_rule'
return getattr(client, func_name)


Expand Down Expand Up @@ -311,7 +308,7 @@ def take_action(self, parsed_args):
)
rule_type = _find_rule_type(qos, rule_id)
if not rule_type:
raise Exception('Rule %s not found' % rule_id)
raise Exception(f'Rule {rule_id} not found')
_rule_action_call(network_client, ACTION_DELETE, rule_type)(
rule_id, qos.id
)
Expand Down
6 changes: 3 additions & 3 deletions openstackclient/network/v2/port.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ def _prepare_filter_fixed_ips(client_manager, parsed_args):
_subnet = client.find_subnet(
subnet_name_id, ignore_missing=False
)
ips.append('subnet_id=%s' % _subnet.id)
ips.append(f'subnet_id={_subnet.id}')

if 'ip-address' in ip_spec:
ips.append('ip_address=%s' % ip_spec['ip-address'])
ips.append('ip_address={}'.format(ip_spec['ip-address']))

if 'ip-substring' in ip_spec:
ips.append('ip_address_substr=%s' % ip_spec['ip-substring'])
ips.append('ip_address_substr={}'.format(ip_spec['ip-substring']))
return ips


Expand Down
5 changes: 3 additions & 2 deletions openstackclient/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def _load_plugins(self):
key=lambda s: list(map(int, s.split('.'))),
)
self.log.warning(
"%s version %s is not in supported versions: %s"
% (api, version_opt, ', '.join(sorted_versions))
"{} version {} is not in supported versions: {}".format(
api, version_opt, ', '.join(sorted_versions)
)
)

# Command groups deal only with major versions
Expand Down
Loading

0 comments on commit f98006c

Please sign in to comment.