From 2ba90581d5d68c565347b6c3aa16faf4b6478f2d Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Thu, 29 Aug 2024 10:53:30 +0100 Subject: [PATCH] pre-commit: Migrate from flake8 to ruff Well, mostly. We still keep our own flake8 hooks and the hacking hooks enabled. Everything else can be handled by ruff. Doing this enables a couple of hacking checks that were previously unaddressed. It also highlights a few cases that flake8 missed. Both are addressed. Change-Id: If81c7055e9ef692425da2789bae18a96d04b104f Signed-off-by: Stephen Finucane --- .pre-commit-config.yaml | 5 +++++ openstackclient/common/quota.py | 2 +- openstackclient/compute/v2/flavor.py | 2 +- openstackclient/compute/v2/server_migration.py | 13 +++++++++++-- openstackclient/image/v2/image.py | 14 +++++++------- openstackclient/image/v2/metadef_objects.py | 10 ++++++---- openstackclient/volume/v2/volume_type.py | 8 ++++---- openstackclient/volume/v3/volume.py | 2 +- openstackclient/volume/v3/volume_group.py | 10 +++------- openstackclient/volume/v3/volume_type.py | 12 ++++++------ tox.ini | 8 +++----- 11 files changed, 48 insertions(+), 38 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0088cd060c..3ff6e73356 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -24,6 +24,11 @@ repos: hooks: - id: black args: ['-S', '-l', '79'] + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.6.2 + hooks: + - id: ruff + args: ['--fix'] - repo: https://github.com/PyCQA/bandit rev: 1.7.9 hooks: diff --git a/openstackclient/common/quota.py b/openstackclient/common/quota.py index c408e97bac..6d8d0c6fca 100644 --- a/openstackclient/common/quota.py +++ b/openstackclient/common/quota.py @@ -176,7 +176,7 @@ def get_network_quotas( default=False, ): def _network_quota_to_dict(network_quota, detail=False): - if type(network_quota) is not dict: + if not isinstance(network_quota, dict): dict_quota = network_quota.to_dict() else: dict_quota = network_quota diff --git a/openstackclient/compute/v2/flavor.py b/openstackclient/compute/v2/flavor.py index e4df628856..f94b3162e4 100644 --- a/openstackclient/compute/v2/flavor.py +++ b/openstackclient/compute/v2/flavor.py @@ -577,7 +577,7 @@ class UnsetFlavor(command.Command): parsed_args.flavor, get_extra_specs=True, ignore_missing=False ) except sdk_exceptions.ResourceNotFound as e: - raise exceptions.CommandError(_(e.message)) + raise exceptions.CommandError(e.message) result = 0 if parsed_args.properties: diff --git a/openstackclient/compute/v2/server_migration.py b/openstackclient/compute/v2/server_migration.py index 18299156a8..75073a0c33 100644 --- a/openstackclient/compute/v2/server_migration.py +++ b/openstackclient/compute/v2/server_migration.py @@ -253,8 +253,17 @@ def _get_migration_by_uuid(compute_client, server_id, migration_uuid): if migration.uuid == migration_uuid: return migration else: - msg = _('In-progress live migration %s is not found for server %s.') - raise exceptions.CommandError(msg % (migration_uuid, server_id)) + msg = _( + 'In-progress live migration %(migration)s is not found for ' + 'server %(server)s' + ) + raise exceptions.CommandError( + msg + % { + 'migration': migration_uuid, + 'server': server_id, + } + ) class ShowMigration(command.ShowOne): diff --git a/openstackclient/image/v2/image.py b/openstackclient/image/v2/image.py index 46d374e9ec..5390a704ca 100644 --- a/openstackclient/image/v2/image.py +++ b/openstackclient/image/v2/image.py @@ -1614,8 +1614,6 @@ class ImportImage(command.ShowOne): metavar='', help=_('Image to initiate import process for (name or ID)'), ) - # TODO(stephenfin): Uncomment help text when we have this command - # implemented parser.add_argument( '--method', metavar='', @@ -1630,8 +1628,6 @@ class ImportImage(command.ShowOne): help=_( "Import method used for image import process. " "Not all deployments will support all methods. " - # "Valid values can be retrieved with the 'image import " - # "methods' command. " "The 'glance-direct' method (default) requires images be " "first staged using the 'image-stage' command." ), @@ -1734,11 +1730,15 @@ class ImportImage(command.ShowOne): if parsed_args.import_method not in import_methods: msg = _( - "The '%s' import method is not supported by this deployment. " - "Supported: %s" + "The '%(method)s' import method is not supported by this " + "deployment. Supported: %(supported)s" ) raise exceptions.CommandError( - msg % (parsed_args.import_method, ', '.join(import_methods)), + msg + % { + 'method': parsed_args.import_method, + 'supported': ', '.join(import_methods), + }, ) if parsed_args.import_method == 'web-download': diff --git a/openstackclient/image/v2/metadef_objects.py b/openstackclient/image/v2/metadef_objects.py index 2dc33968ad..04c0fa55f1 100644 --- a/openstackclient/image/v2/metadef_objects.py +++ b/openstackclient/image/v2/metadef_objects.py @@ -260,10 +260,12 @@ class ShowMetadefObjectProperty(command.ShowOne): prop['name'] = parsed_args.property except KeyError: - msg = _('Property %s not found in object %s.') % ( - parsed_args.property, - parsed_args.object, - ) + msg = _( + 'Property %(property)s not found in object %(object)s.' + ) % { + 'property': parsed_args.property, + 'object': parsed_args.object, + } raise exceptions.CommandError(msg) return zip(*sorted(prop.items())) diff --git a/openstackclient/volume/v2/volume_type.py b/openstackclient/volume/v2/volume_type.py index f4540f4e85..be57f4b500 100644 --- a/openstackclient/volume/v2/volume_type.py +++ b/openstackclient/volume/v2/volume_type.py @@ -171,7 +171,7 @@ class CreateVolumeType(command.ShowOne): default=False, help=_( "Enabled replication for this volume type " - "(this is an alias for '--property replication_enabled= True') " # noqa: E501 + "(this is an alias for '--property replication_enabled= True') " "(requires driver support)" ), ) @@ -181,7 +181,7 @@ class CreateVolumeType(command.ShowOne): dest='availability_zones', help=_( "Set an availability zone for this volume type " - "(this is an alias for '--property RESKEY:availability_zones:') " # noqa: E501 + "(this is an alias for '--property RESKEY:availability_zones:') " "(repeat option to set multiple availability zones)" ), ) @@ -535,7 +535,7 @@ class SetVolumeType(command.Command): default=False, help=_( "Enabled replication for this volume type " - "(this is an alias for '--property replication_enabled= True') " # noqa: E501 + "(this is an alias for '--property replication_enabled= True') " "(requires driver support)" ), ) @@ -545,7 +545,7 @@ class SetVolumeType(command.Command): dest='availability_zones', help=_( "Set an availability zone for this volume type " - "(this is an alias for '--property RESKEY:availability_zones:') " # noqa: E501 + "(this is an alias for '--property RESKEY:availability_zones:') " "(repeat option to set multiple availability zones)" ), ) diff --git a/openstackclient/volume/v3/volume.py b/openstackclient/volume/v3/volume.py index 3bb3cdaed5..2a3f13d956 100644 --- a/openstackclient/volume/v3/volume.py +++ b/openstackclient/volume/v3/volume.py @@ -154,7 +154,7 @@ class CreateVolume(volume_v2.CreateVolume): "Cinder cluster on which the existing volume resides; " "takes the form: cluster@backend-name#pool. This is only " "used along with the --remote-source option. " - "(supported by --os-volume-api-version 3.16 or above)", + "(supported by --os-volume-api-version 3.16 or above)" ), ) return parser diff --git a/openstackclient/volume/v3/volume_group.py b/openstackclient/volume/v3/volume_group.py index ba9368e013..201e7a6d7e 100644 --- a/openstackclient/volume/v3/volume_group.py +++ b/openstackclient/volume/v3/volume_group.py @@ -288,7 +288,7 @@ class DeleteVolumeGroup(command.Command): default=False, help=_( 'Delete the volume group even if it contains volumes. ' - 'This will delete any remaining volumes in the group.', + 'This will delete any remaining volumes in the group.' ), ) return parser @@ -582,18 +582,14 @@ class FailoverVolumeGroup(command.Command): action='store_true', dest='allow_attached_volume', default=False, - help=_( - 'Allow group with attached volumes to be failed over.', - ), + help=_('Allow group with attached volumes to be failed over.'), ) parser.add_argument( '--disallow-attached-volume', action='store_false', dest='allow_attached_volume', default=False, - help=_( - 'Disallow group with attached volumes to be failed over.', - ), + help=_('Disallow group with attached volumes to be failed over.'), ) parser.add_argument( '--secondary-backend-id', diff --git a/openstackclient/volume/v3/volume_type.py b/openstackclient/volume/v3/volume_type.py index 96d7553d4a..03c3fe2459 100644 --- a/openstackclient/volume/v3/volume_type.py +++ b/openstackclient/volume/v3/volume_type.py @@ -172,7 +172,7 @@ class CreateVolumeType(command.ShowOne): default=False, help=_( "Enabled replication for this volume type " - "(this is an alias for '--property replication_enabled= True') " # noqa: E501 + "(this is an alias for '--property replication_enabled= True') " "(requires driver support)" ), ) @@ -182,7 +182,7 @@ class CreateVolumeType(command.ShowOne): dest='availability_zones', help=_( "Set an availability zone for this volume type " - "(this is an alias for '--property RESKEY:availability_zones:') " # noqa: E501 + "(this is an alias for '--property RESKEY:availability_zones:') " "(repeat option to set multiple availability zones)" ), ) @@ -448,7 +448,7 @@ class ListVolumeType(command.Lister): default=False, help=_( "List only volume types with replication enabled " - "(this is an alias for '--property replication_enabled= True') " # noqa: E501 + "(this is an alias for '--property replication_enabled= True') " "(supported by --os-volume-api-version 3.52 or above)" ), ) @@ -458,7 +458,7 @@ class ListVolumeType(command.Lister): dest='availability_zones', help=_( "List only volume types with this availability configured " - "(this is an alias for '--property RESKEY:availability_zones:') " # noqa: E501 + "(this is an alias for '--property RESKEY:availability_zones:') " "(repeat option to filter on multiple availability zones)" ), ) @@ -617,7 +617,7 @@ class SetVolumeType(command.Command): default=False, help=_( "Enabled replication for this volume type " - "(this is an alias for '--property replication_enabled= True') " # noqa: E501 + "(this is an alias for '--property replication_enabled= True') " "(requires driver support)" ), ) @@ -627,7 +627,7 @@ class SetVolumeType(command.Command): dest='availability_zones', help=_( "Set an availability zone for this volume type " - "(this is an alias for '--property RESKEY:availability_zones:') " # noqa: E501 + "(this is an alias for '--property RESKEY:availability_zones:') " "(repeat option to set multiple availability zones)" ), ) diff --git a/tox.ini b/tox.ini index e7c84c5110..7b3d951d20 100644 --- a/tox.ini +++ b/tox.ini @@ -113,11 +113,9 @@ commands = [flake8] show-source = true exclude = .venv,.git,.tox,dist,doc,*lib/python*,*egg,build,tools,releasenotes -# E203 Black will put spaces after colons in list comprehensions -# E501 Black takes care of line length for us -# E704 Black will occasionally put multiple statements on one line +# We only enable the hacking (H) checks +select = H # H301 Black will put commas after imports that can't fit on one line -# W503 and W504 are disabled since they're not very useful -ignore = E203, E501, E701, H301, W503, W504 +ignore = H301 import-order-style = pep8 application_import_names = openstackclient