Skip to content

Commit f63348d

Browse files
ref: add types to the "easy" functions in event_search
1 parent 58de2b2 commit f63348d

File tree

1 file changed

+47
-45
lines changed

1 file changed

+47
-45
lines changed

src/sentry/api/event_search.py

+47-45
Original file line numberDiff line numberDiff line change
@@ -411,14 +411,14 @@ class SearchValue(NamedTuple):
411411
raw_value: str | float | datetime | Sequence[float] | Sequence[str]
412412

413413
@property
414-
def value(self):
414+
def value(self) -> Any:
415415
if _is_wildcard(self.raw_value):
416416
return translate_wildcard(self.raw_value)
417417
elif isinstance(self.raw_value, str):
418418
return translate_escape_sequences(self.raw_value)
419419
return self.raw_value
420420

421-
def to_query_string(self):
421+
def to_query_string(self) -> str:
422422
# for any sequence (but not string) we want to iterate over the items
423423
# we do that because a simple str() would not be usable for strings
424424
# str(["a","b"]) == "['a', 'b']" but we would like "[a,b]"
@@ -511,7 +511,7 @@ class SearchFilter(NamedTuple):
511511
operator: str
512512
value: SearchValue
513513

514-
def __str__(self):
514+
def __str__(self) -> str:
515515
return f"{self.key.name}{self.operator}{self.value.raw_value}"
516516

517517
def to_query_string(self) -> str:
@@ -690,14 +690,14 @@ def _get_fallback_builder() -> UnresolvedQuery:
690690
self.get_function_result_type = _get_fallback_builder().get_function_result_type
691691

692692
@cached_property
693-
def key_mappings_lookup(self):
694-
lookup = {}
695-
for target_field, source_fields in self.config.key_mappings.items():
696-
for source_field in source_fields:
697-
lookup[source_field] = target_field
698-
return lookup
699-
700-
def is_numeric_key(self, key):
693+
def key_mappings_lookup(self) -> dict[str, str]:
694+
return {
695+
source_field: target_field
696+
for target_field, source_fields in self.config.key_mappings.items()
697+
for source_field in source_fields
698+
}
699+
700+
def is_numeric_key(self, key: str) -> bool:
701701
return (
702702
key in self.config.numeric_keys
703703
or is_measurement(key)
@@ -707,7 +707,7 @@ def is_numeric_key(self, key):
707707
or self.is_size_key(key)
708708
)
709709

710-
def is_duration_key(self, key):
710+
def is_duration_key(self, key: str) -> bool:
711711
duration_types = [*DURATION_UNITS, "duration"]
712712
return (
713713
key in self.config.duration_keys
@@ -716,13 +716,13 @@ def is_duration_key(self, key):
716716
or self.get_field_type(key) in duration_types
717717
)
718718

719-
def is_size_key(self, key):
719+
def is_size_key(self, key: str) -> bool:
720720
return self.get_field_type(key) in SIZE_UNITS
721721

722-
def is_date_key(self, key):
722+
def is_date_key(self, key: str) -> bool:
723723
return key in self.config.date_keys
724724

725-
def is_boolean_key(self, key):
725+
def is_boolean_key(self, key: str) -> bool:
726726
return key in self.config.boolean_keys
727727

728728
def visit_search(self, node, children):
@@ -731,18 +731,18 @@ def visit_search(self, node, children):
731731
def visit_term(self, node, children):
732732
return remove_optional_nodes(flatten(children[0]))
733733

734-
def visit_boolean_operator(self, node, children):
734+
def visit_boolean_operator(self, node: Node, children: tuple[QueryOp]) -> QueryOp:
735735
if not self.config.allow_boolean:
736736
raise InvalidSearchQuery(
737737
'Boolean statements containing "OR" or "AND" are not supported in this search'
738738
)
739739

740740
return children[0]
741741

742-
def visit_free_text_unquoted(self, node, children):
742+
def visit_free_text_unquoted(self, node: Node, children: object) -> str | None:
743743
return node.text.strip(" ") or None
744744

745-
def visit_free_text(self, node, children):
745+
def visit_free_text(self, node: Node, children: tuple[str]) -> SearchFilter | None:
746746
if not children[0]:
747747
return None
748748
# Free text searches need to be treated like they were wildcards
@@ -1206,10 +1206,10 @@ def visit_search_key(self, node, children):
12061206
return key
12071207
return SearchKey(self.key_mappings_lookup.get(key, key))
12081208

1209-
def visit_text_key(self, node, children):
1209+
def visit_text_key(self, node: Node, children: tuple[SearchKey]) -> SearchKey:
12101210
return children[0]
12111211

1212-
def visit_value(self, node, children):
1212+
def visit_value(self, node: Node, children: object) -> str:
12131213
# A properly quoted value will match the quoted value regex, so any unescaped
12141214
# quotes are errors.
12151215
value = node.text
@@ -1236,13 +1236,13 @@ def visit_quoted_value(self, node, children):
12361236

12371237
return value
12381238

1239-
def visit_in_value(self, node, children):
1239+
def visit_in_value(self, node: Node, children: object) -> str:
12401240
return node.text.replace('\\"', '"')
12411241

1242-
def visit_text_in_value(self, node, children):
1242+
def visit_text_in_value(self, node: Node, children: tuple[str]) -> str:
12431243
return children[0]
12441244

