Skip to content

Commit 33740a9

Browse files
author
Lucas McDonald
committed
m
1 parent bf280e4 commit 33740a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+625
-2144
lines changed

.github/workflows/ci_static_analysis_python.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,4 @@ jobs:
7373
- name: Run static analysis
7474
working-directory: ./DynamoDbEncryption/runtimes/python
7575
run: |
76-
tox -e ruff
77-
tox -e black-check
76+
tox -e lint-check

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/internal/client_to_resource.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ def condition_handler(self, expression_key, request):
2828
condition = request[expression_key]
2929

3030
# This conversion in client_to_resource does not update ExpressionAttributeNames or ExpressionAttributeValues.
31-
# However, resource_to_client condition_handler may add new ExpressionAttributeNames and ExpressionAttributeValues.
32-
# Smithy-generated code expects condition_handlers to return ExpressionAttributeNames and ExpressionAttributeValues,
31+
# However, resource_to_client condition_handler may add new ExpressionAttributeNames and
32+
# ExpressionAttributeValues.
33+
# Smithy-generated code expects condition_handlers to return ExpressionAttributeNames and
34+
# ExpressionAttributeValues,
3335
# expecting empty dicts if there are none.
3436
try:
3537
names = request["ExpressionAttributeNames"]
@@ -88,7 +90,7 @@ def delete_item_request(self, delete_item_request):
8890
if self.delete_table_name:
8991
del out["TableName"]
9092
return out
91-
93+
9294
def delete_item_response(self, delete_item_response):
9395
return self.boto3_converter.DeleteItemOutput(delete_item_response)
9496

@@ -98,7 +100,7 @@ def update_item_request(self, update_item_request):
98100
if self.delete_table_name:
99101
del out["TableName"]
100102
return out
101-
103+
102104
def update_item_response(self, update_item_response):
103105
return self.boto3_converter.UpdateItemOutput(update_item_response)
104106

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/internal/condition_expression_builder.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,23 @@ class InternalDBESDKDynamoDBConditionExpressionBuilder:
1717
def __init__(self):
1818
self._name_count = 0
1919
self._value_count = 0
20-
self._name_placeholder = 'n'
21-
self._value_placeholder = 'v'
20+
self._name_placeholder = "n"
21+
self._value_placeholder = "v"
2222

2323
def _get_name_placeholder(self):
24-
return '#' + self._name_placeholder + str(self._name_count)
24+
return "#" + self._name_placeholder + str(self._name_count)
2525

2626
def _get_value_placeholder(self):
27-
return ':' + self._value_placeholder + str(self._value_count)
27+
return ":" + self._value_placeholder + str(self._value_count)
2828

2929
def reset(self):
3030
"""Resets the placeholder name and values"""
3131
self._name_count = 0
3232
self._value_count = 0
3333

