Skip to content

Commit 0a26a83

Browse files
authored
[aws-xray-sdk] Improve stubs (#14277)
1 parent 1c02239 commit 0a26a83

36 files changed

+326
-269
lines changed

stubs/aws-xray-sdk/@tests/stubtest_allowlist.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
aws_xray_sdk.core.models.subsegment.subsegment_decorator
22
aws_xray_sdk.core.sampling.connector.ServiceConnector.fetch_sampling_rules
3-
aws_xray_sdk.core.sampling.sampler.ServiceConnector.fetch_sampling_rules
43

54
# We can not import 3rd-party libraries in teststubs runtime,
65
# but we can use Protocol to replace this types:

stubs/aws-xray-sdk/aws_xray_sdk/core/emitters/udp_emitter.pyi

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
from logging import Logger
2+
from typing import Final
23

3-
from aws_xray_sdk.core.daemon_config import DaemonConfig as DaemonConfig
4-
5-
from ..exceptions.exceptions import InvalidDaemonAddressException as InvalidDaemonAddressException
4+
from aws_xray_sdk.core.models.entity import Entity
65

76
log: Logger
8-
PROTOCOL_HEADER: str
9-
PROTOCOL_DELIMITER: str
10-
DEFAULT_DAEMON_ADDRESS: str
7+
PROTOCOL_HEADER: Final[str]
8+
PROTOCOL_DELIMITER: Final[str]
9+
DEFAULT_DAEMON_ADDRESS: Final[str]
1110

1211
class UDPEmitter:
13-
def __init__(self, daemon_address="127.0.0.1:2000") -> None: ...
14-
def send_entity(self, entity) -> None: ...
15-
def set_daemon_address(self, address) -> None: ...
12+
def __init__(self, daemon_address: str = "127.0.0.1:2000") -> None: ...
13+
def send_entity(self, entity: Entity) -> None: ...
14+
def set_daemon_address(self, address: str | None) -> None: ...
1615
@property
1716
def ip(self): ...
1817
@property
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from ..utils.search_pattern import wildcard_match as wildcard_match
2-
31
class DefaultDynamicNaming:
4-
def __init__(self, pattern, fallback) -> None: ...
5-
def get_name(self, host_name): ...
2+
def __init__(self, pattern: str, fallback: str) -> None: ...
3+
def get_name(self, host_name: str | None) -> str: ...
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,8 @@
1-
from .noop_traceid import NoOpTraceId as NoOpTraceId
2-
from .segment import Segment as Segment
3-
from .subsegment import Subsegment as Subsegment
4-
from .traceid import TraceId as TraceId
1+
from .segment import Segment
2+
from .subsegment import Subsegment
53

64
class DummySegment(Segment):
7-
sampled: bool
85
def __init__(self, name: str = "dummy") -> None: ...
9-
def set_aws(self, aws_meta) -> None: ...
10-
def put_http_meta(self, key, value) -> None: ...
11-
def put_annotation(self, key, value) -> None: ...
12-
def put_metadata(self, key, value, namespace: str = "default") -> None: ...
13-
def set_user(self, user) -> None: ...
14-
def set_service(self, service_info) -> None: ...
15-
def apply_status_code(self, status_code) -> None: ...
16-
def add_exception(self, exception, stack, remote: bool = False) -> None: ...
17-
def serialize(self) -> None: ...
186

197
class DummySubsegment(Subsegment):
20-
sampled: bool
218
def __init__(self, segment, name: str = "dummy") -> None: ...
22-
def set_aws(self, aws_meta) -> None: ...
23-
def put_http_meta(self, key, value) -> None: ...
24-
def put_annotation(self, key, value) -> None: ...
25-
def put_metadata(self, key, value, namespace: str = "default") -> None: ...
26-
def set_sql(self, sql) -> None: ...
27-
def apply_status_code(self, status_code) -> None: ...
28-
def add_exception(self, exception, stack, remote: bool = False) -> None: ...
29-
def serialize(self) -> None: ...
Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
1+
from _typeshed import Incomplete
12
from logging import Logger
23
from traceback import StackSummary
3-
from typing import Any
4+
from typing import Any, Final, Literal, overload
5+
6+
from .subsegment import Subsegment
7+
from .throwable import Throwable
48

59
log: Logger
6-
ORIGIN_TRACE_HEADER_ATTR_KEY: str
10+
ORIGIN_TRACE_HEADER_ATTR_KEY: Final[str]
711

812
class Entity:
9-
id: Any
10-
name: Any
11-
start_time: Any
12-
parent_id: Any
13+
id: str
14+
name: str
15+
start_time: float
16+
parent_id: str | None
1317
sampled: bool
1418
in_progress: bool
15-
http: Any
16-
annotations: Any
17-
metadata: Any
18-
aws: Any
19-
cause: Any
20-
subsegments: Any
21-
end_time: Any
22-
def __init__(self, name, entity_id=None) -> None: ...
23-
def close(self, end_time=None) -> None: ...
24-
def add_subsegment(self, subsegment) -> None: ...
25-
def remove_subsegment(self, subsegment) -> None: ...
26-
def put_http_meta(self, key, value) -> None: ...
27-
def put_annotation(self, key, value) -> None: ...
28-
def put_metadata(self, key, value, namespace: str = "default") -> None: ...
19+
http: dict[str, dict[str, str | int]]
20+
annotations: dict[str, float | str | bool]
21+
metadata: dict[str, dict[str, Any]] # value is any object that can be serialized into JSON string
22+
aws: dict[str, Incomplete]
23+
cause: dict[str, str | list[Throwable]]
24+
subsegments: list[Subsegment]
25+
end_time: float
26+
def __init__(self, name: str, entity_id: str | None = None) -> None: ...
27+
def close(self, end_time: float | None = None) -> None: ...
28+
def add_subsegment(self, subsegment: Subsegment) -> None: ...
29+
def remove_subsegment(self, subsegment: Subsegment) -> None: ...
30+
@overload
31+
def put_http_meta(self, key: Literal["status", "content_length"], value: int) -> None: ...
32+
@overload
33+
def put_http_meta(self, key: Literal["url", "method", "user_agent", "client_ip", "x_forwarded_for"], value: str) -> None: ...
34+
def put_annotation(self, key: str, value: float | str | bool) -> None: ...
35+
def put_metadata(
36+
self, key: str, value: Any, namespace: str = "default" # value is any object that can be serialized into JSON string
37+
) -> None: ...
2938
def set_aws(self, aws_meta) -> None: ...
3039
throttle: bool
3140
def add_throttle_flag(self) -> None: ...
3241
fault: bool
3342
def add_fault_flag(self) -> None: ...
3443
error: bool
3544
def add_error_flag(self) -> None: ...
36-
def apply_status_code(self, status_code) -> None: ...
45+
def apply_status_code(self, status_code: int | None) -> None: ...
3746
def add_exception(self, exception: Exception, stack: StackSummary, remote: bool = False) -> None: ...
3847
def save_origin_trace_header(self, trace_header) -> None: ...
3948
def get_origin_trace_header(self): ...
40-
def serialize(self): ...
41-
def to_dict(self): ...
49+
def serialize(self) -> str: ...
50+
def to_dict(self) -> dict[str, Incomplete]: ...
Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
from typing import Any
1+
from typing import Final
22

33
from .segment import Segment
44

5-
MUTATION_UNSUPPORTED_MESSAGE: str
5+
MUTATION_UNSUPPORTED_MESSAGE: Final[str]
66

77
class FacadeSegment(Segment):
8-
initializing: Any
9-
def __init__(self, name, entityid, traceid, sampled) -> None: ...
10-
def close(self, end_time=None) -> None: ...
11-
def put_http_meta(self, key, value) -> None: ...
12-
def put_annotation(self, key, value) -> None: ...
13-
def put_metadata(self, key, value, namespace: str = "default") -> None: ...
14-
def set_aws(self, aws_meta) -> None: ...
15-
def set_user(self, user) -> None: ...
16-
def add_throttle_flag(self) -> None: ...
17-
def add_fault_flag(self) -> None: ...
18-
def add_error_flag(self) -> None: ...
19-
def add_exception(self, exception, stack, remote: bool = False) -> None: ...
20-
def apply_status_code(self, status_code) -> None: ...
21-
def serialize(self) -> None: ...
22-
def ready_to_send(self): ...
23-
def increment(self) -> None: ...
24-
def decrement_ref_counter(self) -> None: ...
8+
initializing: bool
9+
def __init__(self, name: str, entityid: str | None, traceid: str | None, sampled: bool | None) -> None: ...
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from typing import Any
1+
from typing import Final
22

3-
URL: str
4-
METHOD: str
5-
USER_AGENT: str
6-
CLIENT_IP: str
7-
X_FORWARDED_FOR: str
8-
STATUS: str
9-
CONTENT_LENGTH: str
10-
XRAY_HEADER: str
11-
ALT_XRAY_HEADER: str
12-
request_keys: Any
13-
response_keys: Any
3+
URL: Final[str]
4+
METHOD: Final[str]
5+
USER_AGENT: Final[str]
6+
CLIENT_IP: Final[str]
7+
X_FORWARDED_FOR: Final[str]
8+
STATUS: Final[str]
9+
CONTENT_LENGTH: Final[str]
10+
XRAY_HEADER: Final[str]
11+
ALT_XRAY_HEADER: Final[str]
12+
request_keys: tuple[str, ...]
13+
response_keys: tuple[str, ...]
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from typing import ClassVar
2+
13
class NoOpTraceId:
2-
VERSION: str
3-
DELIMITER: str
4+
VERSION: ClassVar[str]
5+
DELIMITER: ClassVar[str]
46
start_time: str
57
def __init__(self) -> None: ...
6-
def to_id(self): ...
8+
def to_id(self) -> str: ...
Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,59 @@
1+
from _typeshed import Incomplete
12
from types import TracebackType
2-
from typing import Any
3+
from typing import Final
34

45
from ..recorder import AWSXRayRecorder
56
from ..utils.atomic_counter import AtomicCounter
7+
from .dummy_entities import DummySegment
68
from .entity import Entity
79
from .subsegment import Subsegment
810

9-
ORIGIN_TRACE_HEADER_ATTR_KEY: str
11+
ORIGIN_TRACE_HEADER_ATTR_KEY: Final[str]
1012

1113
class SegmentContextManager:
12-
name: str
13-
segment_kwargs: dict[str, Any]
14+
name: str | None
15+
segment_kwargs: dict[str, str | bool | None]
1416
recorder: AWSXRayRecorder
15-
segment: Segment
16-
def __init__(self, recorder: AWSXRayRecorder, name: str | None = None, **segment_kwargs) -> None: ...
17-
def __enter__(self): ...
17+
segment: Segment | None
18+
def __init__(
19+
self,
20+
recorder: AWSXRayRecorder,
21+
name: str | None = None,
22+
*,
23+
traceid: str | None = None,
24+
parent_id: str | None = None,
25+
sampling: bool | None = None,
26+
) -> None: ...
27+
def __enter__(self) -> DummySegment | Segment: ...
1828
def __exit__(
1929
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
2030
) -> None: ...
2131

2232
class Segment(Entity):
23-
trace_id: str | None
24-
id: str | None
33+
trace_id: str
34+
id: str
2535
in_progress: bool
2636
sampled: bool
2737
user: str | None
2838
ref_counter: AtomicCounter
29-
parent_id: str | None
39+
parent_id: str
3040
service: dict[str, str]
3141
def __init__(
32-
self, name, entityid: str | None = None, traceid: str | None = None, parent_id: str | None = None, sampled: bool = True
42+
self,
43+
name: str,
44+
entityid: str | None = None,
45+
traceid: str | None = None,
46+
parent_id: str | None = None,
47+
sampled: bool = True,
3348
) -> None: ...
3449
def add_subsegment(self, subsegment: Subsegment) -> None: ...
3550
def increment(self) -> None: ...
3651
def decrement_ref_counter(self) -> None: ...
37-
def ready_to_send(self): ...
38-
def get_total_subsegments_size(self): ...
39-
def decrement_subsegments_size(self): ...
40-
def remove_subsegment(self, subsegment) -> None: ...
52+
def ready_to_send(self) -> bool: ...
53+
def get_total_subsegments_size(self) -> int: ...
54+
def decrement_subsegments_size(self) -> int: ...
55+
def remove_subsegment(self, subsegment: Subsegment) -> None: ...
4156
def set_user(self, user) -> None: ...
42-
def set_service(self, service_info) -> None: ...
43-
def set_rule_name(self, rule_name) -> None: ...
44-
def to_dict(self): ...
57+
def set_service(self, service_info: dict[str, str]) -> None: ...
58+
def set_rule_name(self, rule_name: str) -> None: ...
59+
def to_dict(self) -> dict[str, Incomplete]: ...
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import time
1+
from _typeshed import Incomplete
22
from types import TracebackType
3-
from typing import Any
3+
from typing import Final
44

5-
from ...core import AWSXRayRecorder
5+
from ..recorder import AWSXRayRecorder
6+
from .dummy_entities import DummySubsegment
67
from .entity import Entity
78
from .segment import Segment
89

9-
SUBSEGMENT_RECORDING_ATTRIBUTE: str
10+
SUBSEGMENT_RECORDING_ATTRIBUTE: Final[str]
1011

1112
def set_as_recording(decorated_func, wrapped) -> None: ...
1213
def is_already_recording(func): ...
1314
def subsegment_decorator(wrapped, instance, args, kwargs): ...
1415

1516
class SubsegmentContextManager:
1617
name: str | None
17-
subsegment_kwargs: dict[str, Any] | None
18+
subsegment_kwargs: dict[str, str]
1819
recorder: AWSXRayRecorder
19-
subsegment: Subsegment
20-
def __init__(self, recorder: AWSXRayRecorder, name=None, **subsegment_kwargs) -> None: ...
21-
def __call__(self, wrapped, instance, args: list[Any], kwargs: dict[str, Any]): ...
22-
def __enter__(self) -> Subsegment | None: ...
20+
subsegment: Subsegment | None
21+
def __init__(self, recorder: AWSXRayRecorder, name: str | None = None, *, namespace: str = "local") -> None: ...
22+
def __call__(self, wrapped, instance, args: list[Incomplete], kwargs: dict[str, Incomplete]): ...
23+
def __enter__(self) -> DummySubsegment | Subsegment | None: ...
2324
def __exit__(
2425
self, exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None
2526
) -> None: ...
@@ -29,10 +30,10 @@ class Subsegment(Entity):
2930
trace_id: str
3031
type: str
3132
namespace: str
32-
sql: dict[str, Any]
33+
sql: dict[str, Incomplete]
3334
def __init__(self, name: str, namespace: str, segment: Segment) -> None: ...
3435
def add_subsegment(self, subsegment: Subsegment) -> None: ...
3536
def remove_subsegment(self, subsegment: Subsegment) -> None: ...
36-
def close(self, end_time: time.struct_time | None = None) -> None: ...
37-
def set_sql(self, sql: dict[str, Any]) -> None: ...
38-
def to_dict(self) -> dict[str, Any]: ...
37+
def close(self, end_time: float | None = None) -> None: ...
38+
def set_sql(self, sql: dict[str, Incomplete]) -> None: ...
39+
def to_dict(self) -> dict[str, Incomplete]: ...

0 commit comments

Comments
 (0)