Skip to content

Commit 65ca3b4

Browse files
authored
fix(options): Fix and improve type coverage (#104474)
This fixes a case where we had 'type' rather than 'Type', refines a few untyped things while we're there. This was mostly motivated by wanting to ensure 'Find All References' actually finds the relevant references after I noticed it wasn't when trying to adjust the type checking semantics.
1 parent f5f1448 commit 65ca3b4

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

src/sentry/options/manager.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
from collections.abc import Sequence
66
from enum import Enum
77
from typing import TYPE_CHECKING
8+
from typing import Any as TAny
89

910
from django.conf import settings
1011

1112
from sentry.utils.flag import record_option
1213
from sentry.utils.hashlib import md5_text
13-
from sentry.utils.types import Any, type_from_value
14+
from sentry.utils.types import Any, Type, type_from_value
1415

1516
if TYPE_CHECKING:
16-
from sentry.options.store import Key, OptionsStore
17+
from sentry.options.store import GroupingInfo, Key, OptionsStore
1718

1819
# Prevent ourselves from clobbering the builtin
1920
_type = type
@@ -155,7 +156,7 @@ class UnknownOption(KeyError):
155156
}
156157

157158

158-
def _make_cache_key(key):
159+
def _make_cache_key(key: str) -> str:
159160
return "o:%s" % md5_text(key).hexdigest()
160161

161162

@@ -236,13 +237,13 @@ def lookup_key(self, key: str):
236237
def make_key(
237238
self,
238239
name: str,
239-
default,
240-
type,
240+
default: TAny,
241+
type: Type[TAny],
241242
flags: int,
242243
ttl: int,
243244
grace: int,
244-
grouping_info,
245-
):
245+
grouping_info: GroupingInfo | None,
246+
) -> Key:
246247
from sentry.options.store import Key
247248

248249
return Key(

src/sentry/options/store.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from sentry.db.postgres.transactions import in_test_hide_transaction_boundary
1515
from sentry.options.manager import UpdateChannel
16+
from sentry.utils.types import Type
1617

1718
CACHE_FETCH_ERR = "Unable to fetch option cache for %s"
1819
CACHE_UPDATE_ERR = "Unable to update option cache for %s"
@@ -37,7 +38,7 @@ class GroupingInfo:
3738
class Key:
3839
name: str
3940
default: Any
40-
type: type
41+
type: Type[Any]
4142
flags: int
4243
ttl: int
4344
grace: int

src/sentry/testutils/helpers/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def new_get(key, **kwargs):
2828
except KeyError:
2929
return wrapped(key, **kwargs)
3030

31-
def new_lookup(self: OptionsManager, key):
31+
def new_lookup(self: OptionsManager, key: str):
3232
# use the default key definition if available
3333
if key not in options or key in self.registry:
3434
return original_lookup(self, key)

src/sentry/utils/types.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import typing
4+
from typing import Any as TAny
45
from typing import TypeGuard
56

67
from yaml.parser import ParserError
@@ -41,7 +42,7 @@ def __call__(self, value: object | None = None) -> T:
4142
return rv
4243
raise InvalidTypeError(f"{value!r} is not a valid {self!r}")
4344

44-
def convert(self, value):
45+
def convert(self, value: TAny) -> T | None:
4546
return value
4647

4748
def _default(self) -> T:
@@ -72,7 +73,7 @@ class BoolType(Type[bool]):
7273
expected_types = (bool,)
7374
compatible_types = (str, int)
7475

75-
def convert(self, value):
76+
def convert(self, value: TAny) -> bool | None:
7677
if isinstance(value, int):
7778
return bool(value)
7879
value = value.lower()
@@ -91,7 +92,7 @@ class IntType(Type[int]):
9192
default = 0
9293
expected_types = (int,)
9394

94-
def convert(self, value):
95+
def convert(self, value: TAny) -> int | None:
9596
try:
9697
return int(value)
9798
except ValueError:
@@ -106,7 +107,7 @@ class FloatType(Type[float]):
106107
expected_types = (float,)
107108
compatible_types = (str, int, float)
108109

109-
def convert(self, value):
110+
def convert(self, value: TAny) -> float | None:
110111
try:
111112
return float(value)
112113
except ValueError:
@@ -128,11 +129,11 @@ class DictType(Type[dict]):
128129
name = "dictionary"
129130
expected_types = (dict,)
130131

131-
def _default(self) -> dict[str, typing.Any]:
132+
def _default(self) -> dict[str, TAny]:
132133
# make sure we create a fresh dict each time
133134
return {}
134135

135-
def convert(self, value):
136+
def convert(self, value: TAny) -> dict[str, TAny] | None:
136137
try:
137138
return safe_load(value)
138139
except (AttributeError, ParserError, ScannerError):
@@ -146,11 +147,11 @@ class SequenceType(Type[list]):
146147
expected_types = (list,)
147148
compatible_types = (str, tuple, list)
148149

149-
def _default(self) -> list[typing.Any]:
150+
def _default(self) -> list[TAny]:
150151
# make sure we create a fresh list each time
151152
return []
152153

153-
def convert(self, value):
154+
def convert(self, value: TAny) -> list[TAny] | None:
154155
if isinstance(value, str):
155156
try:
156157
value = safe_load(value)

0 commit comments

Comments
 (0)