Skip to content

Commit 271cdf6

Browse files
committed
Refix linting issues
1 parent 27084c3 commit 271cdf6

File tree

5 files changed

+100
-36
lines changed

5 files changed

+100
-36
lines changed

awslambdaric/bootstrap.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,13 @@ def handle_event_request(
143143
invoke_id,
144144
invoked_function_arn,
145145
)
146-
event = lambda_runtime_client.marshaller.unmarshal_request(event_body, content_type)
146+
event = lambda_runtime_client.marshaller.unmarshal_request(
147+
event_body, content_type
148+
)
147149
response = request_handler(event, lambda_context)
148-
result, result_content_type = lambda_runtime_client.marshaller.marshal_response(response)
150+
result, result_content_type = lambda_runtime_client.marshaller.marshal_response(
151+
response
152+
)
149153
except FaultException as e:
150154
xray_fault = make_xray_fault("LambdaValidationError", e.msg, os.getcwd(), [])
151155
error_result = make_error(e.msg, e.exception_type, e.trace, invoke_id)
@@ -169,7 +173,9 @@ def handle_event_request(
169173
invoke_id, to_json(error_result), to_json(xray_fault)
170174
)
171175
else:
172-
lambda_runtime_client.post_invocation_result(invoke_id, result, result_content_type)
176+
lambda_runtime_client.post_invocation_result(
177+
invoke_id, result, result_content_type
178+
)
173179

174180

175181
def parse_json_header(header, name):
@@ -214,7 +220,9 @@ def build_fault_result(exc_info, msg):
214220
tb_tuples = tb_tuples[i:]
215221
break
216222

217-
return make_error(msg if msg else str(value), etype.__name__, traceback.format_list(tb_tuples))
223+
return make_error(
224+
msg if msg else str(value), etype.__name__, traceback.format_list(tb_tuples)
225+
)
218226

219227

220228
def make_xray_fault(ex_type, ex_msg, working_dir, tb_tuples):

awslambdaric/lambda_context.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ def __init__(
2727

2828
self.client_context = make_obj_from_dict(ClientContext, client_context)
2929
if self.client_context is not None:
30-
self.client_context.client = make_obj_from_dict(Client, self.client_context.client)
30+
self.client_context.client = make_obj_from_dict(
31+
Client, self.client_context.client
32+
)
3133

3234
self.identity = make_obj_from_dict(CognitoIdentity, {})
3335
if cognito_identity is not None:
34-
self.identity.cognito_identity_id = cognito_identity.get("cognitoIdentityId")
35-
self.identity.cognito_identity_pool_id = cognito_identity.get("cognitoIdentityPoolId")
36+
self.identity.cognito_identity_id = cognito_identity.get(
37+
"cognitoIdentityId"
38+
)
39+
self.identity.cognito_identity_pool_id = cognito_identity.get(
40+
"cognitoIdentityPoolId"
41+
)
3642

3743
self._epoch_deadline_time_in_ms = epoch_deadline_time_in_ms
3844

awslambdaric/lambda_runtime_client.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010

1111
def _user_agent():
12-
py_version = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
12+
py_version = (
13+
f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
14+
)
1315
pkg_version = __version__
1416
return f"aws-lambda-python/{py_version}-{pkg_version}"
1517

@@ -75,10 +77,14 @@ def wait_next_invocation(self):
7577
event_body=response_body,
7678
)
7779

78-
def post_invocation_result(self, invoke_id, result_data, content_type="application/json"):
80+
def post_invocation_result(
81+
self, invoke_id, result_data, content_type="application/json"
82+
):
7983
runtime_client.post_invocation_result(
8084
invoke_id,
81-
result_data if isinstance(result_data, bytes) else result_data.encode("utf-8"),
85+
result_data
86+
if isinstance(result_data, bytes)
87+
else result_data.encode("utf-8"),
8288
content_type,
8389
)
8490

tests/test_bootstrap.py

