Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff update #3206

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ repos:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: 'https://github.com/asottile/pyupgrade'
rev: v3.15.0
hooks:
- id: pyupgrade
args:
- '--py38-plus'
- repo: 'https://github.com/PyCQA/isort'
rev: 5.12.0
hooks:
- id: isort
- repo: 'https://github.com/psf/black'
rev: 23.11.0
hooks:
- id: black
- repo: 'https://github.com/pycqa/flake8'
rev: 6.1.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
hooks:
- id: flake8
- id: ruff
args: [ --fix ]
- id: ruff-format
15 changes: 6 additions & 9 deletions CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ Reporting An Issue/Feature

Codestyle
---------
This project uses flake8 to enforce codstyle requirements. We've codified this
process using a tool called `pre-commit <https://pre-commit.com/>`__. pre-commit
allows us to specify a config file with all tools required for code linting,
and surfaces either a git commit hook, or single command, for enforcing these.
This project uses `ruff <https://github.com/astral-sh/ruff>`__ to enforce
codstyle requirements. We've codified this process using a tool called
`pre-commit <https://pre-commit.com/>`__. pre-commit allows us to specify a
config file with all tools required for code linting, and surfaces either a
git commit hook, or single command, for enforcing these.

To validate your PR prior to publishing, you can use the following
`installation guide <https://pre-commit.com/#install>`__ to setup pre-commit.
Expand All @@ -88,11 +89,7 @@ to automatically perform the codestyle validation:
$ pre-commit run

This will automatically perform simple updates (such as white space clean up)
and provide a list of any failing flake8 checks. After these are addressed,
and provide a list of any failing checks. After these are addressed,
you can commit the changes prior to publishing the PR.
These checks are also included in our CI setup under the "Lint" workflow which
will provide output on Github for anything missed locally.

See the `flake8` section of the
`setup.cfg <https://github.com/boto/botocore/blob/develop/setup.cfg>`__ for the
currently enforced rules.
3 changes: 2 additions & 1 deletion botocore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
considered internal, and *not* a public API.