3434
def build_expression(self, condition, is_key_condition=False):
35-
"""Builds the condition expression and the dictionary of placeholders.
35+
"""
36+
Builds the condition expression and the dictionary of placeholders.
3637
3738
:type condition: ConditionBase
3839
:param condition: A condition to be built into a condition expression
@@ -75,7 +76,7 @@ def _build_expression(
7576
):
7677
expression_dict = condition.get_expression()
7778
replaced_values = []
78-
for value in expression_dict['values']:
79+
for value in expression_dict["values"]:
7980
# Build the necessary placeholders for that value.
8081
# Placeholders are built for both attribute names and values.
8182
replaced_value = self._build_expression_component(
@@ -88,9 +89,7 @@ def _build_expression(
8889
replaced_values.append(replaced_value)
8990
# Fill out the expression using the operator and the
9091
# values that have been replaced with placeholders.
91-
return expression_dict['format'].format(
92-
*replaced_values, operator=expression_dict['operator']
93-
)
92+
return expression_dict["format"].format(*replaced_values, operator=expression_dict["operator"])
9493

9594
def _build_expression_component(
9695
self,
@@ -115,27 +114,23 @@ def _build_expression_component(
115114
elif isinstance(value, AttributeBase):
116115
if is_key_condition and not isinstance(value, Key):
117116
raise DynamoDBNeedsKeyConditionError(
118-
f'Attribute object {value.name} is of type {type(value)}. '
119-
f'KeyConditionExpression only supports Attribute objects '
120-
f'of type Key'
117+
f"Attribute object {value.name} is of type {type(value)}. "
118+
f"KeyConditionExpression only supports Attribute objects "
119+
f"of type Key"
121120
)
122-
return self._build_name_placeholder(
123-
value, attribute_name_placeholders
124-
)
121+
return self._build_name_placeholder(value, attribute_name_placeholders)
125122
# If it is anything else, we treat it as a value and thus placeholders
126123
# are needed for the value.
127124
else:
128-
return self._build_value_placeholder(
129-
value, attribute_value_placeholders, has_grouped_values
130-
)
125+
return self._build_value_placeholder(value, attribute_value_placeholders, has_grouped_values)
131126

132127
def _build_name_placeholder(self, value, attribute_name_placeholders):
133128
attribute_name = value.name
134129
# Figure out which parts of the attribute name that needs replacement.
135130
attribute_name_parts = ATTR_NAME_REGEX.findall(attribute_name)
136131

137132
# Add a temporary placeholder for each of these parts.
138-
placeholder_format = ATTR_NAME_REGEX.sub('%s', attribute_name)
133+
placeholder_format = ATTR_NAME_REGEX.sub("%s", attribute_name)
139134
str_format_args = []
140135
for part in attribute_name_parts:
141136
name_placeholder = self._get_name_placeholder()
@@ -146,9 +141,7 @@ def _build_name_placeholder(self, value, attribute_name_placeholders):
146141
# Replace the temporary placeholders with the designated placeholders.
147142
return placeholder_format % tuple(str_format_args)
148143

149-
def _build_value_placeholder(
150-
self, value, attribute_value_placeholders, has_grouped_values=False
151-
):
144+
def _build_value_placeholder(self, value, attribute_value_placeholders, has_grouped_values=False):
152145
# If the values are grouped, we need to add a placeholder for
153146
# each element inside of the actual value.
154147
if has_grouped_values:
@@ -161,7 +154,7 @@ def _build_value_placeholder(
161154
# Assuming the values are grouped by parenthesis.
162155
# IN is the currently the only one that uses this so it maybe
163156
# needed to be changed in future.
164-
return '(' + ', '.join(placeholder_list) + ')'
157+
return "(" + ", ".join(placeholder_list) + ")"
165158
# Otherwise, treat the value as a single value that needs only
166159
# one placeholder.
167160
else:

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/internal/resource_to_client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from aws_cryptography_internal_dynamodb.smithygenerated.com_amazonaws_dynamodb.boto3_conversions import (
44
InternalBoto3DynamoDBFormatConverter,
55
)
6-
from boto3.dynamodb.types import TypeSerializer
76
from boto3.dynamodb.conditions import ConditionExpressionBuilder
7+
from boto3.dynamodb.types import TypeSerializer
88

99

1010
class ResourceShapeToClientShapeConverter:
@@ -43,10 +43,12 @@ def condition_handler(self, expression_key, request):
4343
hasattr(condition_expression, "__module__")
4444
and condition_expression.__module__ == "boto3.dynamodb.conditions"
4545
):
46-
built_condition_expression = self.expression_builder.build_expression(
47-
condition_expression
46+
built_condition_expression = self.expression_builder.build_expression(condition_expression)
47+
return (
48+
built_condition_expression.condition_expression,
49+
built_condition_expression.attribute_name_placeholders,
50+
built_condition_expression.attribute_value_placeholders,
4851
)
49-
return built_condition_expression.condition_expression, built_condition_expression.attribute_name_placeholders, built_condition_expression.attribute_value_placeholders
5052
else:
5153
return condition_expression, existing_expression_attribute_names, existing_expression_attribute_values
5254

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/smithygenerated/aws_cryptography_dbencryptionsdk_dynamodb/aws_sdk_to_dafny.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ def com_amazonaws_dynamodb_AttributeValue(native_input):
2525
"".join(
2626
[
2727
chr(int.from_bytes(pair, "big"))
28-
for pair in zip(
29-
*[iter(native_input["S"].encode("utf-16-be"))] * 2
30-
)
28+
for pair in zip(*[iter(native_input["S"].encode("utf-16-be"))] * 2)
3129
]
3230
)
3331
)
@@ -38,9 +36,7 @@ def com_amazonaws_dynamodb_AttributeValue(native_input):
3836
"".join(
3937
[
4038
chr(int.from_bytes(pair, "big"))
41-
for pair in zip(
42-
*[iter(native_input["N"].encode("utf-16-be"))] * 2
43-
)
39+
for pair in zip(*[iter(native_input["N"].encode("utf-16-be"))] * 2)
4440
]
4541
)
4642
)
@@ -55,9 +51,7 @@ def com_amazonaws_dynamodb_AttributeValue(native_input):
5551
"".join(
5652
[
5753
chr(int.from_bytes(pair, "big"))
58-
for pair in zip(
59-
*[iter(list_element.encode("utf-16-be"))] * 2
60-
)
54+
for pair in zip(*[iter(list_element.encode("utf-16-be"))] * 2)
6155
]
6256
)
6357
)
@@ -73,9 +67,7 @@ def com_amazonaws_dynamodb_AttributeValue(native_input):
7367
"".join(
7468
[
7569
chr(int.from_bytes(pair, "big"))
76-
for pair in zip(
77-
*[iter(list_element.encode("utf-16-be"))] * 2
78-
)
70+
for pair in zip(*[iter(list_element.encode("utf-16-be"))] * 2)
7971
]
8072
)
8173
)
@@ -84,19 +76,14 @@ def com_amazonaws_dynamodb_AttributeValue(native_input):
8476
)
8577
)
8678
elif "BS" in native_input.keys():
87-
AttributeValue_union_value = AttributeValue_BS(
88-
Seq([Seq(list_element) for list_element in native_input["BS"]])
89-
)
79+
AttributeValue_union_value = AttributeValue_BS(Seq([Seq(list_element) for list_element in native_input["BS"]]))
9080
elif "M" in native_input.keys():
9181
AttributeValue_union_value = AttributeValue_M(
9282
Map(
9383
{
9484
Seq(
9585
"".join(
96-
[
97-
chr(int.from_bytes(pair, "big"))
98-
for pair in zip(*[iter(key.encode("utf-16-be"))] * 2)
99-
]
86+
[chr(int.from_bytes(pair, "big")) for pair in zip(*[iter(key.encode("utf-16-be"))] * 2)]
10087
)
10188
): aws_cryptography_internal_dynamodb.smithygenerated.com_amazonaws_dynamodb.aws_sdk_to_dafny.com_amazonaws_dynamodb_AttributeValue(
10289
value
@@ -121,8 +108,6 @@ def com_amazonaws_dynamodb_AttributeValue(native_input):
121108
elif "BOOL" in native_input.keys():
122109
AttributeValue_union_value = AttributeValue_BOOL(native_input["BOOL"])
123110
else:
124-
raise ValueError(
125-
"No recognized union value in union type: " + str(native_input)
126-
)
111+
raise ValueError("No recognized union value in union type: " + str(native_input))
127112

128113
return AttributeValue_union_value

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/smithygenerated/aws_cryptography_dbencryptionsdk_dynamodb/client.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ def _execute_operation(
107107
operation_name: str,
108108
) -> Output:
109109
try:
110-
return self._handle_execution(
111-
input, plugins, serialize, deserialize, config, operation_name
112-
)
110+
return self._handle_execution(input, plugins, serialize, deserialize, config, operation_name)
113111
except Exception as e:
114112
# Make sure every exception that we throw is an instance of ServiceError so
115113
# customers can reliably catch everything we throw.
@@ -173,9 +171,7 @@ def _handle_execution(
173171
interceptor.read_before_serialization(context)
174172

175173
# Step 4: Serialize the request
176-
context_with_transport_request = cast(
177-
InterceptorContext[Input, None, DafnyRequest, None], context
178-
)
174+
context_with_transport_request = cast(InterceptorContext[Input, None, DafnyRequest, None], context)
179175
context_with_transport_request._transport_request = serialize(
180176
context_with_transport_request.request, config
181177
)
@@ -186,8 +182,8 @@ def _handle_execution(
186182

187183
# Step 6: Invoke modify_before_retry_loop
188184
for interceptor in interceptors:
189-
context_with_transport_request._transport_request = (
190-
interceptor.modify_before_retry_loop(context_with_transport_request)
185+
context_with_transport_request._transport_request = interceptor.modify_before_retry_loop(
186+
context_with_transport_request
191187
)
192188

193189
# Step 7: Acquire the retry token.
@@ -237,9 +233,7 @@ def _handle_execution(
237233
# The response will be set either with the modeled output or an exception. The
238234
# transport_request and transport_response may be set or None.
239235
execution_context = cast(
240-
InterceptorContext[
241-
Input, Output, DafnyRequest | None, DafnyResponse | None
242-
],
236+
InterceptorContext[Input, Output, DafnyRequest | None, DafnyResponse | None],
243237
context,
244238
)
245239
return self._finalize_execution(interceptors, execution_context)
@@ -261,14 +255,10 @@ def _handle_attempt(
261255
if config.dafnyImplInterface.impl is None:
262256
raise Exception("No impl found on the operation config.")
263257

264-
context_with_response = cast(
265-
InterceptorContext[Input, None, DafnyRequest, DafnyResponse], context
266-
)
258+
context_with_response = cast(InterceptorContext[Input, None, DafnyRequest, DafnyResponse], context)
267259

268-
context_with_response._transport_response = (
269-
config.dafnyImplInterface.handle_request(
270-
input=context_with_response.transport_request
271-
)
260+
context_with_response._transport_response = config.dafnyImplInterface.handle_request(
261+
input=context_with_response.transport_request
272262
)
273263

274264
# Step 7n: Invoke read_after_transmit
@@ -277,8 +267,8 @@ def _handle_attempt(
277267

278268
# Step 7o: Invoke modify_before_deserialization
279269
for interceptor in interceptors:
280-
context_with_response._transport_response = (
281-
interceptor.modify_before_deserialization(context_with_response)
270+
context_with_response._transport_response = interceptor.modify_before_deserialization(
271+
context_with_response
282272
)
283273

284274
# Step 7p: Invoke read_before_deserialization
@@ -290,9 +280,7 @@ def _handle_attempt(
290280
InterceptorContext[Input, Output, DafnyRequest, DafnyResponse],
291281
context_with_response,
292282
)
293-
context_with_output._response = deserialize(
294-
context_with_output._transport_response, config
295-
)
283+
context_with_output._response = deserialize(context_with_output._transport_response, config)
296284

297285
# Step 7r: Invoke read_after_deserialization
298286
for interceptor in interceptors:
@@ -318,9 +306,7 @@ def _finalize_attempt(
318306
# Step 7s: Invoke modify_before_attempt_completion
319307
try:
320308
for interceptor in interceptors:
321-
context._response = interceptor.modify_before_attempt_completion(
322-
context
323-
)
309+
context._response = interceptor.modify_before_attempt_completion(context)
324310
except Exception as e:
325311
context._response = e
326312

@@ -336,9 +322,7 @@ def _finalize_attempt(
336322
def _finalize_execution(
337323
self,
338324
interceptors: list[Interceptor[Input, Output, DafnyRequest, DafnyResponse]],
339-
context: InterceptorContext[
340-
Input, Output, DafnyRequest | None, DafnyResponse | None
341-
],
325+
context: InterceptorContext[Input, Output, DafnyRequest | None, DafnyResponse | None],
342326
) -> Output:
343327
try:
344328
# Step 9: Invoke modify_before_completion

DynamoDbEncryption/runtimes/python/src/aws_dbesdk_dynamodb/smithygenerated/aws_cryptography_dbencryptionsdk_dynamodb/dafny_to_aws_sdk.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,30 @@ def com_amazonaws_dynamodb_AttributeValue(dafny_input):
2121
# Convert AttributeValue
2222
if isinstance(dafny_input, AttributeValue_S):
2323
AttributeValue_union_value = {
24-
"S": b"".join(ord(c).to_bytes(2, "big") for c in dafny_input.S).decode(
25-
"utf-16-be"
26-
)
24+
"S": b"".join(ord(c).to_bytes(2, "big") for c in dafny_input.S).decode("utf-16-be")
2725
}
2826
elif isinstance(dafny_input, AttributeValue_N):
2927
AttributeValue_union_value = {
30-
"N": b"".join(ord(c).to_bytes(2, "big") for c in dafny_input.N).decode(
31-
"utf-16-be"
32-
)
28+
"N": b"".join(ord(c).to_bytes(2, "big") for c in dafny_input.N).decode("utf-16-be")
3329
}
3430
elif isinstance(dafny_input, AttributeValue_B):
3531
AttributeValue_union_value = {"B": bytes(dafny_input.B)}
3632
elif isinstance(dafny_input, AttributeValue_SS):
3733
AttributeValue_union_value = {
3834
"SS": [
39-
b"".join(ord(c).to_bytes(2, "big") for c in list_element).decode(
40-
"utf-16-be"
41-
)
35+
b"".join(ord(c).to_bytes(2, "big") for c in list_element).decode("utf-16-be")
4236
for list_element in dafny_input.SS
4337
]
4438
}
4539
elif isinstance(dafny_input, AttributeValue_NS):
4640
AttributeValue_union_value = {
4741
"NS": [
48-
b"".join(ord(c).to_bytes(2, "big") for c in list_element).decode(
49-
"utf-16-be"
50-
)
42+
b"".join(ord(c).to_bytes(2, "big") for c in list_element).decode("utf-16-be")
5143
for list_element in dafny_input.NS
5244
]
5345
}
5446
elif isinstance(dafny_input, AttributeValue_BS):
55-
AttributeValue_union_value = {
56-
"BS": [bytes(list_element) for list_element in dafny_input.BS]
57-
}
47+
AttributeValue_union_value = {"BS": [bytes(list_element) for list_element in dafny_input.BS]}
5848
elif isinstance(dafny_input, AttributeValue_M):
5949
AttributeValue_union_value = {
6050
"M": {

0 commit comments

Comments
 (0)