Skip to content

Commit bc7cb2e

Browse files
committed
fix(data_classes): address mypy issues
1 parent fbecef0 commit bc7cb2e

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

aws_lambda_powertools/utilities/data_classes/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from .bedrock_agent_event import BedrockAgentEvent
1010
from .cloud_watch_alarm_event import (
1111
CloudWatchAlarmEvent,
12-
CloudWatchAlarmStateValue,
13-
CloudWatchAlarmState,
1412
CloudWatchAlarmMetric,
13+
CloudWatchAlarmState,
14+
CloudWatchAlarmStateValue,
1515
)
1616
from .cloud_watch_custom_widget_event import CloudWatchDashboardCustomWidgetEvent
1717
from .cloud_watch_logs_event import CloudWatchLogsEvent

aws_lambda_powertools/utilities/data_classes/cloud_watch_alarm_event.py

+34-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
from enum import Enum, auto
35
from typing import List, Optional
@@ -32,7 +34,10 @@ def reason_data(self) -> Optional[dict]:
3234
Additional data to back up the reason, usually contains the evaluated data points,
3335
the calculated threshold and timestamps.
3436
"""
35-
return json.loads(self.get("reasonData")) if self.get("reasonData") is not None else None
37+
if self.get("reasonData") is None:
38+
return None
39+
40+
return json.loads(str(self.get("reasonData")))
3641

3742
@property
3843
def timestamp(self) -> str:
@@ -43,6 +48,11 @@ def timestamp(self) -> str:
4348

4449

4550
class CloudWatchAlarmMetric(DictWrapper):
51+
def __init__(self, data: dict):
52+
super().__init__(data)
53+
54+
self._metric_stat: dict | None = self.get("metricStat")
55+
4656
@property
4757
def metric_id(self) -> str:
4858
"""
@@ -55,35 +65,50 @@ def namespace(self) -> Optional[str]:
5565
"""
5666
Namespace of the correspondent CloudWatch Metric.
5767
"""
58-
return self.get("metricStat", {}).get("metric", {}).get("namespace", None)
68+
if self._metric_stat is not None:
69+
return self._metric_stat.get("metric", {}).get("namespace", None)
70+
71+
return None
5972

6073
@property
6174
def name(self) -> Optional[str]:
6275
"""
6376
Name of the correspondent CloudWatch Metric.
6477
"""
65-
return self.get("metricStat", {}).get("metric", {}).get("name", None)
78+
if self._metric_stat is not None:
79+
return self._metric_stat.get("metric", {}).get("name", None)
80+
81+
return None
6682

6783
@property
6884
def dimensions(self) -> Optional[dict]:
6985
"""
7086
Additional dimensions of the correspondent CloudWatch Metric, if available.
7187
"""
72-
return self.get("metricStat", {}).get("metric", {}).get("dimensions", None)
88+
if self._metric_stat is not None:
89+
return self._metric_stat.get("metric", {}).get("dimensions", None)
90+
91+
return None
7392

7493
@property
7594
def period(self) -> Optional[int]:
7695
"""
7796
Metric evaluation period, in seconds.
7897
"""
79-
return self.get("metricStat", {}).get("period", None)
98+
if self._metric_stat is not None:
99+
return self._metric_stat.get("period", None)
100+
101+
return None
80102

81103
@property
82104
def stat(self) -> Optional[str]:
83105
"""
84106
Statistical aggregation of metric points, e.g. Average, SampleCount, etc.
85107
"""
86-
return self.get("metricStat", {}).get("stat", None)
108+
if self._metric_stat is not None:
109+
return self._metric_stat.get("stat", None)
110+
111+
return None
87112

88113
@property
89114
def return_data(self) -> bool:
@@ -131,14 +156,14 @@ def alarm_name(self) -> str:
131156
"""
132157
Alarm name.
133158
"""
134-
return self.get("alarmData").get("alarmName")
159+
return self["alarmData"]["alarmName"]
135160

136161
@property
137162
def alarm_description(self) -> Optional[str]:
138163
"""
139164
Optional description for the Alarm.
140165
"""
141-
return self.get("alarmData").get("configuration", {}).get("description", None)
166+
return self["alarmData"].get("configuration", {}).get("description", None)
142167

143168
@property
144169
def state(self):
@@ -156,7 +181,7 @@ def previous_state(self):
156181

157182
@property
158183
def alarm_metrics(self) -> Optional[List[CloudWatchAlarmMetric]]:
159-
maybe_metrics = self.get("alarmData", {}).get("configuration", {}).get("metrics", None)
184+
maybe_metrics = self["alarmData"].get("configuration", {}).get("metrics", None)
160185

161186
if maybe_metrics is not None:
162187
return [CloudWatchAlarmMetric(i) for i in maybe_metrics]

tests/unit/data_classes/test_cloud_watch_alarm_event.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import json
22

33
from aws_lambda_powertools.utilities.data_classes.cloud_watch_alarm_event import (
4-
CloudWatchAlarmStateValue,
54
CloudWatchAlarmEvent,
5+
CloudWatchAlarmStateValue,
66
)
77
from tests.functional.utils import load_event
88

@@ -22,7 +22,9 @@ def test_cloud_watch_alarm_event():
2222
assert parsed_event.state.reason_data == json.loads(raw_event["alarmData"]["state"]["reasonData"])
2323
assert parsed_event.state.timestamp == raw_event["alarmData"]["state"]["timestamp"]
2424

25-
assert parsed_event.previous_state.value == CloudWatchAlarmStateValue[raw_event["alarmData"]["previousState"]["value"]]
25+
assert (
26+
parsed_event.previous_state.value == CloudWatchAlarmStateValue[raw_event["alarmData"]["previousState"]["value"]]
27+
)
2628
assert parsed_event.previous_state.reason == raw_event["alarmData"]["previousState"]["reason"]
2729
assert parsed_event.previous_state.reason_data == json.loads(raw_event["alarmData"]["previousState"]["reasonData"])
2830
assert parsed_event.previous_state.timestamp == raw_event["alarmData"]["previousState"]["timestamp"]

0 commit comments

Comments
 (0)