+61-23
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,21 @@ def __init__(self, message):
277277
xray_fault = json.loads(args[2])
278278
self.assertEqual(xray_fault["working_directory"], self.working_directory)
279279
self.assertEqual(len(xray_fault["exceptions"]), 1)
280-
self.assertEqual(xray_fault["exceptions"][0]["message"], expected_response["errorMessage"])
281-
self.assertEqual(xray_fault["exceptions"][0]["type"], expected_response["errorType"])
280+
self.assertEqual(
281+
xray_fault["exceptions"][0]["message"], expected_response["errorMessage"]
282+
)
283+
self.assertEqual(
284+
xray_fault["exceptions"][0]["type"], expected_response["errorType"]
285+
)
282286
self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1)
283287
self.assertEqual(
284288
xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler"
285289
)
286290
self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int)
287291
self.assertTrue(
288-
xray_fault["exceptions"][0]["stack"][0]["path"].endswith(os.path.relpath(__file__))
292+
xray_fault["exceptions"][0]["stack"][0]["path"].endswith(
293+
os.path.relpath(__file__)
294+
)
289295
)
290296
self.assertEqual(len(xray_fault["paths"]), 1)
291297
self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__)))
@@ -323,15 +329,21 @@ def __init__(self, message):
323329
xray_fault = json.loads(args[2])
324330
self.assertEqual(xray_fault["working_directory"], self.working_directory)
325331
self.assertEqual(len(xray_fault["exceptions"]), 1)
326-
self.assertEqual(xray_fault["exceptions"][0]["message"], expected_response["errorMessage"])
327-
self.assertEqual(xray_fault["exceptions"][0]["type"], expected_response["errorType"])
332+
self.assertEqual(
333+
xray_fault["exceptions"][0]["message"], expected_response["errorMessage"]
334+
)
335+
self.assertEqual(
336+
xray_fault["exceptions"][0]["type"], expected_response["errorType"]
337+
)
328338
self.assertEqual(len(xray_fault["exceptions"][0]["stack"]), 1)
329339
self.assertEqual(
330340
xray_fault["exceptions"][0]["stack"][0]["label"], "raise_exception_handler"
331341
)
332342
self.assertIsInstance(xray_fault["exceptions"][0]["stack"][0]["line"], int)
333343
self.assertTrue(
334-
xray_fault["exceptions"][0]["stack"][0]["path"].endswith(os.path.relpath(__file__))
344+
xray_fault["exceptions"][0]["stack"][0]["path"].endswith(
345+
os.path.relpath(__file__)
346+
)
335347
)
336348
self.assertEqual(len(xray_fault["paths"]), 1)
337349
self.assertTrue(xray_fault["paths"][0].endswith(os.path.relpath(__file__)))
@@ -460,7 +472,9 @@ def raise_exception_handler(json_input, lambda_context):
460472
try:
461473
import invalid_module # noqa: F401
462474
except ImportError:
463-
raise bootstrap.FaultException("FaultExceptionType", "Fault exception msg", None)
475+
raise bootstrap.FaultException(
476+
"FaultExceptionType", "Fault exception msg", None
477+
)
464478

465479
bootstrap.handle_event_request(
466480
self.lambda_runtime,
@@ -474,14 +488,14 @@ def raise_exception_handler(json_input, lambda_context):
474488
0,
475489
bootstrap.StandardLogSink(),
476490
)
477-
error_logs = (
478-
"[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
479-
)
491+
error_logs = "[ERROR] FaultExceptionType: Fault exception msg\rTraceback (most recent call last):\n"
480492

481493
self.assertEqual(mock_stdout.getvalue(), error_logs)
482494