"""

import copy
import logging
import socket
Expand Down Expand Up @@ -460,7 +461,7 @@ def _get_sts_regional_endpoints_config(self):

def _set_global_sts_endpoint(self, endpoint_config, is_secure):
scheme = 'https' if is_secure else 'http'
endpoint_config['endpoint_url'] = '%s://sts.amazonaws.com' % scheme
endpoint_config['endpoint_url'] = f'{scheme}://sts.amazonaws.com'
endpoint_config['signing_region'] = 'us-east-1'

def _resolve_endpoint(
Expand Down
12 changes: 6 additions & 6 deletions botocore/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,12 @@ def add_auth(self, request):
self._inject_signature_to_request(request, signature)

def _inject_signature_to_request(self, request, signature):
auth_str = ['AWS4-HMAC-SHA256 Credential=%s' % self.scope(request)]
auth_str = [f'AWS4-HMAC-SHA256 Credential={self.scope(request)}']
headers_to_sign = self.headers_to_sign(request)
auth_str.append(
f"SignedHeaders={self.signed_headers(headers_to_sign)}"
)
auth_str.append('Signature=%s' % signature)
auth_str.append(f'Signature={signature}')
request.headers['Authorization'] = ', '.join(auth_str)
return request

Expand Down Expand Up @@ -685,7 +685,7 @@ def _inject_signature_to_request(self, request, signature):
# Rather than calculating an "Authorization" header, for the query
# param quth, we just append an 'X-Amz-Signature' param to the end
# of the query string.
request.url += '&X-Amz-Signature=%s' % signature
request.url += f'&X-Amz-Signature={signature}'

def _normalize_url_path(self, path):
# For S3, we do not normalize the path.
Expand Down Expand Up @@ -777,7 +777,7 @@ def _inject_signature_to_request(self, request, signature):
# Rather than calculating an "Authorization" header, for the query
# param quth, we just append an 'X-Amz-Signature' param to the end
# of the query string.
request.url += '&X-Amz-Signature=%s' % signature
request.url += f'&X-Amz-Signature={signature}'


class S3SigV4QueryAuth(SigV4QueryAuth):
Expand Down Expand Up @@ -990,15 +990,15 @@ def get_signature(
string_to_sign = self.canonical_string(
method, split, headers, auth_path=auth_path
)
logger.debug('StringToSign:\n%s', string_to_sign)
logger.debug(f'StringToSign:\n{string_to_sign}')
return self.sign_string(string_to_sign)

def add_auth(self, request):
if self.credentials is None:
raise NoCredentialsError
logger.debug("Calculating signature using hmacv1 auth.")
split = urlsplit(request.url)
logger.debug('HTTP request method: %s', request.method)
logger.debug(f'HTTP request method: {request.method}')
signature = self.get_signature(
request.method, split, request.headers, auth_path=request.auth_path
)
Expand Down
4 changes: 2 additions & 2 deletions botocore/awsrequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ def prepare_request_dict(
percent_encode_sequence = botocore.utils.percent_encode_sequence
encoded_query_string = percent_encode_sequence(r['query_string'])
if '?' not in url:
url += '?%s' % encoded_query_string
url += f'?{encoded_query_string}'
else:
url += '&%s' % encoded_query_string
url += f'&{encoded_query_string}'
r['url'] = url
r['context'] = context
if context is None:
Expand Down
27 changes: 10 additions & 17 deletions botocore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _create_client_class(self, service_name, service_model):
bases = [BaseClient]
service_id = service_model.service_id.hyphenize()
self._event_emitter.emit(
'creating-client-class.%s' % service_id,
f'creating-client-class.{service_id}',
class_attributes=class_attributes,
base_classes=bases,
)
Expand All @@ -223,10 +223,10 @@ def _normalize_fips_region(self, region_name, client_config):
else:
client_config = config_use_fips_endpoint
logger.warning(
'transforming region from %s to %s and setting '
f'transforming region from {region_name} to '
f'{normalized_region_name} and setting '
'use_fips_endpoint to true. client should not '
'be configured with a fips psuedo region.'
% (region_name, normalized_region_name)
)
region_name = normalized_region_name
return region_name, client_config
Expand Down Expand Up @@ -289,7 +289,7 @@ def _register_legacy_retries(self, client):
handler = self._retry_handler_factory.create_retry_handler(
retry_config, endpoint_prefix
)
unique_id = 'retry-config-%s' % service_event_name
unique_id = f'retry-config-{service_event_name}'
client.meta.events.register(
f"needs-retry.{service_event_name}", handler, unique_id=unique_id
)
Expand Down Expand Up @@ -573,7 +573,7 @@ def _api_call(self, *args, **kwargs):
method_name=operation_name,
event_emitter=self._event_emitter,
method_description=operation_model.documentation,
example_prefix='response = client.%s' % py_operation_name,
example_prefix=f'response = client.{py_operation_name}',
include_signature=False,
)
_api_call.__doc__ = docstring
Expand Down Expand Up @@ -982,9 +982,7 @@ def _make_api_call(self, operation_name, api_params):

service_id = self._service_model.service_id.hyphenize()
handler, event_response = self.meta.events.emit_until_response(
'before-call.{service_id}.{operation_name}'.format(
service_id=service_id, operation_name=operation_name
),
f'before-call.{service_id}.{operation_name}',
model=operation_model,
params=request_dict,
request_signer=self._request_signer,
Expand All @@ -1003,9 +1001,7 @@ def _make_api_call(self, operation_name, api_params):
)

self.meta.events.emit(
'after-call.{service_id}.{operation_name}'.format(
service_id=service_id, operation_name=operation_name
),
f'after-call.{service_id}.{operation_name}',
http_response=http,
parsed=parsed_response,
model=operation_model,
Expand All @@ -1027,10 +1023,7 @@ def _make_request(self, operation_model, request_dict, request_context):
return self._endpoint.make_request(operation_model, request_dict)
except Exception as e:
self.meta.events.emit(
'after-call-error.{service_id}.{operation_name}'.format(
service_id=self._service_model.service_id.hyphenize(),
operation_name=operation_model.name,
),
f'after-call-error.{self._service_model.service_id.hyphenize()}.{operation_model.name}',
exception=e,
context=request_context,
)
Expand Down Expand Up @@ -1259,13 +1252,13 @@ def get_waiter(self, waiter_name):
"""
config = self._get_waiter_config()
if not config:
raise ValueError("Waiter does not exist: %s" % waiter_name)
raise ValueError(f"Waiter does not exist: {waiter_name}")
model = waiter.WaiterModel(config)
mapping = {}
for name in model.waiter_names:
mapping[xform_name(name)] = name
if waiter_name not in mapping:
raise ValueError("Waiter does not exist: %s" % waiter_name)
raise ValueError(f"Waiter does not exist: {waiter_name}")

