1
+ from __future__ import annotations
2
+
1
3
import contextlib
2
4
import functools
3
5
import inspect
4
6
import logging
5
7
import os
6
8
from abc import ABC , abstractmethod
7
9
from contextlib import asynccontextmanager , contextmanager
8
- from typing import Any , AsyncGenerator , Callable , Generator , Optional , Sequence , Union , cast , overload
10
+ from typing import TYPE_CHECKING , Any , AsyncGenerator , Callable , Generator , Sequence , cast , overload
9
11
10
12
from aws_lambda_powertools .shared import constants
11
13
from aws_lambda_powertools .shared .functions import resolve_truthy_env_var_choice , sanitize_xray_segment_name
12
14
from aws_lambda_powertools .shared .types import AnyCallableT
13
- from aws_lambda_powertools .tracing .base import BaseSegment
15
+
16
+ if TYPE_CHECKING :
17
+ from aws_lambda_powertools .tracing .base import BaseSegment
14
18
15
19
logger = logging .getLogger (__name__ )
16
20
@@ -32,7 +36,7 @@ def set_attribute(self, key: str, value: Any, **kwargs) -> None:
32
36
Attribute key
33
37
value: Any
34
38
Attribute value
35
- kwargs: Optional[ dict]
39
+ kwargs: dict | None
36
40
Optional parameters
37
41
"""
38
42
@@ -43,8 +47,8 @@ def record_exception(self, exception: BaseException, **kwargs):
43
47
Parameters
44
48
----------
45
49
exception: Exception
46
- Caught exception during the exectution of this Span
47
- kwargs: Optional[ dict]
50
+ Caught exception during the execution of this Span
51
+ kwargs: dict | None
48
52
Optional parameters
49
53
"""
50
54
@@ -71,7 +75,7 @@ def trace(self, name: str, **kwargs) -> Generator[BaseSpan, None, None]:
71
75
----------
72
76
name: str
73
77
Span name
74
- kwargs: Optional[ dict]
78
+ kwargs: dict | None
75
79
Optional parameters to be propagated to the span
76
80
"""
77
81
@@ -89,7 +93,7 @@ def trace_async(self, name: str, **kwargs) -> AsyncGenerator[BaseSpan, None]:
89
93
----------
90
94
name: str
91
95
Span name
92
- kwargs: Optional[ dict]
96
+ kwargs: dict | None
93
97
Optional parameters to be propagated to the span
94
98
"""
95
99
@@ -103,7 +107,7 @@ def set_attribute(self, key: str, value: Any, **kwargs) -> None:
103
107
attribute key
104
108
value: Any
105
109
attribute value
106
- kwargs: Optional[ dict]
110
+ kwargs: dict | None
107
111
Optional parameters to be propagated to the span
108
112
"""
109
113
@@ -132,8 +136,8 @@ def patch_all(self) -> None:
132
136
def capture_lambda_handler (
133
137
self ,
134
138
lambda_handler : Any = None ,
135
- capture_response : Optional [ bool ] = None ,
136
- capture_error : Optional [ bool ] = None ,
139
+ capture_response : bool | None = None ,
140
+ capture_error : bool | None = None ,
137
141
):
138
142
"""Decorator to create subsegment for lambda handlers
139
143
@@ -230,21 +234,21 @@ def decorate(event, context, **kwargs):
230
234
231
235
# see #465
232
236
@overload
233
- def capture_method (self , method : " AnyCallableT" ) -> " AnyCallableT" : ... # pragma: no cover
237
+ def capture_method (self , method : AnyCallableT ) -> AnyCallableT : ... # pragma: no cover
234
238
235
239
@overload
236
240
def capture_method (
237
241
self ,
238
242
method : None = None ,
239
- capture_response : Optional [ bool ] = None ,
240
- capture_error : Optional [ bool ] = None ,
241
- ) -> Callable [[" AnyCallableT" ], " AnyCallableT" ]: ... # pragma: no cover
243
+ capture_response : bool | None = None ,
244
+ capture_error : bool | None = None ,
245
+ ) -> Callable [[AnyCallableT ], AnyCallableT ]: ... # pragma: no cover
242
246
243
247
def capture_method (
244
248
self ,
245
- method : Optional [ AnyCallableT ] = None ,
246
- capture_response : Optional [ bool ] = None ,
247
- capture_error : Optional [ bool ] = None ,
249
+ method : AnyCallableT | None = None ,
250
+ capture_response : bool | None = None ,
251
+ capture_error : bool | None = None ,
248
252
) -> AnyCallableT :
249
253
"""Decorator to create subsegment for arbitrary functions
250
254
@@ -450,9 +454,9 @@ async def async_tasks():
450
454
def _decorate_async_function (
451
455
self ,
452
456
method : Callable ,
453
- capture_response : Optional [ Union [ bool , str ]] = None ,
454
- capture_error : Optional [ Union [ bool , str ]] = None ,
455
- method_name : Optional [ str ] = None ,
457
+ capture_response : bool | str | None = None ,
458
+ capture_error : bool | str | None = None ,
459
+ method_name : str | None = None ,
456
460
):
457
461
@functools .wraps (method )
458
462
async def decorate (* args , ** kwargs ):
@@ -483,9 +487,9 @@ async def decorate(*args, **kwargs):
483
487
def _decorate_generator_function (
484
488
self ,
485
489
method : Callable ,
486
- capture_response : Optional [ Union [ bool , str ]] = None ,
487
- capture_error : Optional [ Union [ bool , str ]] = None ,
488
- method_name : Optional [ str ] = None ,
490
+ capture_response : bool | str | None = None ,
491
+ capture_error : bool | str | None = None ,
492
+ method_name : str | None = None ,
489
493
):
490
494
@functools .wraps (method )
491
495
def decorate (* args , ** kwargs ):
@@ -516,9 +520,9 @@ def decorate(*args, **kwargs):
516
520
def _decorate_generator_function_with_context_manager (
517
521
self ,
518
522
method : Callable ,
519
- capture_response : Optional [ Union [ bool , str ]] = None ,
520
- capture_error : Optional [ Union [ bool , str ]] = None ,
521
- method_name : Optional [ str ] = None ,
523
+ capture_response : bool | str | None = None ,
524
+ capture_error : bool | str | None = None ,
525
+ method_name : str | None = None ,
522
526
):
523
527
@functools .wraps (method )
524
528
@contextlib .contextmanager
@@ -550,9 +554,9 @@ def decorate(*args, **kwargs):
550
554
def _decorate_sync_function (
551
555
self ,
552
556
method : AnyCallableT ,
553
- capture_response : Optional [ Union [ bool , str ]] = None ,
554
- capture_error : Optional [ Union [ bool , str ]] = None ,
555
- method_name : Optional [ str ] = None ,
557
+ capture_response : bool | str | None = None ,
558
+ capture_error : bool | str | None = None ,
559
+ method_name : str | None = None ,
556
560
) -> AnyCallableT :
557
561
@functools .wraps (method )
558
562
def decorate (* args , ** kwargs ):
@@ -582,10 +586,10 @@ def decorate(*args, **kwargs):
582
586
583
587
def _add_response_as_metadata (
584
588
self ,
585
- method_name : Optional [ str ] = None ,
586
- data : Optional [ Any ] = None ,
587
- subsegment : Optional [ BaseSegment ] = None ,
588
- capture_response : Optional [ Union [ bool , str ]] = None ,
589
+ method_name : str | None = None ,
590
+ data : Any | None = None ,
591
+ subsegment : BaseSegment | None = None ,
592
+ capture_response : bool | str | None = None ,
589
593
):
590
594
"""Add response as metadata for given subsegment
591
595
@@ -610,7 +614,7 @@ def _add_full_exception_as_metadata(
610
614
method_name : str ,
611
615
error : Exception ,
612
616
subsegment : BaseSegment ,
613
- capture_error : Optional [ bool ] = None ,
617
+ capture_error : bool | None = None ,
614
618
):
615
619
"""Add full exception object as metadata for given subsegment
616
620
0 commit comments