Skip to content

Commit 42df7ef

Browse files
author
Michael Brewer
committed
chore: Correct the typing as suggested by mypy
1 parent e179864 commit 42df7ef

File tree

9 files changed

+24
-21
lines changed

9 files changed

+24
-21
lines changed

aws_lambda_powertools/logging/lambda_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Any
2+
3+
14
class LambdaContextModel:
25
"""A handful of Lambda Runtime Context fields
36
@@ -31,7 +34,7 @@ def __init__(
3134
self.function_request_id = function_request_id
3235

3336

34-
def build_lambda_context_model(context: object) -> LambdaContextModel:
37+
def build_lambda_context_model(context: Any) -> LambdaContextModel:
3538
"""Captures Lambda function runtime info to be used across all log statements
3639
3740
Parameters

aws_lambda_powertools/logging/logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import random
66
import sys
7-
from typing import Any, Callable, Dict, Union
7+
from typing import Any, Callable, Dict, Optional, Union
88

99
import jmespath
1010

@@ -318,12 +318,12 @@ def set_correlation_id(self, value: str):
318318
self.structure_logs(append=True, correlation_id=value)
319319

320320
@staticmethod
321-
def _get_log_level(level: Union[str, int]) -> Union[str, int]:
321+
def _get_log_level(level: Union[str, int, None]) -> Union[str, int]:
322322
""" Returns preferred log level set by the customer in upper case """
323323
if isinstance(level, int):
324324
return level
325325

326-
log_level: str = level or os.getenv("LOG_LEVEL")
326+
log_level: Optional[str] = level or os.getenv("LOG_LEVEL")
327327
if log_level is None:
328328
return logging.INFO
329329

aws_lambda_powertools/metrics/metric.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import logging
33
from contextlib import contextmanager
4-
from typing import Dict
4+
from typing import Dict, Optional, Union
55

66
from .base import MetricManager, MetricUnit
77

@@ -42,7 +42,7 @@ class SingleMetric(MetricManager):
4242
Inherits from `aws_lambda_powertools.metrics.base.MetricManager`
4343
"""
4444

45-
def add_metric(self, name: str, unit: MetricUnit, value: float):
45+
def add_metric(self, name: str, unit: Union[MetricUnit, str], value: float):
4646
"""Method to prevent more than one metric being created
4747
4848
Parameters
@@ -109,11 +109,11 @@ def single_metric(name: str, unit: MetricUnit, value: float, namespace: str = No
109109
SchemaValidationError
110110
When metric object fails EMF schema validation
111111
"""
112-
metric_set = None
112+
metric_set: Optional[Dict] = None
113113
try:
114114
metric: SingleMetric = SingleMetric(namespace=namespace)
115115
metric.add_metric(name=name, unit=unit, value=value)
116116
yield metric
117-
metric_set: Dict = metric.serialize_metric_set()
117+
metric_set = metric.serialize_metric_set()
118118
finally:
119119
print(json.dumps(metric_set, separators=(",", ":")))

aws_lambda_powertools/metrics/metrics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import logging
44
import warnings
5-
from typing import Any, Callable
5+
from typing import Any, Callable, Dict, Optional
66

77
from .base import MetricManager, MetricUnit
88
from .metric import single_metric
@@ -71,15 +71,15 @@ def do_something():
7171
When metric object fails EMF schema validation
7272
"""
7373

74-
_metrics = {}
75-
_dimensions = {}
76-
_metadata = {}
74+
_metrics: Dict[str, Any] = {}
75+
_dimensions: Dict[str, Any] = {}
76+
_metadata: Dict[str, Any] = {}
7777

7878
def __init__(self, service: str = None, namespace: str = None):
7979
self.metric_set = self._metrics
8080
self.dimension_set = self._dimensions
8181
self.service = service
82-
self.namespace = namespace
82+
self.namespace: Optional[str] = namespace
8383
self.metadata_set = self._metadata
8484

8585
super().__init__(

aws_lambda_powertools/shared/functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from distutils.util import strtobool
2-
from typing import Any, Union
2+
from typing import Any, Optional, Union
33

44

55
def resolve_truthy_env_var_choice(env: Any, choice: bool = None) -> bool:
@@ -22,7 +22,7 @@ def resolve_truthy_env_var_choice(env: Any, choice: bool = None) -> bool:
2222
return choice if choice is not None else strtobool(env)
2323

2424

25-
def resolve_env_var_choice(env: Any, choice: bool = None) -> Union[bool, Any]:
25+
def resolve_env_var_choice(env: Any, choice: Optional[Any] = None) -> Union[bool, Any]:
2626
"""Pick explicit choice over env, if available, otherwise return env value received
2727
2828
NOTE: Environment variable should be resolved by the caller.

aws_lambda_powertools/utilities/batch/sqs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def _get_queue_url(self) -> Optional[str]:
7171
Format QueueUrl from first records entry
7272
"""
7373
if not getattr(self, "records", None):
74-
return
74+
return None
7575

7676
*_, account_id, queue_name = self.records[0]["eventSourceARN"].split(":")
7777
return f"{self.client._endpoint.host}/{account_id}/{queue_name}"

aws_lambda_powertools/utilities/idempotency/persistence/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def __init__(
4040
idempotency_key,
4141
status: str = "",
4242
expiry_timestamp: int = None,
43-
response_data: str = "",
43+
response_data: Optional[str] = "",
4444
payload_hash: str = None,
4545
) -> None:
4646
"""

aws_lambda_powertools/utilities/parameters/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
from abc import ABC, abstractmethod
88
from collections import namedtuple
99
from datetime import datetime, timedelta
10-
from typing import Dict, Optional, Tuple, Union
10+
from typing import Any, Dict, Optional, Tuple, Union
1111

1212
from .exceptions import GetParameterError, TransformParameterError
1313

1414
DEFAULT_MAX_AGE_SECS = 5
1515
ExpirableValue = namedtuple("ExpirableValue", ["value", "ttl"])
1616
# These providers will be dynamically initialized on first use of the helper functions
17-
DEFAULT_PROVIDERS = {}
17+
DEFAULT_PROVIDERS: Dict[str, Any] = {}
1818
TRANSFORM_METHOD_JSON = "json"
1919
TRANSFORM_METHOD_BINARY = "binary"
2020
SUPPORTED_TRANSFORM_METHODS = [TRANSFORM_METHOD_JSON, TRANSFORM_METHOD_BINARY]

aws_lambda_powertools/utilities/parameters/dynamodb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55

6-
from typing import Dict, Optional
6+
from typing import Any, Dict, Optional
77

88
import boto3
99
from boto3.dynamodb.conditions import Key
@@ -139,7 +139,7 @@ class DynamoDBProvider(BaseProvider):
139139
c Parameter value c
140140
"""
141141

142-
table = None
142+
table: Any = None
143143
key_attr = None
144144
sort_attr = None
145145
value_attr = None

0 commit comments

Comments
 (0)