Skip to content

Commit a637739

Browse files
committed
add types to enhancements modules
1 parent 8c59917 commit a637739

File tree

4 files changed

+136
-55
lines changed

4 files changed

+136
-55
lines changed

src/sentry/grouping/enhancer/__init__.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,12 @@ class Enhancements:
145145
# See ``GroupingConfigLoader._get_enhancements`` in src/sentry/grouping/api.py.
146146

147147
def __init__(
148-
self, rules, rust_enhancements: RustEnhancements, version=None, bases=None, id=None
148+
self,
149+
rules: list[EnhancementRule],
150+
rust_enhancements: RustEnhancements,
151+
version: int | None = None,
152+
bases: list[str] | None = None,
153+
id: str | None = None,
149154
):
150155
self.id = id
151156
self.rules = rules
@@ -286,8 +291,8 @@ def assemble_stacktrace_component(
286291

287292
return stacktrace_component
288293

289-
def as_dict(self, with_rules=False):
290-
rv = {
294+
def as_dict(self, with_rules: bool = False) -> EnhancementsDict:
295+
rv: EnhancementsDict = {
291296
"id": self.id,
292297
"bases": self.bases,
293298
"latest": projectoptions.lookup_well_known_key(
@@ -299,7 +304,8 @@ def as_dict(self, with_rules=False):
299304
rv["rules"] = [x.as_dict() for x in self.rules]
300305
return rv
301306

302-
def _to_config_structure(self):
307+
def _to_config_structure(self) -> list[Any]:
308+
# TODO: Can we switch this to a tuple so we can type it more exactly?
303309
return [
304310
self.version,
305311
self.bases,
@@ -312,7 +318,11 @@ def dumps(self) -> str:
312318
return base64.urlsafe_b64encode(compressed).decode("ascii").strip("=")
313319

314320
@classmethod
315-
def _from_config_structure(cls, data, rust_enhancements: RustEnhancements) -> Enhancements:
321+
def _from_config_structure(
322+
cls,
323+
data: list[Any],
324+
rust_enhancements: RustEnhancements,
325+
) -> Enhancements:
316326
version, bases, rules = data
317327
if version not in VERSIONS:
318328
raise ValueError("Unknown version")
@@ -324,7 +334,7 @@ def _from_config_structure(cls, data, rust_enhancements: RustEnhancements) -> En
324334
)
325335

326336
@classmethod
327-
def loads(cls, data) -> Enhancements:
337+
def loads(cls, data: str | bytes) -> Enhancements:
328338
if isinstance(data, str):
329339
data = data.encode("ascii", "ignore")
330340
padded = data + b"=" * (4 - (len(data) % 4))
@@ -344,7 +354,9 @@ def loads(cls, data) -> Enhancements:
344354

345355
@classmethod
346356
@sentry_sdk.tracing.trace
347-
def from_config_string(cls, s, bases=None, id=None) -> Enhancements:
357+
def from_config_string(
358+
cls, s: str, bases: list[str] | None = None, id: str | None = None
359+
) -> Enhancements:
348360
rust_enhancements = parse_rust_enhancements("config_string", s)
349361

350362
rules = parse_enhancements(s)

src/sentry/grouping/enhancer/actions.py

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
from __future__ import annotations
22

33
from collections.abc import Callable, Sequence
4-
from typing import Any
4+
from typing import TYPE_CHECKING, Any
55

6+
from sentry.grouping.component import BaseGroupingComponent
7+
from sentry.grouping.enhancer.matchers import MatchFrame
68
from sentry.utils.safe import get_path, set_path
79

810
from .exceptions import InvalidEnhancerConfig
911

12+
if TYPE_CHECKING:
13+
from sentry.grouping.enhancer.rules import EnhancementRule
14+
1015
ACTIONS = ["group", "app"]
1116
ACTION_BITSIZE = 8
1217
# Ensure that the number of possible actions is smaller than the number of numbers which can be
@@ -30,14 +35,18 @@ class EnhancementAction:
3035
def apply_modifications_to_frame(
3136
self,
3237
frames: Sequence[dict[str, Any]],
33-
match_frames: Sequence[dict[str, Any]],
38+
match_frames: list[MatchFrame],
3439
idx: int,
35-
rule: Any = None,
40+
rule: Any | None = None,
3641
) -> None:
3742
pass
3843

3944
def update_frame_components_contributions(
40-
self, components, frames: Sequence[dict[str, Any]], idx, rule=None
45+
self,
46+
components: list[BaseGroupingComponent],
47+
frames: list[dict[str, Any]],
48+
idx: int,
49+
rule: Any | None = None,
4150
) -> None:
4251
pass
4352

@@ -55,7 +64,7 @@ def is_updater(self) -> bool:
5564
return self._is_updater
5665

5766
@classmethod
58-
def _from_config_structure(cls, val, version: int):
67+
def _from_config_structure(cls, val: list[str] | int, version: int) -> EnhancementAction:
5968
if isinstance(val, list): # This is a `VarAction`
6069
variable, value = val
6170
return VarAction(variable, value)
@@ -88,7 +97,7 @@ def __str__(self) -> str:
8897
self.key,
8998
)
9099

91-
def _to_config_structure(self, version: int):
100+
def _to_config_structure(self, version: int) -> int:
92101
"""
93102
Convert the action into an integer by
94103
- converting the combination of its boolean value (if it's a `+app/+group` rule or a
@@ -100,7 +109,7 @@ def _to_config_structure(self, version: int):
100109
"""
101110
return ACTIONS.index(self.key) | (ACTION_FLAGS[self.flag, self.range] << ACTION_BITSIZE)
102111

103-
def _slice_to_range(self, seq, idx):
112+
def _slice_to_range(self, seq: list[Any], idx: int) -> list[Any]:
104113
if self.range is None:
105114
return [seq[idx]]
106115
elif self.range == "down":
@@ -122,17 +131,21 @@ def _in_app_changed(self, frame: dict[str, Any]) -> bool:
122131
def apply_modifications_to_frame(
123132
self,
124133
frames: Sequence[dict[str, Any]],
125-
match_frames: Sequence[dict[str, Any]],
134+
match_frames: list[MatchFrame],
126135
idx: int,
127-
rule: Any = None,
136+
rule: Any | None = None,
128137
) -> None:
129138
# Change a frame or many to be in_app
130139
if self.key == "app":
131140
for match_frame in self._slice_to_range(match_frames, idx):
132141
match_frame["in_app"] = self.flag
133142

134143
def update_frame_components_contributions(
135-
self, components, frames: Sequence[dict[str, Any]], idx, rule=None
144+
self,
145+
components: list[BaseGroupingComponent],
146+
frames: list[dict[str, Any]],
147+
idx: int,
148+
rule: EnhancementRule | None = None,
136149
) -> None:
137150
rule_hint = "stack trace rule"
138151
if rule:
@@ -184,19 +197,20 @@ def __init__(self, var: str, value: str) -> None:
184197
def __str__(self) -> str:
185198
return f"{self.var}={self.value}"
186199

187-
def _to_config_structure(self, version):
200+
def _to_config_structure(self, version: int) -> list[str | int]:
201+
# TODO: Can we switch this to a tuple so we can type it more exactly?
188202
return [self.var, self.value]
189203

190-
def modify_stacktrace_state(self, state, rule):
204+
def modify_stacktrace_state(self, state, rule) -> None:
191205
if self.var not in VarAction._FRAME_VARIABLES:
192206
state.set(self.var, self.value, rule)
193207

194208
def apply_modifications_to_frame(
195209
self,
196210
frames: Sequence[dict[str, Any]],
197-
match_frames: Sequence[dict[str, Any]],
211+
match_frames: list[MatchFrame],
198212
idx: int,
199-
rule: Any = None,
213+
rule: Any | None = None,
200214
) -> None:
201215
if self.var == "category":
202216
frame = frames[idx]

0 commit comments

Comments
 (0)