return waiter.create_waiter_with_client(
mapping[waiter_name], model, self
Expand Down
15 changes: 5 additions & 10 deletions botocore/configprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""This module contains the interface for controlling how configuration
is loaded.
"""

import copy
import logging
import os
Expand Down Expand Up @@ -697,7 +698,7 @@ def _convert_type(self, value):
return value

def __repr__(self):
return '[%s]' % ', '.join([str(p) for p in self._providers])
return '[{}]'.format(', '.join([str(p) for p in self._providers]))


class InstanceVarProvider(BaseProvider):
Expand Down Expand Up @@ -728,10 +729,7 @@ def provide(self):
return value

def __repr__(self):
return 'InstanceVarProvider(instance_var={}, session={})'.format(
self._instance_var,
self._session,
)
return f'InstanceVarProvider(instance_var={self._instance_var}, session={self._session})'


class ScopedConfigProvider(BaseProvider):
Expand Down Expand Up @@ -767,10 +765,7 @@ def provide(self):
return scoped_config.get(self._config_var_name)

def __repr__(self):
return 'ScopedConfigProvider(config_var_name={}, session={})'.format(
self._config_var_name,
self._session,
)
return f'ScopedConfigProvider(config_var_name={self._config_var_name}, session={self._session})'


class EnvironmentProvider(BaseProvider):
Expand Down Expand Up @@ -878,7 +873,7 @@ def provide(self):
return self._value

def __repr__(self):
return 'ConstantProvider(value=%s)' % self._value
return f'ConstantProvider(value={self._value})'


class ConfiguredEndpointProvider(BaseProvider):
Expand Down
14 changes: 7 additions & 7 deletions botocore/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def __init__(
super().__init__(cache, expiry_window_seconds)

def _generate_assume_role_name(self):
self._role_session_name = 'botocore-session-%s' % (int(time.time()))
self._role_session_name = f'botocore-session-{int(time.time())}'
self._assume_kwargs['RoleSessionName'] = self._role_session_name
self._using_default_session_name = True

Expand Down Expand Up @@ -848,7 +848,7 @@ def _assume_role_kwargs(self):
mfa_serial = assume_role_kwargs.get('SerialNumber')

if mfa_serial is not None:
prompt = 'Enter MFA code for %s: ' % mfa_serial
prompt = f'Enter MFA code for {mfa_serial}: '
token_code = self._mfa_prompter(prompt)
assume_role_kwargs['TokenCode'] = token_code

Expand Down Expand Up @@ -1570,8 +1570,8 @@ def _get_role_config(self, profile_name):
if credential_source is not None and source_profile is not None:
raise InvalidConfigError(
error_msg=(
'The profile "%s" contains both source_profile and '
'credential_source.' % profile_name
f'The profile "{profile_name}" contains both '
'source_profile and credential_source.'
)
)
elif credential_source is None and source_profile is None:
Expand Down Expand Up @@ -1720,7 +1720,7 @@ def _resolve_credentials_from_source(
provider=credential_source,
error_msg=(
'No credentials found in credential_source referenced '
'in profile %s' % profile_name
f'in profile {profile_name}'
),
)
return credentials
Expand Down Expand Up @@ -2242,8 +2242,8 @@ def _load_sso_config(self):
missing = ', '.join(missing_config_vars)
raise InvalidConfigError(
error_msg=(
'The profile "%s" is configured to use SSO but is missing '
'required configuration: %s' % (profile_name, missing)
f'The profile "{profile_name}" is configured to use SSO '
f'but is missing required configuration: {missing}'
)
)
return config
Expand Down
9 changes: 4 additions & 5 deletions botocore/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ def describe_endpoint(self, **kwargs):
if not self._always_discover and not discovery_required:
# Discovery set to only run on required operations
logger.debug(
'Optional discovery disabled. Skipping discovery for Operation: %s'
% operation
f'Optional discovery disabled. Skipping discovery for Operation: {operation}'
)
return None

Expand Down Expand Up @@ -228,12 +227,12 @@ def __init__(self, manager):

def register(self, events, service_id):
events.register(
'before-parameter-build.%s' % service_id, self.gather_identifiers
f'before-parameter-build.{service_id}', self.gather_identifiers
)
events.register_first(
'request-created.%s' % service_id, self.discover_endpoint
f'request-created.{service_id}', self.discover_endpoint
)
events.register('needs-retry.%s' % service_id, self.handle_retries)
events.register(f'needs-retry.{service_id}', self.handle_retries)

def gather_identifiers(self, params, model, context, **kwargs):
endpoint_discovery = model.endpoint_discovery
Expand Down
10 changes: 5 additions & 5 deletions botocore/docs/bcdoc/docstringparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def add_tag(self, tag, attrs=None, is_start=True):

def _doc_has_handler(self, tag, is_start):
if is_start:
handler_name = 'start_%s' % tag
handler_name = f'start_{tag}'
else:
handler_name = 'end_%s' % tag
handler_name = f'end_{tag}'

return hasattr(self.doc.style, handler_name)

Expand Down Expand Up @@ -226,12 +226,12 @@ def collapse_whitespace(self):
child.collapse_whitespace()

def _write_start(self, doc):
handler_name = 'start_%s' % self.tag
handler_name = f'start_{self.tag}'
if hasattr(doc.style, handler_name):
getattr(doc.style, handler_name)(self.attrs)

def _write_end(self, doc, next_child):
handler_name = 'end_%s' % self.tag
handler_name = f'end_{self.tag}'
if hasattr(doc.style, handler_name):
if handler_name == 'end_a':
# We use lookahead to determine if a space is needed after a link node
Expand All @@ -248,7 +248,7 @@ class DataNode(Node):
def __init__(self, data, parent=None):
super().__init__(parent)
if not isinstance(data, str):
raise ValueError("Expecting string type, %s given." % type(data))
raise ValueError(f"Expecting string type, {type(data)} given.")
self._leading_whitespace = ''
self._trailing_whitespace = ''
self._stripped_data = ''
Expand Down
Loading
Loading