Skip to content

Commit d02d15b

Browse files
refactor(metrics): use standard collections for types (#6472)
Using generics types Co-authored-by: Ana Falcão <[email protected]>
1 parent 631b713 commit d02d15b

File tree

9 files changed

+45
-27
lines changed

9 files changed

+45
-27
lines changed

aws_lambda_powertools/metrics/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import warnings
1616
from collections import defaultdict
1717
from contextlib import contextmanager
18-
from typing import TYPE_CHECKING, Any, Callable, Generator
18+
from typing import TYPE_CHECKING, Any
1919

2020
from aws_lambda_powertools.metrics.exceptions import (
2121
MetricResolutionError,
@@ -34,6 +34,8 @@
3434
from aws_lambda_powertools.shared.functions import resolve_env_var_choice
3535

3636
if TYPE_CHECKING:
37+
from collections.abc import Callable, Generator
38+
3739
from aws_lambda_powertools.metrics.types import MetricNameUnitResolution
3840

3941
logger = logging.getLogger(__name__)

aws_lambda_powertools/metrics/metrics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# NOTE: keeps for compatibility
22
from __future__ import annotations
33

4-
from typing import TYPE_CHECKING, Any, Callable
4+
from typing import TYPE_CHECKING, Any
55

66
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.cloudwatch import AmazonCloudWatchEMFProvider
77

88
if TYPE_CHECKING:
9+
from collections.abc import Callable
10+
911
from aws_lambda_powertools.metrics.base import MetricResolution, MetricUnit
1012
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import CloudWatchEMFOutput
1113
from aws_lambda_powertools.shared.types import AnyCallableT

tests/functional/metrics/conftest.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import Any, Dict, List, Union
1+
from __future__ import annotations
2+
3+
from typing import Any
24

35
import pytest
46

@@ -20,51 +22,51 @@ def reset_metric_set():
2022

2123

2224
@pytest.fixture
23-
def metric_with_resolution() -> Dict[str, Union[str, int]]:
25+
def metric_with_resolution() -> dict[str, str | int]:
2426
return {"name": "single_metric", "unit": MetricUnit.Count, "value": 1, "resolution": MetricResolution.High}
2527

2628

2729
@pytest.fixture
28-
def metric() -> Dict[str, str]:
30+
def metric() -> dict[str, str]:
2931
return {"name": "single_metric", "unit": MetricUnit.Count, "value": 1}
3032

3133

3234
@pytest.fixture
33-
def metric_datadog() -> Dict[str, str]:
35+
def metric_datadog() -> dict[str, str]:
3436
return {"name": "single_metric", "value": 1, "timestamp": 1691678198, "powertools": "datadog"}
3537

3638

3739
@pytest.fixture
38-
def metrics() -> List[Dict[str, str]]:
40+
def metrics() -> list[dict[str, str]]:
3941
return [
4042
{"name": "metric_one", "unit": MetricUnit.Count, "value": 1},
4143
{"name": "metric_two", "unit": MetricUnit.Count, "value": 1},
4244
]
4345

4446

4547
@pytest.fixture
46-
def metrics_same_name() -> List[Dict[str, str]]:
48+
def metrics_same_name() -> list[dict[str, str]]:
4749
return [
4850
{"name": "metric_one", "unit": MetricUnit.Count, "value": 1},
4951
{"name": "metric_one", "unit": MetricUnit.Count, "value": 5},
5052
]
5153

5254

5355
@pytest.fixture
54-
def dimension() -> Dict[str, str]:
56+
def dimension() -> dict[str, str]:
5557
return {"name": "test_dimension", "value": "test"}
5658

5759

5860
@pytest.fixture
59-
def dimensions() -> List[Dict[str, str]]:
61+
def dimensions() -> list[dict[str, str]]:
6062
return [
6163
{"name": "test_dimension", "value": "test"},
6264
{"name": "test_dimension_2", "value": "test"},
6365
]
6466

6567

6668
@pytest.fixture
67-
def non_str_dimensions() -> List[Dict[str, Any]]:
69+
def non_str_dimensions() -> list[dict[str, Any]]:
6870
return [
6971
{"name": "test_dimension", "value": True},
7072
{"name": "test_dimension_2", "value": 3},
@@ -82,15 +84,15 @@ def service() -> str:
8284

8385

8486
@pytest.fixture
85-
def metadata() -> Dict[str, str]:
87+
def metadata() -> dict[str, str]:
8688
return {"key": "username", "value": "test"}
8789

8890

8991
@pytest.fixture
90-
def a_hundred_metrics() -> List[Dict[str, str]]:
92+
def a_hundred_metrics() -> list[dict[str, str]]:
9193
return [{"name": f"metric_{i}", "unit": "Count", "value": 1} for i in range(100)]
9294

9395

9496
@pytest.fixture
95-
def a_hundred_metric_values() -> List[Dict[str, str]]:
97+
def a_hundred_metric_values() -> list[dict[str, str]]:
9698
return [{"name": "metric", "unit": "Count", "value": i} for i in range(100)]

tests/functional/metrics/datadog/test_metrics_datadog.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
import warnings
35
from collections import namedtuple

tests/functional/metrics/required_dependencies/test_metrics_cloudwatch_emf.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from __future__ import annotations
2+
13
import datetime
24
import json
35
import warnings
46
from collections import namedtuple
5-
from typing import Dict, List, Optional, Union
7+
from typing import Any
68

79
import pytest
810

@@ -29,10 +31,10 @@
2931

3032

3133
def serialize_metrics(
32-
metrics: List[Dict],
33-
dimensions: List[Dict],
34+
metrics: list[dict[str, Any]],
35+
dimensions: list[dict[str, Any]],
3436
namespace: str,
35-
metadatas: Optional[List[Dict]] = None,
37+
metadatas: list[dict] | None = None,
3638
) -> CloudWatchEMFOutput:
3739
"""Helper function to build EMF object from a list of metrics, dimensions"""
3840
my_metrics = AmazonCloudWatchEMFProvider(namespace=namespace)
@@ -51,11 +53,11 @@ def serialize_metrics(
5153

5254

5355
def serialize_single_metric(
54-
metric: Dict,
55-
dimension: Dict,
56+
metric: dict[str, Any],
57+
dimension: dict[str, Any],
5658
namespace: str,
57-
metadata: Optional[Dict] = None,
58-
timestamp: Union[int, datetime.datetime, None] = None,
59+
metadata: dict[str, Any] | None = None,
60+
timestamp: int | datetime.datetime | None = None,
5961
) -> CloudWatchEMFOutput:
6062
"""Helper function to build EMF object from a given metric, dimension and namespace"""
6163
my_metrics = AmazonCloudWatchEMFProvider(namespace=namespace)
@@ -71,7 +73,7 @@ def serialize_single_metric(
7173
return my_metrics.serialize_metric_set()
7274

7375

74-
def remove_timestamp(metrics: List):
76+
def remove_timestamp(metrics: list):
7577
"""Helper function to remove Timestamp key from EMF objects as they're built at serialization"""
7678
for metric in metrics:
7779
del metric["_aws"]["Timestamp"]
@@ -81,7 +83,7 @@ def capture_metrics_output(capsys):
8183
return json.loads(capsys.readouterr().out.strip())
8284

8385

84-
def capture_metrics_output_multiple_emf_objects(capsys) -> List[CloudWatchEMFOutput]:
86+
def capture_metrics_output_multiple_emf_objects(capsys) -> list[CloudWatchEMFOutput]:
8587
return [json.loads(line.strip()) for line in capsys.readouterr().out.split("\n") if line]
8688

8789

tests/functional/metrics/required_dependencies/test_metrics_provider.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
13
import json
2-
from typing import Any, List
4+
from typing import Any
35

46
from aws_lambda_powertools.metrics import (
57
SchemaValidationError,
@@ -15,9 +17,9 @@ def capture_metrics_output(capsys):
1517

1618
class FakeMetricsProvider(BaseProvider):
1719
def __init__(self):
18-
self.metric_store: List = []
20+
self.metric_store: list = []
1921

20-
def add_metric(self, name: str, value: float, tag: List = None, *args, **kwargs):
22+
def add_metric(self, name: str, value: float, tag: list = None, *args, **kwargs):
2123
self.metric_store.append({"name": name, "value": value})
2224

2325
def serialize_metric_set(self, raise_on_empty_metrics: bool = False, *args, **kwargs):

tests/unit/metrics/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35

tests/unit/metrics/test_functions.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import warnings
24

35
import pytest

tests/unit/metrics/test_unit_datadog.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from aws_lambda_powertools.metrics.exceptions import SchemaValidationError

0 commit comments

Comments
 (0)