483495
@patch("sys.stdout", new_callable=StringIO)
484-
def test_handle_event_request_fault_exception_logging_nomessage_notrace(self, mock_stdout):
496+
def test_handle_event_request_fault_exception_logging_nomessage_notrace(
497+
self, mock_stdout
498+
):
485499
def raise_exception_handler(json_input, lambda_context):
486500
try:
487501
import invalid_module # noqa: F401
@@ -505,7 +519,9 @@ def raise_exception_handler(json_input, lambda_context):
505519
self.assertEqual(mock_stdout.getvalue(), error_logs)
506520

507521
@patch("sys.stdout", new_callable=StringIO)
508-
def test_handle_event_request_fault_exception_logging_notype_notrace(self, mock_stdout):
522+
def test_handle_event_request_fault_exception_logging_notype_notrace(
523+
self, mock_stdout
524+
):
509525
def raise_exception_handler(json_input, lambda_context):
510526
try:
511527
import invalid_module # noqa: F401
@@ -529,7 +545,9 @@ def raise_exception_handler(json_input, lambda_context):
529545
self.assertEqual(mock_stdout.getvalue(), error_logs)
530546

531547
@patch("sys.stdout", new_callable=StringIO)
532-
def test_handle_event_request_fault_exception_logging_notype_nomessage(self, mock_stdout):
548+
def test_handle_event_request_fault_exception_logging_notype_nomessage(
549+
self, mock_stdout
550+
):
533551
def raise_exception_handler(json_input, lambda_context):
534552
try:
535553
import invalid_module # noqa: F401
@@ -652,7 +670,9 @@ def __init__(self):
652670
self.assertEqual(actual["exceptions"][0]["stack"][0]["label"], "test_method")
653671
self.assertEqual(actual["exceptions"][0]["stack"][0]["path"], "test.py")
654672
self.assertEqual(actual["exceptions"][0]["stack"][0]["line"], 28)
655-
self.assertEqual(actual["exceptions"][0]["stack"][1]["label"], "another_test_method")
673+
self.assertEqual(
674+
actual["exceptions"][0]["stack"][1]["label"], "another_test_method"
675+
)
656676
self.assertEqual(actual["exceptions"][0]["stack"][1]["path"], "another_test.py")
657677
self.assertEqual(actual["exceptions"][0]["stack"][1]["line"], 2718)
658678

@@ -662,7 +682,9 @@ class FaultExceptionMatcher(BaseException):
662682
def __init__(self, msg, exception_type=None, trace_pattern=None):
663683
self.msg = msg
664684
self.exception_type = exception_type
665-
self.trace = trace_pattern if trace_pattern is None else re.compile(trace_pattern)
685+
self.trace = (
686+
trace_pattern if trace_pattern is None else re.compile(trace_pattern)
687+
)
666688

667689
def __eq__(self, other):
668690
trace_matches = True
@@ -710,8 +732,12 @@ def test_get_event_handler_import_error(self):
710732

711733
def test_get_event_handler_syntax_error(self):
712734
importlib.invalidate_caches()
713-
with tempfile.NamedTemporaryFile(suffix=".py", dir=".", delete=False) as tmp_file:
714-
tmp_file.write(b"def syntax_error()\n\tprint('syntax error, no colon after function')")
735+
with tempfile.NamedTemporaryFile(
736+
suffix=".py", dir=".", delete=False
737+
) as tmp_file:
738+
tmp_file.write(
739+
b"def syntax_error()\n\tprint('syntax error, no colon after function')"
740+
)
715741
tmp_file.flush()
716742

717743
filename_w_ext = os.path.basename(tmp_file.name)
@@ -733,7 +759,9 @@ def test_get_event_handler_syntax_error(self):
733759

734760
def test_get_event_handler_missing_error(self):
735761
importlib.invalidate_caches()
736-
with tempfile.NamedTemporaryFile(suffix=".py", dir=".", delete=False) as tmp_file:
762+
with tempfile.NamedTemporaryFile(
763+
suffix=".py", dir=".", delete=False
764+
) as tmp_file:
737765
tmp_file.write(b"def wrong_handler_name():\n\tprint('hello')")
738766
tmp_file.flush()
739767

