Skip to content

Commit e70c5a2

Browse files
authored
Use f-strings instead of string concatenation (#4675)
1 parent f2bd57c commit e70c5a2

File tree

9 files changed

+20
-26
lines changed

9 files changed

+20
-26
lines changed

boto3/docs/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def generate_docs(root_dir, session):
4545
service_name, session, services_doc_path
4646
).document_service()
4747
service_doc_path = os.path.join(
48-
services_doc_path, service_name + '.rst'
48+
services_doc_path, f"{service_name}.rst"
4949
)
5050
with open(service_doc_path, 'wb') as f:
5151
f.write(docs)

boto3/docs/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _document_resources(self, section):
190190

191191
def _get_example_file(self):
192192
return os.path.realpath(
193-
os.path.join(self.EXAMPLE_PATH, self._service_name + '.rst')
193+
os.path.join(self.EXAMPLE_PATH, f"{self._service_name}.rst")
194194
)
195195

196196
def _document_examples(self, section):

boto3/dynamodb/conditions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,10 @@ def __init__(self):
311311
self._value_placeholder = 'v'
312312

313313
def _get_name_placeholder(self):
314-
return '#' + self._name_placeholder + str(self._name_count)
314+
return f"#{self._name_placeholder}{self._name_count}"
315315

316316
def _get_value_placeholder(self):
317-
return ':' + self._value_placeholder + str(self._value_count)
317+
return f":{self._value_placeholder}{self._value_count}"
318318

319319
def reset(self):
320320
"""Resets the placeholder name and values"""
@@ -451,7 +451,7 @@ def _build_value_placeholder(
451451
# Assuming the values are grouped by parenthesis.
452452
# IN is the currently the only one that uses this so it maybe
453453
# needed to be changed in future.
454-
return '(' + ', '.join(placeholder_list) + ')'
454+
return f"({', '.join(placeholder_list)})"
455455
# Otherwise, treat the value as a single value that needs only
456456
# one placeholder.
457457
else:

boto3/resources/base.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(self, *args, **kwargs):
104104
# Allow setting identifiers as positional arguments in the order
105105
# in which they were defined in the ResourceJSON.
106106
for i, value in enumerate(args):
107-
setattr(self, '_' + self.meta.identifiers[i], value)
107+
setattr(self, f"_{self.meta.identifiers[i]}", value)
108108

109109
# Allow setting identifiers via keyword arguments. Here we need
110110
# extra logic to ignore other keyword arguments like ``client``.
@@ -115,23 +115,19 @@ def __init__(self, *args, **kwargs):
115115
if name not in self.meta.identifiers:
116116
raise ValueError(f'Unknown keyword argument: {name}')
117117

118-
setattr(self, '_' + name, value)
118+
setattr(self, f"_{name}", value)
119119

120120
# Validate that all identifiers have been set.
121121
for identifier in self.meta.identifiers:
122122
if getattr(self, identifier) is None:
123123
raise ValueError(f'Required parameter {identifier} not set')
124124

125125
def __repr__(self):
126-
identifiers = []
127-
for identifier in self.meta.identifiers:
128-
identifiers.append(
129-
f'{identifier}={repr(getattr(self, identifier))}'
130-
)
131-
return "{}({})".format(
132-
self.__class__.__name__,
133-
', '.join(identifiers),
134-
)
126+
identifiers = [
127+
f'{identifier}={repr(getattr(self, identifier))}'
128+
for identifier in self.meta.identifiers
129+
]
130+
return f"{self.__class__.__name__}({', '.join(identifiers)})"
135131

136132
def __eq__(self, other):
137133
# Should be instances of the same resource class

boto3/resources/factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def load_from_definition(
145145
cls_name = resource_name
146146
if service_context.service_name == resource_name:
147147
cls_name = 'ServiceResource'
148-
cls_name = service_context.service_name + '.' + cls_name
148+
cls_name = f"{service_context.service_name}.{cls_name}"
149149

150150
base_classes = [ServiceResource]
151151
if self._emitter is not None:
@@ -325,7 +325,7 @@ def get_identifier(self):
325325
# identifiers have a value ``None``. If any are ``None``,
326326
# a more informative user error than a generic AttributeError
327327
# is raised.
328-
return getattr(self, '_' + identifier.name, None)
328+
return getattr(self, f"_{identifier.name}", None)
329329

330330
get_identifier.__name__ = str(identifier.name)
331331
get_identifier.__doc__ = docstring.IdentifierDocstring(
@@ -344,7 +344,7 @@ def _create_identifier_alias(
344344
"""
345345

346346
def get_identifier(self):
347-
return getattr(self, '_' + identifier.name, None)
347+
return getattr(self, f"_{identifier.name}", None)
348348

349349
get_identifier.__name__ = str(identifier.member_name)
350350
get_identifier.__doc__ = docstring.AttributeDocstring(

boto3/resources/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ def _load_name_with_category(self, names, name, category, snake_case=True):
367367

368368
if name in names:
369369
logger.debug('Renaming %s %s %s', self.name, category, name)
370-
self._renamed[(category, name)] = name + '_' + category
371-
name += '_' + category
370+
self._renamed[(category, name)] = f"{name}_{category}"
371+
name += f"_{category}"
372372

373373
if name in names:
374374
# This isn't good, let's raise instead of trying to keep

boto3/resources/params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def build_param_structure(params, target, value, index=None):
132132
else:
133133
# We have an explicit index
134134
index = int(result.group(1))
135-
part = part[: -len(str(index) + '[]')]
135+
part = part[: -len(f"{index}[]")]
136136
else:
137137
# Index will be set after we know the proper part
138138
# name and that it's a list instance.

boto3/s3/transfer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,7 @@ def upload_file(
456456
# client error.
457457
except ClientError as e:
458458
raise S3UploadFailedError(
459-
"Failed to upload {} to {}: {}".format(
460-
filename, '/'.join([bucket, key]), e
461-
)
459+
f"Failed to upload {filename} to {bucket}/{key}: {e}"
462460
)
463461

464462
def download_file(

boto3/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(
7272
if self._session.user_agent_name == 'Botocore':
7373
botocore_info = f'Botocore/{self._session.user_agent_version}'
7474
if self._session.user_agent_extra:
75-
self._session.user_agent_extra += ' ' + botocore_info
75+
self._session.user_agent_extra += f" {botocore_info}"
7676
else:
7777
self._session.user_agent_extra = botocore_info
7878
self._session.user_agent_name = 'Boto3'

0 commit comments

Comments
 (0)