1245-
def visit_search_value(self, node, children):
1245+
def visit_search_value(self, node: Node, children: tuple[str]) -> SearchValue:
12461246
return SearchValue(children[0])
12471247

12481248
def visit_numeric_value(self, node, children):
@@ -1252,7 +1252,7 @@ def visit_numeric_value(self, node, children):
12521252

12531253
return [f"{sign}{value}", suffix]
12541254

1255-
def visit_boolean_value(self, node, children):
1255+
def visit_boolean_value(self, node: Node, children: object) -> Node:
12561256
return node
12571257

12581258
def visit_text_in_list(self, node, children):
@@ -1261,58 +1261,60 @@ def visit_text_in_list(self, node, children):
12611261
def visit_numeric_in_list(self, node, children):
12621262
return process_list(children[1], children[2])
12631263

1264-
def visit_iso_8601_date_format(self, node, children):
1264+
def visit_iso_8601_date_format(self, node: Node, children: object) -> str:
12651265
return node.text
12661266

1267-
def visit_rel_date_format(self, node, children):
1267+
def visit_rel_date_format(self, node: Node, children: object) -> Node:
12681268
return node
12691269

1270-
def visit_duration_format(self, node, children):
1270+
def visit_duration_format(
1271+
self, node: Node, children: tuple[str, tuple[Node], Node]
1272+
) -> list[str]:
12711273
return [children[0], children[1][0].text]
12721274

1273-
def visit_size_format(self, node, children):
1275+
def visit_size_format(self, node: Node, children: tuple[str, tuple[Node]]) -> list[str]:
12741276
return [children[0], children[1][0].text]
12751277

1276-
def visit_percentage_format(self, node, children):
1278+
def visit_percentage_format(self, node: Node, children: tuple[str, Node]) -> str:
12771279
return children[0]
12781280

1279-
def visit_operator(self, node, children):
1281+
def visit_operator(self, node: Node, children: object) -> str:
12801282
return node.text
12811283

1282-
def visit_or_operator(self, node, children):
1284+
def visit_or_operator(self, node: Node, children: object) -> str:
12831285
return node.text.upper()
12841286

1285-
def visit_and_operator(self, node, children):
1287+
def visit_and_operator(self, node: Node, children: object) -> str:
12861288
return node.text.upper()
12871289

1288-
def visit_numeric(self, node, children):
1290+
def visit_numeric(self, node: Node, children: object) -> str:
12891291
return node.text
12901292

1291-
def visit_open_paren(self, node, children):
1293+
def visit_open_paren(self, node: Node, children: object) -> str:
12921294
return node.text
12931295

1294-
def visit_closed_paren(self, node, children):
1296+
def visit_closed_paren(self, node: Node, children: object) -> str:
12951297
return node.text
12961298

1297-
def visit_open_bracket(self, node, children):
1299+
def visit_open_bracket(self, node: Node, children: object) -> str:
12981300
return node.text
12991301

1300-
def visit_closed_bracket(self, node, children):
1302+
def visit_closed_bracket(self, node: Node, children: object) -> str:
13011303
return node.text
13021304

1303-
def visit_sep(self, node, children):
1305+
def visit_sep(self, node: Node, children: object) -> Node:
13041306
return node
13051307

1306-
def visit_negation(self, node, children):
1308+
def visit_negation(self, node: Node, children: object) -> Node:
13071309
return node
13081310

1309-
def visit_comma(self, node, children):
1311+
def visit_comma(self, node: Node, children: object) -> Node:
13101312
return node
13111313

1312-
def visit_spaces(self, node, children):
1314+
def visit_spaces(self, node: Node, children: object) -> str:
13131315
return " "
13141316

1315-
def generic_visit(self, node, children):
1317+
def generic_visit(self, node: Node, children: Sequence[Any]) -> Any:
13161318
return children or node
13171319

13181320

@@ -1358,7 +1360,7 @@ def parse_search_query(
13581360
query: str,
13591361
*,
13601362
config: SearchConfig[Literal[False]],
1361-
params=None,
1363+
params: ParamsType | None = None,
13621364
get_field_type: Callable[[str], str | None] | None = None,
13631365
get_function_result_type: Callable[[str], str | None] | None = None,
13641366
) -> Sequence[SearchFilter | AggregateFilter]: ...
@@ -1369,7 +1371,7 @@ def parse_search_query(
13691371
query: str,
13701372
*,
13711373
config: SearchConfig[Literal[True]] | None = None,
1372-
params=None,
1374+
params: ParamsType | None = None,
13731375
get_field_type: Callable[[str], str | None] | None = None,
13741376
get_function_result_type: Callable[[str], str | None] | None = None,
13751377
) -> Sequence[QueryToken]: ...
@@ -1379,7 +1381,7 @@ def parse_search_query(
13791381
query: str,
13801382
*,
13811383
config: SearchConfig[Any] | None = None,
1382-
params=None,
1384+
params: ParamsType | None = None,
13831385
get_field_type: Callable[[str], str | None] | None = None,
13841386
get_function_result_type: Callable[[str], str | None] | None = None,
13851387
) -> Sequence[QueryToken]:

0 commit comments

Comments
 (0)