Skip to content

Commit cd89128

Browse files
authored
[profiling.sampling] Update for Python 3.15.0rc3 (#16478)
- Add `_FlamegraphCollectorStats` and `_ThreadStatusCounts` typed dicts - Add class attributes - Add `_typeshed.SupportsKeysAndGet`
1 parent 5251c31 commit cd89128

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎stdlib/_typeshed/__init__.pyi‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ class SupportsKeysAndGetItem(Protocol[_KT, _VT_co]):
189189
def keys(self) -> Iterable[_KT]: ...
190190
def __getitem__(self, key: _KT, /) -> _VT_co: ...
191191

192+
class SupportsKeysAndGet(Protocol[_KT, _VT_co]): # type: ignore[misc] # Covariant type as parameter
193+
def keys(self) -> Iterable[_KT]: ...
194+
195+
@overload
196+
def get(self, key: _KT, /) -> _VT_co | None: ...
197+
@overload
198+
def get( # pyrefly: ignore[invalid-variance]
199+
self, key: _KT, default: _VT_co, / # type: ignore[misc] # pyright: ignore[reportGeneralTypeIssues] # Covariant type as parameter
200+
) -> _VT_co: ...
201+
@overload
202+
def get(self, key: _KT, default: _T, /) -> _VT_co | _T: ...
203+
192204
# stable
193205
class SupportsGetItem(Protocol[_KT_contra, _VT_co]):
194206
def __getitem__(self, key: _KT_contra, /) -> _VT_co: ...

‎stdlib/profiling/sampling/stack_collector.pyi‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,58 @@
1-
from _typeshed import StrOrBytesPath, SupportsGet
1+
from _typeshed import StrOrBytesPath, SupportsGet, SupportsKeysAndGet
22
from abc import ABCMeta
3+
from collections import Counter
34
from collections.abc import Sequence
5+
from typing import Literal, TypeAlias, TypedDict, type_check_only
6+
from typing_extensions import Required
47

58
from _remote_debugging import AwaitedInfo, InterpreterInfo
69

7-
from .collector import Collector, _Frame, _Timestamps
10+
from .collector import Collector, _Frame, _Location, _Timestamps
11+
12+
# type check only
13+
# TODO: move to binary_collector.pyi when that is added
14+
_CaptureFeatureKeys: TypeAlias = Literal["all_threads", "native", "gc", "opcodes", "blocking"]
815

916
class StackTraceCollector(Collector, metaclass=ABCMeta):
17+
sample_interval_usec: int
18+
skip_idle: bool
19+
1020
def __init__(self, sample_interval_usec: int, *, skip_idle: bool = False) -> None: ...
1121
def collect(
1222
self, stack_frames: Sequence[InterpreterInfo] | Sequence[AwaitedInfo], timestamps_us: _Timestamps = None
1323
) -> None: ...
1424
def process_frames(self, frames: Sequence[_Frame], thread_id: int, weight: int = 1) -> None: ...
1525

1626
class CollapsedStackCollector(StackTraceCollector):
27+
stack_counter: Counter[tuple[tuple[str, _Location, str], int]]
1728
def __init__(self, sample_interval_usec: int, *, skip_idle: bool = False) -> None: ...
1829
def process_frames(self, frames: Sequence[_Frame], thread_id: int, weight: int = 1) -> None: ...
1930
def export(self, filename: StrOrBytesPath) -> None: ...
2031

32+
@type_check_only
33+
class _FlamegraphCollectorStats(TypedDict, total=False):
34+
sample_interval_usec: Required[int]
35+
duration_sec: float
36+
sample_rate: float
37+
error_rate: float | None
38+
missed_samples: float | None
39+
mode: int | None
40+
41+
@type_check_only
42+
class _ThreadStatusCounts(TypedDict):
43+
has_gil: int
44+
on_cpu: int
45+
gil_requested: int
46+
unknown: int
47+
has_exception: int
48+
total: int
49+
2150
class FlamegraphCollector(StackTraceCollector):
51+
stats: _FlamegraphCollectorStats
52+
thread_status_counts: _ThreadStatusCounts
53+
samples_with_gc_frames: int
54+
per_thread_stats: dict[int, _ThreadStatusCounts]
55+
2256
def __init__(self, sample_interval_usec: int, *, skip_idle: bool = False) -> None: ...
2357
def collect(
2458
self, stack_frames: Sequence[InterpreterInfo] | Sequence[AwaitedInfo], timestamps_us: _Timestamps = None
@@ -33,8 +67,21 @@ class FlamegraphCollector(StackTraceCollector):
3367
mode: int | None = None,
3468
) -> None: ...
3569
def set_replay_stats(self, info: SupportsGet[str, float | None]) -> None: ...
70+
def set_mode(self, mode: int | None) -> None: ...
3671
def export(self, filename: StrOrBytesPath) -> None: ...
3772
def process_frames(self, frames: Sequence[_Frame], thread_id: int, weight: int = 1) -> None: ...
3873

3974
class DiffFlamegraphCollector(FlamegraphCollector):
40-
def __init__(self, sample_interval_usec: int, *, baseline_binary_path: StrOrBytesPath, skip_idle: bool = False) -> None: ...
75+
baseline_binary_path: StrOrBytesPath
76+
mode: int | None
77+
capture_config: SupportsKeysAndGet[_CaptureFeatureKeys, bool] | None
78+
79+
def __init__(
80+
self,
81+
sample_interval_usec: int,
82+
*,
83+
baseline_binary_path: StrOrBytesPath,
84+
skip_idle: bool = False,
85+
mode: int | None = None,
86+
capture_config: SupportsKeysAndGet[_CaptureFeatureKeys, bool] | None = None,
87+
) -> None: ...

0 commit comments

Comments
 (0)