Skip to content

Commit 90ffd14

Browse files
chore(profiling): improve typing in profiler.py
1 parent ba60fe5 commit 90ffd14

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

ddtrace/profiling/profiler.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import logging
33
import os
44
from typing import Any
5+
from typing import Callable
56
from typing import Dict
6-
from typing import List # noqa:F401
7-
from typing import Optional # noqa:F401
8-
from typing import Type # noqa:F401
9-
from typing import Union # noqa:F401
7+
from typing import List
8+
from typing import Optional
9+
from typing import Type
10+
from typing import Union
1011

1112
import ddtrace
1213
from ddtrace import config
@@ -29,8 +30,6 @@
2930
from ddtrace.profiling.collector import threading
3031

3132

32-
# TODO(vlad): add type annotations
33-
3433
LOG = logging.getLogger(__name__)
3534

3635

@@ -42,10 +41,10 @@ class Profiler(object):
4241
4342
"""
4443

45-
def __init__(self, *args, **kwargs):
46-
self._profiler = _ProfilerInstance(*args, **kwargs)
44+
def __init__(self, *args: Any, **kwargs: Any) -> None:
45+
self._profiler: "_ProfilerInstance" = _ProfilerInstance(*args, **kwargs)
4746

48-
def start(self, stop_on_exit=True, profile_children=True):
47+
def start(self, stop_on_exit: bool = True, profile_children: bool = True) -> None:
4948
"""Start the profiler.
5049
5150
:param stop_on_exit: Whether to stop the profiler and flush the profile on exit.
@@ -75,7 +74,7 @@ def start(self, stop_on_exit=True, profile_children=True):
7574

7675
telemetry_writer.product_activated(TELEMETRY_APM_PRODUCT.PROFILER, True)
7776

78-
def stop(self, flush=True):
77+
def stop(self, flush: bool = True) -> None:
7978
"""Stop the profiler.
8079
8180
:param flush: Flush last profile.
@@ -88,7 +87,7 @@ def stop(self, flush=True):
8887
# Not a best practice, but for backward API compatibility that allowed to call `stop` multiple times.
8988
pass
9089

91-
def _restart_on_fork(self):
90+
def _restart_on_fork(self) -> None:
9291
# Be sure to stop the parent first, since it might have to e.g. unpatch functions
9392
# Do not flush data as we don't want to have multiple copies of the parent profile exported.
9493
try:
@@ -99,11 +98,7 @@ def _restart_on_fork(self):
9998
self._profiler = self._profiler.copy()
10099
self._profiler.start()
101100

102-
def __getattr__(
103-
self,
104-
key, # type: str
105-
):
106-
# type: (...) -> Any
101+
def __getattr__(self, key: str) -> Any:
107102
return getattr(self._profiler, key)
108103

109104

@@ -145,22 +140,22 @@ def __init__(
145140
self.endpoint_collection_enabled: bool = endpoint_collection_enabled
146141

147142
# Non-user-supplied values
148-
self._collectors: List[Union[stack.StackCollector, memalloc.MemoryCollector]] = []
149-
self._collectors_on_import: Any = None
143+
self._collectors: List[collector.Collector] = []
144+
self._collectors_on_import: Optional[List[tuple[str, Callable[[Any], None]]]] = None
150145
self._scheduler: Optional[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]] = None
151146
self._lambda_function_name: Optional[str] = os.environ.get("AWS_LAMBDA_FUNCTION_NAME")
152147

153148
self.__post_init__()
154149

155-
def __eq__(self, other):
150+
def __eq__(self, other: Any) -> bool:
156151
for k, v in vars(self).items():
157152
if k.startswith("_") or k in self._COPY_IGNORE_ATTRIBUTES:
158153
continue
159154
if v != getattr(other, k, None):
160155
return False
161156
return True
162157

163-
def _build_default_exporters(self):
158+
def _build_default_exporters(self) -> None:
164159
if self._lambda_function_name is not None:
165160
self.tags.update({"functionname": self._lambda_function_name})
166161

@@ -185,9 +180,7 @@ def _build_default_exporters(self):
185180
)
186181
ddup.start()
187182

188-
def __post_init__(self):
189-
# type: (...) -> None
190-
183+
def __post_init__(self) -> None:
191184
if self._stack_collector_enabled:
192185
LOG.debug("Profiling collector (stack) enabled")
193186
try:
@@ -228,7 +221,7 @@ def start_collector(collector_class: Type) -> None:
228221

229222
if self._pytorch_collector_enabled:
230223

231-
def start_collector(collector_class: Type) -> None:
224+
def start_collector(collector_class: Type[collector.Collector]) -> None:
232225
with self._service_lock:
233226
col = collector_class()
234227

@@ -258,14 +251,16 @@ def start_collector(collector_class: Type) -> None:
258251

259252
self._build_default_exporters()
260253

261-
scheduler_class = scheduler.ServerlessScheduler if self._lambda_function_name else scheduler.Scheduler # type: (Type[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]])
254+
scheduler_class: Type[Union[scheduler.Scheduler, scheduler.ServerlessScheduler]] = (
255+
scheduler.ServerlessScheduler if self._lambda_function_name else scheduler.Scheduler
256+
)
262257

263258
self._scheduler = scheduler_class(
264259
before_flush=self._collectors_snapshot,
265260
tracer=self.tracer,
266261
)
267262

268-
def _collectors_snapshot(self):
263+
def _collectors_snapshot(self) -> None:
269264
for c in self._collectors:
270265
try:
271266
c.snapshot()
@@ -274,7 +269,7 @@ def _collectors_snapshot(self):
274269

275270
_COPY_IGNORE_ATTRIBUTES = {"status"}
276271

277-
def copy(self):
272+
def copy(self) -> "_ProfilerInstance":
278273
return self.__class__(
279274
**{
280275
key: value
@@ -283,8 +278,7 @@ def copy(self):
283278
}
284279
)
285280

286-
def _start_service(self):
287-
# type: (...) -> None
281+
def _start_service(self) -> None:
288282
"""Start the profiler."""
289283
collectors = []
290284
for col in self._collectors:
@@ -301,8 +295,7 @@ def _start_service(self):
301295
if self._scheduler is not None:
302296
self._scheduler.start()
303297

304-
def _stop_service(self, flush=True, join=True):
305-
# type: (bool, bool) -> None
298+
def _stop_service(self, flush: bool = True, join: bool = True) -> None:
306299
"""Stop the profiler.
307300
308301
:param flush: Flush a last profile.

0 commit comments

Comments
 (0)