Skip to content

Commit f742d58

Browse files
committed
add types to enhancements modules
1 parent d8ad142 commit f742d58

File tree

4 files changed

+132
-54
lines changed

4 files changed

+132
-54
lines changed

src/sentry/grouping/enhancer/__init__.py

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

142142
def __init__(
143-
self, rules, rust_enhancements: RustEnhancements, version=None, bases=None, id=None
143+
self,
144+
rules: list[EnhancementRule],
145+
rust_enhancements: RustEnhancements,
146+
version: int | None = None,
147+
bases: list[str] | None = None,
148+
id: str | None = None,
144149
):
145150
self.id = id
146151
self.rules = rules
@@ -281,8 +286,8 @@ def assemble_stacktrace_component(
281286

282287
return stacktrace_component
283288

284-
def as_dict(self, with_rules=False):
285-
rv = {
289+
def as_dict(self, with_rules: bool = False) -> EnhancementsDict:
290+
rv: EnhancementsDict = {
286291
"id": self.id,
287292
"bases": self.bases,
288293
"latest": projectoptions.lookup_well_known_key(
@@ -294,7 +299,8 @@ def as_dict(self, with_rules=False):
294299
rv["rules"] = [x.as_dict() for x in self.rules]
295300
return rv
296301

297-
def _to_config_structure(self):
302+
def _to_config_structure(self) -> list[Any]:
303+
# TODO: Can we switch this to a tuple so we can type it more exactly?
298304
return [
299305
self.version,
300306
self.bases,
@@ -307,7 +313,11 @@ def dumps(self) -> str:
307313
return base64.urlsafe_b64encode(compressed).decode("ascii").strip("=")
308314

309315
@classmethod
310-
def _from_config_structure(cls, data, rust_enhancements: RustEnhancements) -> Enhancements:
316+
def _from_config_structure(
317+
cls,
318+
data: list[Any],
319+
rust_enhancements: RustEnhancements,
320+
) -> Enhancements:
311321
version, bases, rules = data
312322
if version not in VERSIONS:
313323
raise ValueError("Unknown version")
@@ -319,7 +329,7 @@ def _from_config_structure(cls, data, rust_enhancements: RustEnhancements) -> En
319329
)
320330

321331
@classmethod
322-
def loads(cls, data) -> Enhancements:
332+
def loads(cls, data: str | bytes) -> Enhancements:
323333
if isinstance(data, str):
324334
data = data.encode("ascii", "ignore")
325335
padded = data + b"=" * (4 - (len(data) % 4))
@@ -339,7 +349,9 @@ def loads(cls, data) -> Enhancements:
339349

340350
@classmethod
341351
@sentry_sdk.tracing.trace
342-
def from_config_string(cls, s, bases=None, id=None) -> Enhancements:
352+
def from_config_string(
353+
cls, s: str, bases: list[str] | None = None, id: str | None = None
354+
) -> Enhancements:
343355
rust_enhancements = parse_rust_enhancements("config_string", s)
344356

345357
rules = parse_enhancements(s)

src/sentry/grouping/enhancer/actions.py

+24-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from collections.abc import Callable, Sequence
44
from typing import 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
@@ -30,14 +32,18 @@ class EnhancementAction:
3032
def apply_modifications_to_frame(
3133
self,
3234
frames: Sequence[dict[str, Any]],
33-
match_frames: Sequence[dict[str, Any]],
35+
match_frames: list[MatchFrame],
3436
idx: int,
35-
rule: Any = None,
37+
rule: Any | None = None,
3638
) -> None:
3739
pass
3840

3941
def update_frame_components_contributions(
40-
self, components, frames: Sequence[dict[str, Any]], idx, rule=None
42+
self,
43+
components: list[BaseGroupingComponent],
44+
frames: list[dict[str, Any]],
45+
idx: int,
46+
rule: Any | None = None,
4147
) -> None:
4248
pass
4349

@@ -55,7 +61,7 @@ def is_updater(self) -> bool:
5561
return self._is_updater
5662

5763
@classmethod
58-
def _from_config_structure(cls, val, version: int):
64+
def _from_config_structure(cls, val: list[str] | int, version: int) -> EnhancementAction:
5965
if isinstance(val, list): # This is a `VarAction`
6066
variable, value = val
6167
return VarAction(variable, value)
@@ -88,7 +94,7 @@ def __str__(self) -> str:
8894
self.key,
8995
)
9096

91-
def _to_config_structure(self, version: int):
97+
def _to_config_structure(self, version: int) -> int:
9298
"""
9399
Convert the action into an integer by
94100
- converting the combination of its boolean value (if it's a `+app/+group` rule or a
@@ -100,7 +106,7 @@ def _to_config_structure(self, version: int):
100106
"""
101107
return ACTIONS.index(self.key) | (ACTION_FLAGS[self.flag, self.range] << ACTION_BITSIZE)
102108

103-
def _slice_to_range(self, seq, idx):
109+
def _slice_to_range(self, seq: list[Any], idx: int) -> list[Any]:
104110
if self.range is None:
105111
return [seq[idx]]
106112
elif self.range == "down":
@@ -122,17 +128,21 @@ def _in_app_changed(self, frame: dict[str, Any]) -> bool:
122128
def apply_modifications_to_frame(
123129
self,
124130
frames: Sequence[dict[str, Any]],
125-
match_frames: Sequence[dict[str, Any]],
131+
match_frames: list[MatchFrame],
126132
idx: int,
127-
rule: Any = None,
133+
rule: Any | None = None,
128134
) -> None:
129135
# Change a frame or many to be in_app
130136
if self.key == "app":
131137
for match_frame in self._slice_to_range(match_frames, idx):
132138
match_frame["in_app"] = self.flag
133139

134140
def update_frame_components_contributions(
135-
self, components, frames: Sequence[dict[str, Any]], idx, rule=None
141+
self,
142+
components: list[BaseGroupingComponent],
143+
frames: list[dict[str, Any]],
144+
idx: int,
145+
rule: Any | None = None,
136146
) -> None:
137147
rule_hint = "stack trace rule"
138148
if rule:
@@ -184,19 +194,20 @@ def __init__(self, var: str, value: str) -> None:
184194
def __str__(self) -> str:
185195
return f"{self.var}={self.value}"
186196

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

190-
def modify_stacktrace_state(self, state, rule):
201+
def modify_stacktrace_state(self, state, rule) -> None:
191202
if self.var not in VarAction._FRAME_VARIABLES:
192203
state.set(self.var, self.value, rule)
193204

194205
def apply_modifications_to_frame(
195206
self,
196207
frames: Sequence[dict[str, Any]],
197-
match_frames: Sequence[dict[str, Any]],
208+
match_frames: list[MatchFrame],
198209
idx: int,
199-
rule: Any = None,
210+
rule: Any | None = None,
200211
) -> None:
201212
if self.var == "category":
202213
frame = frames[idx]

0 commit comments

Comments
 (0)