Skip to content

Commit 1deb0c9

Browse files
ref: stronger typing for a few more utils modules (#70659)
<!-- Describe your PR here. -->
1 parent 71fba9c commit 1deb0c9

File tree

7 files changed

+27
-15
lines changed

7 files changed

+27
-15
lines changed

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,17 +617,22 @@ module = [
617617
"sentry.utils.env",
618618
"sentry.utils.event",
619619
"sentry.utils.files",
620+
"sentry.utils.geo",
621+
"sentry.utils.imports",
620622
"sentry.utils.iterators",
621623
"sentry.utils.javascript",
622624
"sentry.utils.locking.backends.redis",
623625
"sentry.utils.migrations",
626+
"sentry.utils.numbers",
624627
"sentry.utils.otp",
625628
"sentry.utils.performance_issues.detectors.*",
626629
"sentry.utils.performance_issues.performance_detection",
630+
"sentry.utils.pubsub",
627631
"sentry.utils.redis",
628632
"sentry.utils.redis_metrics",
629633
"sentry.utils.sentry_apps.*",
630634
"sentry.utils.sms",
635+
"sentry.utils.urls",
631636
"sentry.utils.uwsgi",
632637
"sentry.utils.zip",
633638
"sentry_plugins.base",

src/sentry/utils/geo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111

1212
# default is no-op
13-
def geo_by_addr(ip):
14-
pass
13+
def geo_by_addr(ip: str) -> dict[str, Any] | None:
14+
return None
1515

1616

1717
rust_geoip: None | GeoIpLookup = None
@@ -48,7 +48,7 @@ def _geo_by_addr(ip: str) -> dict[str, Any] | None:
4848
geo_by_addr = _geo_by_addr
4949

5050

51-
def _init_geoip_rust():
51+
def _init_geoip_rust() -> None:
5252
global rust_geoip
5353

5454
from sentry_relay.processing import GeoIpLookup

src/sentry/utils/imports.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
class ModuleProxyCache(dict):
2-
def __missing__(self, key):
1+
from typing import Any
2+
3+
4+
class ModuleProxyCache(dict[str, object]):
5+
def __missing__(self, key: str) -> object:
36
if "." not in key:
47
return __import__(key)
58

@@ -17,7 +20,7 @@ def __missing__(self, key):
1720
_cache = ModuleProxyCache()
1821

1922

20-
def import_string(path: str):
23+
def import_string(path: str) -> Any:
2124
"""
2225
Path must be module.path.ClassName
2326

src/sentry/utils/numbers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def base36_decode(s: str) -> int:
6262
DEFAULT_UNITS = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
6363

6464

65-
def format_bytes(number, units=DEFAULT_UNITS, decimal_places=2):
65+
def format_bytes(
66+
number: float, units: tuple[str, ...] = DEFAULT_UNITS, decimal_places: int = 2
67+
) -> str:
6668
block = 1024.0
6769
if number < block:
6870
return f"{number} {units[0]}"
@@ -72,7 +74,7 @@ def format_bytes(number, units=DEFAULT_UNITS, decimal_places=2):
7274
while number >= block and u < max_unit:
7375
number /= block
7476
u += 1
75-
return ("{:.%df} {}" % (decimal_places,)).format(number, units[u])
77+
return f"{number:.{decimal_places}f} {units[u]}"
7678

7779

7880
def format_grouped_length(length: int, steps: list[int] | None = None) -> str:
@@ -92,7 +94,7 @@ def format_grouped_length(length: int, steps: list[int] | None = None) -> str:
9294
return f">{steps[-1]}"
9395

9496

95-
def validate_bigint(value):
97+
def validate_bigint(value: object) -> bool:
9698
return isinstance(value, int) and value >= 0 and value.bit_length() <= 63
9799

98100

src/sentry/utils/pubsub.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
from typing import Any
2+
13
from confluent_kafka import Producer
24

35

46
class KafkaPublisher:
5-
def __init__(self, connection, asynchronous=True):
7+
def __init__(self, connection: dict[str, Any], asynchronous: bool = True) -> None:
68
self.producer = Producer(connection or {})
79
self.asynchronous = asynchronous
810

9-
def publish(self, channel, value, key=None):
11+
def publish(self, channel: str, value: str, key: str | None = None) -> None:
1012
self.producer.produce(topic=channel, value=value, key=key)
1113
if self.asynchronous:
1214
self.producer.poll(0)

src/sentry/utils/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import re
2-
from collections.abc import MutableMapping, Sequence
2+
from collections.abc import Mapping, MutableMapping, Sequence
33
from urllib.parse import parse_qs, parse_qsl, urlencode, urljoin, urlparse, urlsplit, urlunparse
44

55
_scheme_re = re.compile(r"^([a-zA-Z0-9-+]+://)(.*)$")
66

77

8-
def non_standard_url_join(base, to_join):
8+
def non_standard_url_join(base: str, to_join: str | None) -> str:
99
"""A version of url join that can deal with unknown protocols."""
1010
# joins to an absolute url are willing by default
1111
if not to_join:
@@ -31,7 +31,7 @@ def non_standard_url_join(base, to_join):
3131
return rv
3232

3333

34-
def add_params_to_url(url, params):
34+
def add_params_to_url(url: str, params: Mapping[str, str]) -> str:
3535
url_parts = urlparse(url)
3636
query = dict(parse_qsl(url_parts.query))
3737
query.update(params)

tests/sentry/utils/test_numbers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def test_format_bytes():
287287
assert format_bytes(3000000000) == "2.79 GB"
288288
assert format_bytes(3000000000000) == "2.73 TB"
289289

290-
assert format_bytes(3000000000000, units=["B", "KB", "MB", "GB"]) == "2793.97 GB"
290+
assert format_bytes(3000000000000, units=("B", "KB", "MB", "GB")) == "2793.97 GB"
291291

292292

293293
def test_format_grouped_length():

0 commit comments

Comments
 (0)