Skip to content

Commit 32fb527

Browse files
AntoniaSzecsiAntoniaSzecsi
authored andcommitted
Add docstrings checking with ruff and implement the fixes (#196)
* Add docstrings checking with ruff and implement the fixes * Fix build and test invocations of RIE * Format file according to linter requirements * Remove ignore rules and move docstring logic to dev file --------- Co-authored-by: AntoniaSzecsi <[email protected]>
1 parent 8bc1b08 commit 32fb527

13 files changed

+136
-57
lines changed

Makefile

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ format:
4747
check-format:
4848
poetry run ruff format --check awslambdaric/ tests/
4949

50+
.PHONY: check-docstr
51+
check-docstr:
52+
python3 scripts/dev.py check-docstr
53+
5054
.PHONY: dev
5155
dev: init test
5256

5357
.PHONY: pr
54-
pr: init check-format check-security dev
58+
pr: init check-format check-annotations check-types check-type-usage check-security dev
5559

5660
.PHONY: codebuild
5761
codebuild: setup-codebuild-agent
@@ -70,17 +74,18 @@ define HELP_MESSAGE
7074
Usage: $ make [TARGETS]
7175

7276
TARGETS
73-
check-security Run bandit to find security issues.
74-
format Run black to automatically update your code to match formatting.
75-
build Build the package using scripts/dev.py.
76-
clean Cleans the working directory using scripts/dev.py.
77-
dev Run all development tests using scripts/dev.py.
78-
init Install dependencies via scripts/dev.py.
79-
build-container Build awslambdaric wheel in isolated container.
80-
test-rie Test with RIE using pre-built wheel (run build-container first).
81-
pr Perform all checks before submitting a Pull Request.
82-
test Run unit tests using scripts/dev.py.
83-
lint Run all linters via scripts/dev.py.
84-
test-smoke Run smoke tests inside Docker.
85-
test-integ Run all integration tests.
77+
check-security Run bandit to find security issues.
78+
check-docstr Check docstrings in project using ruff format check.
79+
format Run ruff to automatically format your code.
80+
build Build the package using scripts/dev.py.
81+
clean Cleans the working directory using scripts/dev.py.
82+
dev Run all development tests using scripts/dev.py.
83+
init Install dependencies via scripts/dev.py.
84+
build-container Build awslambdaric wheel in isolated container.
85+
test-rie Test with RIE using pre-built wheel (run build-container first).
86+
pr Perform all checks before submitting a Pull Request.
87+
test Run unit tests using scripts/dev.py.
88+
lint Run all linters via scripts/dev.py.
89+
test-smoke Run smoke tests inside Docker.
90+
test-integ Run all integration tests.
8691
endef

awslambdaric/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
"""
2-
Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
"""
1+
"""Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved."""
42

53
__version__ = "3.1.1"

awslambdaric/__main__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
"""
1+
"""Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved."""
42

53
import os
64
import sys
@@ -9,6 +7,7 @@
97

108

119
def main(args):
10+
"""Run the Lambda runtime main entry point."""
1211
app_root = os.getcwd()
1312

1413
try:

awslambdaric/bootstrap.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"""
2-
Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
"""
1+
"""Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved."""
42

53
import importlib
64
import json
@@ -36,6 +34,7 @@
3634

3735

3836
def _get_handler(handler):
37+
"""Get handler function from module."""
3938
try:
4039
(modname, fname) = handler.rsplit(".", 1)
4140
except ValueError as e:
@@ -82,6 +81,7 @@ def make_error(
8281
stack_trace,
8382
invoke_id=None,
8483
):
84+
"""Create error response."""
8585
result = {
8686
"errorMessage": error_message if error_message else "",
8787
"errorType": error_type if error_type else "",
@@ -92,6 +92,7 @@ def make_error(
9292

9393

9494
def replace_line_indentation(line, indent_char, new_indent_char):
95+
"""Replace line indentation characters."""
9596
ident_chars_count = 0
9697
for c in line:
9798
if c != indent_char:
@@ -104,6 +105,7 @@ def replace_line_indentation(line, indent_char, new_indent_char):
104105
_ERROR_FRAME_TYPE = _JSON_FRAME_TYPES[logging.ERROR]
105106

106107
def log_error(error_result, log_sink):
108+
"""Log error in JSON format."""
107109
error_result = {
108110
"timestamp": time.strftime(
109111
_DATETIME_FORMAT, logging.Formatter.converter(time.time())
@@ -119,6 +121,7 @@ def log_error(error_result, log_sink):
119121
_ERROR_FRAME_TYPE = _TEXT_FRAME_TYPES[logging.ERROR]
120122

121123
def log_error(error_result, log_sink):
124+
"""Log error in text format."""
122125
error_description = "[ERROR]"
123126

124127
error_result_type = error_result.get("errorType")
@@ -161,6 +164,7 @@ def handle_event_request(
161164
tenant_id,
162165
log_sink,
163166
):
167+
"""Handle Lambda event request."""
164168
error_result = None
165169
try:
166170
lambda_context = create_lambda_context(
@@ -212,6 +216,7 @@ def handle_event_request(
212216

213217

214218
def parse_json_header(header, name):
219+
"""Parse JSON header."""
215220
try:
216221
return json.loads(header)
217222
except Exception as e:
@@ -230,6 +235,7 @@ def create_lambda_context(
230235
invoked_function_arn,
231236
tenant_id,
232237
):
238+
"""Create Lambda context object."""
233239
client_context = None
234240
if client_context_json:
235241
client_context = parse_json_header(client_context_json, "Client Context")
@@ -248,6 +254,7 @@ def create_lambda_context(
248254

249255

250256
def build_fault_result(exc_info, msg):
257+
"""Build fault result from exception info."""
251258
etype, value, tb = exc_info
252259
tb_tuples = extract_traceback(tb)
253260
for i in range(len(tb_tuples)):
@@ -263,6 +270,7 @@ def build_fault_result(exc_info, msg):
263270

264271

265272
def make_xray_fault(ex_type, ex_msg, working_dir, tb_tuples):
273+
"""Create X-Ray fault object."""
266274
stack = []
267275
files = set()
268276
for t in tb_tuples:
@@ -281,13 +289,15 @@ def make_xray_fault(ex_type, ex_msg, working_dir, tb_tuples):
281289

282290

283291
def extract_traceback(tb):
292+
"""Extract traceback information."""
284293
return [
285294
(frame.filename, frame.lineno, frame.name, frame.line)
286295
for frame in traceback.extract_tb(tb)
287296
]
288297

289298

290299
def on_init_complete(lambda_runtime_client, log_sink):
300+
"""Handle initialization completion."""
291301
from . import lambda_runtime_hooks_runner
292302

293303
try:
@@ -311,21 +321,29 @@ def on_init_complete(lambda_runtime_client, log_sink):
311321

312322

313323
class LambdaLoggerHandler(logging.Handler):
324+
"""Lambda logger handler."""
325+
314326
def __init__(self, log_sink):
327+
"""Initialize logger handler."""
315328
logging.Handler.__init__(self)
316329
self.log_sink = log_sink
317330

318331
def emit(self, record):
332+
"""Emit log record."""
319333
msg = self.format(record)
320334
self.log_sink.log(msg)
321335

322336

323337
class LambdaLoggerHandlerWithFrameType(logging.Handler):
338+
"""Lambda logger handler with frame type."""
339+
324340
def __init__(self, log_sink):
341+
"""Initialize logger handler."""
325342
super().__init__()
326343
self.log_sink = log_sink
327344

328345
def emit(self, record):
346+
"""Emit log record with frame type."""
329347
self.log_sink.log(
330348
self.format(record),
331349
frame_type=(
@@ -336,14 +354,20 @@ def emit(self, record):
336354

337355

338356
class LambdaLoggerFilter(logging.Filter):
357+
"""Lambda logger filter."""
358+
339359
def filter(self, record):
360+
"""Filter log record."""
340361
record.aws_request_id = _GLOBAL_AWS_REQUEST_ID or ""
341362
record.tenant_id = _GLOBAL_TENANT_ID
342363
return True
343364

344365

345366
class Unbuffered(object):
367+
"""Unbuffered stream wrapper."""
368+
346369
def __init__(self, stream):
370+
"""Initialize unbuffered stream."""
347371
self.stream = stream
348372

349373
def __enter__(self):
@@ -356,16 +380,21 @@ def __getattr__(self, attr):
356380
return getattr(self.stream, attr)
357381

358382
def write(self, msg):
383+
"""Write message to stream."""
359384
self.stream.write(msg)
360385
self.stream.flush()
361386

362387
def writelines(self, msgs):
388+
"""Write multiple lines to stream."""
363389
self.stream.writelines(msgs)
364390
self.stream.flush()
365391

366392

367393
class StandardLogSink(object):
394+
"""Standard log sink."""
395+
368396
def __init__(self):
397+
"""Initialize standard log sink."""
369398
pass
370399

371400
def __enter__(self):
@@ -375,17 +404,19 @@ def __exit__(self, exc_type, exc_value, exc_tb):
375404
pass
376405

377406
def log(self, msg, frame_type=None):
407+
"""Log message to stdout."""
378408
sys.stdout.write(msg)
379409

380410
def log_error(self, message_lines):
411+
"""Log error message to stdout."""
381412
error_message = ERROR_LOG_LINE_TERMINATE.join(message_lines) + "\n"
382413
sys.stdout.write(error_message)
383414

384415

385416
class FramedTelemetryLogSink(object):
386-
"""
387-
FramedTelemetryLogSink implements the logging contract between runtimes and the platform. It implements a simple
388-
framing protocol so message boundaries can be determined. Each frame can be visualized as follows:
417+
"""FramedTelemetryLogSink implements the logging contract between runtimes and the platform.
418+
419+
It implements a simple framing protocol so message boundaries can be determined. Each frame can be visualized as follows:
389420
<pre>
390421
{@code
391422
+----------------------+------------------------+---------------------+-----------------------+
@@ -399,6 +430,7 @@ class FramedTelemetryLogSink(object):
399430
"""
400431

401432
def __init__(self, fd):
433+
"""Initialize framed telemetry log sink."""
402434
self.fd = int(fd)
403435

404436
def __enter__(self):
@@ -409,6 +441,7 @@ def __exit__(self, exc_type, exc_value, exc_tb):
409441
self.file.close()
410442

411443
def log(self, msg, frame_type=None):
444+
"""Log message with frame type."""
412445
encoded_msg = msg.encode("utf8")
413446

414447
timestamp = int(time.time_ns() / 1000) # UNIX timestamp in microseconds
@@ -421,6 +454,7 @@ def log(self, msg, frame_type=None):
421454
self.file.write(log_msg)
422455

423456
def log_error(self, message_lines):
457+
"""Log error message."""
424458
error_message = "\n".join(message_lines)
425459
self.log(
426460
error_message,
@@ -429,6 +463,7 @@ def log_error(self, message_lines):
429463

430464

431465
def update_xray_env_variable(xray_trace_id):
466+
"""Update X-Ray trace ID environment variable."""
432467
if xray_trace_id is not None:
433468
os.environ["_X_AMZN_TRACE_ID"] = xray_trace_id
434469
else:
@@ -437,6 +472,7 @@ def update_xray_env_variable(xray_trace_id):
437472

438473

439474
def create_log_sink():
475+
"""Create appropriate log sink."""
440476
if "_LAMBDA_TELEMETRY_LOG_FD" in os.environ:
441477
fd = os.environ["_LAMBDA_TELEMETRY_LOG_FD"]
442478
del os.environ["_LAMBDA_TELEMETRY_LOG_FD"]
@@ -451,6 +487,7 @@ def create_log_sink():
451487

452488

453489
def _setup_logging(log_format, log_level, log_sink):
490+
"""Set up logging configuration."""
454491
logging.Formatter.converter = time.gmtime
455492
logger = logging.getLogger()
456493

@@ -477,6 +514,7 @@ def _setup_logging(log_format, log_level, log_sink):
477514

478515

479516
def run(app_root, handler, lambda_runtime_api_addr):
517+
"""Run Lambda runtime."""
480518
sys.stdout = Unbuffered(sys.stdout)
481519
sys.stderr = Unbuffered(sys.stderr)
482520

0 commit comments

Comments
 (0)