Skip to content

Commit 9e72a3f

Browse files
committed
refactor(tracing): add from __future__ import annotations
and update code according to ruff rules TCH, UP006, UP007, UP037 and FA100.
1 parent 9022bd3 commit 9e72a3f

File tree

2 files changed

+85
-76
lines changed

2 files changed

+85
-76
lines changed

aws_lambda_powertools/tracing/base.py

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
from __future__ import annotations
2+
13
import abc
2-
import numbers
3-
import traceback
44
from contextlib import contextmanager
5-
from typing import Any, Generator, List, Optional, Sequence, Union
5+
from typing import TYPE_CHECKING, Any, Generator, Sequence
6+
7+
if TYPE_CHECKING:
8+
import numbers
9+
import traceback
610

711

812
class BaseSegment(abc.ABC):
913
"""Holds common properties and methods on segment and subsegment."""
1014

1115
@abc.abstractmethod
12-
def close(self, end_time: Optional[int] = None):
16+
def close(self, end_time: int | None = None):
1317
"""Close the trace entity by setting `end_time`
1418
and flip the in progress flag to False.
1519
@@ -28,7 +32,7 @@ def remove_subsegment(self, subsegment: Any):
2832
"""Remove input subsegment from child subsegments."""
2933

3034
@abc.abstractmethod
31-
def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> None:
35+
def put_annotation(self, key: str, value: str | numbers.Number | bool) -> None:
3236
"""Annotate segment or subsegment with a key-value pair.
3337
3438
Note: Annotations will be indexed for later search query.
@@ -37,7 +41,7 @@ def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> N
3741
----------
3842
key: str
3943
Metadata key
40-
value: Union[str, numbers.Number, bool]
44+
value: str | numbers.Number | bool
4145
Annotation value
4246
"""
4347

@@ -52,19 +56,19 @@ def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None
5256
Metadata key
5357
value: Any
5458
Any object that can be serialized into a JSON string
55-
namespace: Set[str]
59+
namespace: set[str]
5660
Metadata namespace, by default 'default'
5761
"""
5862

5963
@abc.abstractmethod
60-
def add_exception(self, exception: BaseException, stack: List[traceback.StackSummary], remote: bool = False):
64+
def add_exception(self, exception: BaseException, stack: list[traceback.StackSummary], remote: bool = False):
6165
"""Add an exception to trace entities.
6266
6367
Parameters
6468
----------
6569
exception: Exception
6670
Caught exception
67-
stack: List[traceback.StackSummary]
71+
stack: list[traceback.StackSummary]
6872
List of traceback summaries
6973
7074
Output from `traceback.extract_stack()`.
@@ -83,7 +87,7 @@ def in_subsegment(self, name=None, **kwargs) -> Generator[BaseSegment, None, Non
8387
----------
8488
name: str
8589
Subsegment name
86-
kwargs: Optional[dict]
90+
kwargs: dict | None
8791
Optional parameters to be propagated to segment
8892
"""
8993

@@ -96,12 +100,12 @@ def in_subsegment_async(self, name=None, **kwargs) -> Generator[BaseSegment, Non
96100
----------
97101
name: str
98102
Subsegment name
99-
kwargs: Optional[dict]
103+
kwargs: dict | None
100104
Optional parameters to be propagated to segment
101105
"""
102106

103107
@abc.abstractmethod
104-
def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> None:
108+
def put_annotation(self, key: str, value: str | numbers.Number | bool) -> None:
105109
"""Annotate current active trace entity with a key-value pair.
106110
107111
Note: Annotations will be indexed for later search query.
@@ -110,7 +114,7 @@ def put_annotation(self, key: str, value: Union[str, numbers.Number, bool]) -> N
110114
----------
111115
key: str
112116
Metadata key
113-
value: Union[str, numbers.Number, bool]
117+
value: str | numbers.Number | bool
114118
Annotation value
115119
"""
116120

@@ -126,7 +130,7 @@ def put_metadata(self, key: str, value: Any, namespace: str = "default") -> None
126130
Metadata key
127131
value: Any
128132
Any object that can be serialized into a JSON string
129-
namespace: Set[str]
133+
namespace: set[str]
130134
Metadata namespace, by default 'default'
131135
"""
132136

@@ -136,7 +140,7 @@ def patch(self, modules: Sequence[str]) -> None:
136140
137141
Parameters
138142
----------
139-
modules: Set[str]
143+
modules: set[str]
140144
Set of modules to be patched
141145
"""
142146

0 commit comments

Comments
 (0)