1515"""
1616
1717import sys
18+ from typing import TYPE_CHECKING
1819
1920import pytest
2021
2122
23+ if TYPE_CHECKING :
24+ # We need the pyright: ignore because pprof_pb2 does not exist as a real module, only as a pyi.
25+ from tests .profiling .collector import pprof_pb2 # pyright: ignore[reportMissingModuleSource]
26+
27+
2228# Evaluated in the PARENT interpreter (subprocess bodies cannot express a skip:
2329# an in-body ``pytest.skip`` would surface as a non-zero exit and FAIL the outer
2430# test). ``test_hook_hits()`` is a read-only counter query with no side effects —
@@ -170,11 +176,11 @@ def test_profiler_keeps_managed_heap_when_native_heap_armed() -> None:
170176 with mock .patch .object (memalloc , "set_native_heap_partition" ) as set_partition :
171177 from ddtrace .profiling .profiler import Profiler
172178
173- prof = Profiler ()
179+ prof : Profiler = Profiler ()
174180 prof .start ()
175181 try :
176182 assert install .called , "the gotter must still be armed when native heap is enabled"
177- has_mem = any (isinstance (c , memalloc .MemoryCollector ) for c in prof ._profiler ._collectors )
183+ has_mem : bool = any (isinstance (c , memalloc .MemoryCollector ) for c in prof ._profiler ._collectors )
178184 assert has_mem , (
179185 "in-process managed-heap (OBJ/MEM) collector must stay active when the gotter is armed; "
180186 "the gotter owns only the native/raw glibc malloc domain"
@@ -204,10 +210,10 @@ def test_profiler_keeps_managed_heap_when_gotter_not_installed() -> None:
204210 with mock .patch .object (memalloc , "set_native_heap_partition" ) as set_partition :
205211 from ddtrace .profiling .profiler import Profiler
206212
207- prof = Profiler ()
213+ prof : Profiler = Profiler ()
208214 prof .start ()
209215 try :
210- has_mem = any (isinstance (c , memalloc .MemoryCollector ) for c in prof ._profiler ._collectors )
216+ has_mem : bool = any (isinstance (c , memalloc .MemoryCollector ) for c in prof ._profiler ._collectors )
211217 assert has_mem , "in-process memory collector must stay active when the gotter fails to install"
212218 # Not armed -> partition off so ALL sizes keep being sampled.
213219 set_partition .assert_called_once_with (False )
@@ -234,11 +240,11 @@ def test_profiler_keeps_managed_heap_when_native_heap_disabled() -> None:
234240 with mock .patch .object (memalloc , "set_native_heap_partition" ) as set_partition :
235241 from ddtrace .profiling .profiler import Profiler
236242
237- prof = Profiler ()
243+ prof : Profiler = Profiler ()
238244 prof .start ()
239245 try :
240246 assert not install .called
241- has_mem = any (isinstance (c , memalloc .MemoryCollector ) for c in prof ._profiler ._collectors )
247+ has_mem : bool = any (isinstance (c , memalloc .MemoryCollector ) for c in prof ._profiler ._collectors )
242248 assert has_mem , "in-process memory collector must run when native heap is disabled"
243249 # Feature off -> partition off (all sizes sampled).
244250 set_partition .assert_called_once_with (False )
@@ -313,8 +319,8 @@ def test_native_heap_ownership_handoff_end_to_end() -> None:
313319 assert heap_gotter .install () is True
314320 assert heap_gotter .is_installed () is True
315321
316- prefix = os .path .join (tempfile .mkdtemp (), "handoff" )
317- output_filename = prefix + "." + str (os .getpid ())
322+ prefix : str = os .path .join (tempfile .mkdtemp (), "handoff" )
323+ output_filename : str = prefix + "." + str (os .getpid ())
318324 ddup .config (
319325 service = "test_native_heap_ownership_handoff" ,
320326 version = "test" ,
@@ -324,7 +330,7 @@ def test_native_heap_ownership_handoff_end_to_end() -> None:
324330 ddup .start ()
325331
326332 store : list [object ] = []
327- mc = memalloc .MemoryCollector (heap_sample_size = 64 * 1024 )
333+ mc : memalloc . MemoryCollector = memalloc .MemoryCollector (heap_sample_size = 64 * 1024 )
328334 memalloc .set_native_heap_partition (True )
329335 try :
330336 with mc :
@@ -333,19 +339,19 @@ def test_native_heap_ownership_handoff_end_to_end() -> None:
333339 # raw malloc (it is NOT sampling-gated), so background allocations
334340 # can only inflate the delta — never shrink it below the number of
335341 # large buffers we deliberately allocate.
336- hits_before = heap_gotter .test_hook_hits ()
342+ hits_before : "int | None" = heap_gotter .test_hook_hits ()
337343 _allocate_large_buffers (store )
338- hits_after = heap_gotter .test_hook_hits ()
344+ hits_after : "int | None" = heap_gotter .test_hook_hits ()
339345
340346 _allocate_small_objects (store )
341347 mc .snapshot ()
342348 ddup .upload ()
343349
344- profile = pprof_utils .parse_newest_profile (output_filename )
345- heap_samples = pprof_utils .get_samples_with_value_type (profile , "heap-space" )
350+ profile : "pprof_pb2.Profile" = pprof_utils .parse_newest_profile (output_filename )
351+ heap_samples : "list[pprof_pb2.Sample]" = pprof_utils .get_samples_with_value_type (profile , "heap-space" )
346352
347353 # (a) In-process producer dropped the > 512B tail ...
348- large_count = _count_heap_samples_with_function (profile , heap_samples , "_allocate_large_buffers" )
354+ large_count : int = _count_heap_samples_with_function (profile , heap_samples , "_allocate_large_buffers" )
349355 assert large_count == 0 , (
350356 f"partition ON: > 512B managed allocations must NOT be sampled in-process (got { large_count } )"
351357 )
@@ -354,15 +360,15 @@ def test_native_heap_ownership_handoff_end_to_end() -> None:
354360 # is a single raw malloc routed through the patched GOT, so the hook-hit
355361 # counter must advance by at least the number of large buffers.
356362 assert hits_before is not None and hits_after is not None
357- delta = hits_after - hits_before
363+ delta : int = hits_after - hits_before
358364 assert delta >= _PARTITION_LARGE_ALLOC_COUNT , (
359365 "native gotter must capture the > 512B raw-malloc tail the in-process sampler dropped "
360366 f"(hook-hit delta { delta } < { _PARTITION_LARGE_ALLOC_COUNT } large allocations)"
361367 )
362368
363369 # Control: <= 512B pool-served allocations are invisible to the gotter
364370 # and must still be sampled in-process — the partition splits by size.
365- small_count = _count_heap_samples_with_function (profile , heap_samples , "_allocate_small_objects" )
371+ small_count : int = _count_heap_samples_with_function (profile , heap_samples , "_allocate_small_objects" )
366372 assert small_count > 0 , "partition ON: <= 512B managed allocations must still be sampled in-process"
367373 finally :
368374 # Reset the process-global flag so it cannot bleed into other tests
0 commit comments