@@ -772,11 +800,15 @@ def test_get_event_handler_build_in_conflict(self):
772800
)
773801

774802
def test_get_event_handler_doesnt_throw_build_in_module_name_slash(self):
775-
response_handler = bootstrap._get_handler("tests/test_built_in_module_name/sys.my_handler")
803+
response_handler = bootstrap._get_handler(
804+
"tests/test_built_in_module_name/sys.my_handler"
805+
)
776806
response_handler()
777807

778808
def test_get_event_handler_doent_throw_build_in_module_name(self):
779-
response_handler = bootstrap._get_handler("tests.test_built_in_module_name.sys.my_handler")
809+
response_handler = bootstrap._get_handler(
810+
"tests.test_built_in_module_name.sys.my_handler"
811+
)
780812
response_handler()
781813

782814

@@ -946,7 +978,9 @@ def test_log_error_indentation_framed_log_sink(self):
946978

947979
@patch("sys.stdout", new_callable=StringIO)
948980
def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout):
949-
err_to_log = bootstrap.make_error("Error message", "ErrorType", ["line1", "", "line2"])
981+
err_to_log = bootstrap.make_error(
982+
"Error message", "ErrorType", ["line1", "", "line2"]
983+
)
950984
bootstrap.log_error(err_to_log, bootstrap.StandardLogSink())
951985

952986
expected_logged_error = "[ERROR] ErrorType: Error message\rTraceback (most recent call last):\rline1\r\rline2\n"
@@ -1117,7 +1151,9 @@ def test_run(self, mock_runtime_client, mock_handle_event_request):
11171151
]
11181152

11191153
with self.assertRaises(TypeError):
1120-
bootstrap.run(expected_app_root, expected_handler, expected_lambda_runtime_api_addr)
1154+
bootstrap.run(
1155+
expected_app_root, expected_handler, expected_lambda_runtime_api_addr
1156+
)
11211157

11221158
mock_handle_event_request.assert_called_once()
11231159

@@ -1140,7 +1176,9 @@ class TestException(Exception):
11401176
mock_sys.exit.side_effect = TestException("Boom!")
11411177

11421178
with self.assertRaises(TestException):
1143-
bootstrap.run(expected_app_root, expected_handler, expected_lambda_runtime_api_addr)
1179+
bootstrap.run(
1180+
expected_app_root, expected_handler, expected_lambda_runtime_api_addr
1181+
)
11441182

11451183
mock_sys.exit.assert_called_once_with(1)
11461184

tests/test_lambda_runtime_client.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ def test_post_invocation_error(self, mock_runtime_client):
166166

167167
runtime_client.post_invocation_error(invoke_id, error_data, xray_fault)
168168

169-
mock_runtime_client.post_error.assert_called_once_with(invoke_id, error_data, xray_fault)
169+
mock_runtime_client.post_error.assert_called_once_with(
170+
invoke_id, error_data, xray_fault
171+
)
170172

171173
@patch("awslambdaric.lambda_runtime_client.runtime_client")
172174
def test_post_invocation_error_with_large_xray_cause(self, mock_runtime_client):
@@ -188,9 +190,13 @@ def test_post_invocation_error_with_too_large_xray_cause(self, mock_runtime_clie
188190
invoke_id = "1234"
189191
too_large_xray_fault = "a" * int(1024 * 1024)
190192

191-
runtime_client.post_invocation_error(invoke_id, error_data, too_large_xray_fault)
193+
runtime_client.post_invocation_error(
194+
invoke_id, error_data, too_large_xray_fault
195+
)
192196

193-
mock_runtime_client.post_error.assert_called_once_with(invoke_id, error_data, "")
197+
mock_runtime_client.post_error.assert_called_once_with(
198+
invoke_id, error_data, ""
199+
)
194200

195201
def test_connection_refused(self):
196202
with self.assertRaises(ConnectionRefusedError):

0 commit comments

Comments
 (0)