Browse Source

ref: fix some typing errors in utils and utils tests (#51529)

<!-- Describe your PR here. -->
anthony sottile 1 year ago
parent
commit
4976c31987

+ 0 - 26
pyproject.toml

@@ -1035,12 +1035,8 @@ module = [
     "sentry.types.region",
     "sentry.utils.audit",
     "sentry.utils.auth",
-    "sentry.utils.avatar",
-    "sentry.utils.canonical",
     "sentry.utils.committers",
     "sentry.utils.concurrent",
-    "sentry.utils.dates",
-    "sentry.utils.db",
     "sentry.utils.debug",
     "sentry.utils.distutils.commands.base",
     "sentry.utils.distutils.commands.build_assets",
@@ -1048,12 +1044,9 @@ module = [
     "sentry.utils.email.signer",
     "sentry.utils.geo",
     "sentry.utils.http",
-    "sentry.utils.kafka_config",
     "sentry.utils.locking.backends.migration",
     "sentry.utils.metrics",
     "sentry.utils.outcomes",
-    "sentry.utils.performance_issues.detectors.consecutive_db_detector",
-    "sentry.utils.performance_issues.detectors.consecutive_http_detector",
     "sentry.utils.performance_issues.detectors.io_main_thread_detector",
     "sentry.utils.performance_issues.detectors.mn_plus_one_db_span_detector",
     "sentry.utils.performance_issues.detectors.n_plus_one_api_calls_detector",
@@ -1061,8 +1054,6 @@ module = [
     "sentry.utils.performance_issues.detectors.render_blocking_asset_span_detector",
     "sentry.utils.performance_issues.detectors.slow_db_query_detector",
     "sentry.utils.performance_issues.performance_detection",
-    "sentry.utils.performance_issues.performance_problem",
-    "sentry.utils.pubsub",
     "sentry.utils.query",
     "sentry.utils.redis",
     "sentry.utils.request_cache",
@@ -1070,17 +1061,12 @@ module = [
     "sentry.utils.sdk",
     "sentry.utils.sentry_apps.webhooks",
     "sentry.utils.services",
-    "sentry.utils.settings",
-    "sentry.utils.silo.add_silo_decorators",
     "sentry.utils.silo.decorate_models_by_relation",
-    "sentry.utils.silo.decorate_unit_tests",
     "sentry.utils.snowflake",
     "sentry.utils.snuba",
-    "sentry.utils.strings",
     "sentry.utils.suspect_resolutions.get_suspect_resolutions",
     "sentry.utils.suspect_resolutions.metric_correlation",
     "sentry.utils.suspect_resolutions_releases.get_suspect_resolutions_releases",
-    "sentry.utils.types",
     "sentry.web.api",
     "sentry.web.client_config",
     "sentry.web.forms.accounts",
@@ -1620,32 +1606,20 @@ module = [
     "tests.sentry.tsdb.test_redissnuba",
     "tests.sentry.utils.email.test_list_resolver",
     "tests.sentry.utils.email.test_message_builder",
-    "tests.sentry.utils.kvstore.test_bigtable",
     "tests.sentry.utils.kvstore.test_common",
     "tests.sentry.utils.locking.backends.test_redis",
-    "tests.sentry.utils.suspect_resolutions.test_get_suspect_resolutions",
     "tests.sentry.utils.suspect_resolutions.test_metric_correlation",
     "tests.sentry.utils.test_audit",
-    "tests.sentry.utils.test_auth",
-    "tests.sentry.utils.test_committers",
     "tests.sentry.utils.test_cursors",
     "tests.sentry.utils.test_event_frames",
     "tests.sentry.utils.test_functional",
-    "tests.sentry.utils.test_kafka_config",
-    "tests.sentry.utils.test_letter_avatar",
     "tests.sentry.utils.test_meta",
     "tests.sentry.utils.test_migrations",
     "tests.sentry.utils.test_outcomes",
-    "tests.sentry.utils.test_redis",
     "tests.sentry.utils.test_request_cache",
-    "tests.sentry.utils.test_rust",
     "tests.sentry.utils.test_safe",
-    "tests.sentry.utils.test_sdk",
     "tests.sentry.utils.test_services",
-    "tests.sentry.utils.test_session_store",
-    "tests.sentry.utils.test_settings",
     "tests.sentry.utils.test_time_window",
-    "tests.sentry.utils.test_urllib3_timeout",
     "tests.sentry.web.frontend.generic.test_static_media",
     "tests.sentry.web.frontend.test_auth_login",
     "tests.sentry.web.frontend.test_auth_oauth2",

+ 1 - 1
src/sentry/conf/server.py

@@ -2972,7 +2972,7 @@ SENTRY_USER_PERMISSIONS = ("broadcasts.admin", "users.admin", "options.admin")
 # Reading items from this default configuration directly might break deploys.
 # To correctly read items from this dictionary and not worry about the format,
 # see `sentry.utils.kafka_config.get_kafka_consumer_cluster_options`.
-KAFKA_CLUSTERS = {
+KAFKA_CLUSTERS: dict[str, dict[str, Any]] = {
     "default": {
         "common": {"bootstrap.servers": "127.0.0.1:9092"},
         "producers": {

+ 1 - 1
src/sentry/notifications/utils/__init__.py

@@ -553,7 +553,7 @@ class PerformanceProblemContext:
 
     def _sum_span_duration(self, spans: list[Dict[str, Any] | None]) -> float:
         "Given non-overlapping spans, find the sum of the span durations in milliseconds"
-        sum: float = 0.0
+        sum = 0.0
         for span in spans:
             if span:
                 sum += self.get_span_duration(span).total_seconds() * 1000

+ 7 - 5
src/sentry/utils/avatar.py

@@ -2,7 +2,9 @@
 Note: Also see letterAvatar.jsx. Anything changed in this file (how colors are
       selected, the svg, etc) will also need to be changed there.
 """
-from typing import MutableMapping, Optional, TextIO, Union
+from __future__ import annotations
+
+from typing import IO, MutableMapping, Optional, Union
 from urllib.parse import urlencode
 
 from django.conf import settings
@@ -55,19 +57,19 @@ LETTER_AVATAR_COLORS = [
 COLOR_COUNT = len(LETTER_AVATAR_COLORS)
 
 
-def hash_user_identifier(identifier: str) -> int:
+def hash_user_identifier(identifier: str | int) -> int:
     identifier = force_text(identifier, errors="replace")
     return sum(map(ord, identifier))
 
 
-def get_letter_avatar_color(identifier: str) -> str:
+def get_letter_avatar_color(identifier: str | int) -> str:
     hashed_id = hash_user_identifier(identifier)
     return LETTER_AVATAR_COLORS[hashed_id % COLOR_COUNT]
 
 
 def get_letter_avatar(
     display_name: Optional[str],
-    identifier: str,
+    identifier: str | int,
     size: Optional[int] = None,
     use_svg: Optional[bool] = True,
     initials: Optional[str] = None,
@@ -139,7 +141,7 @@ def get_platform_avatar(
     return f'<img class="avatar" src="https://raw.githubusercontent.com/getsentry/platformicons/master/svg/{display_name}.svg" height={size}>'
 
 
-def is_black_alpha_only(data: TextIO) -> bool:
+def is_black_alpha_only(data: IO[bytes]) -> bool:
     """Check if an image has only black pixels (with alpha)"""
     result = False
     with Image.open(data) as image:

+ 10 - 5
src/sentry/utils/canonical.py

@@ -1,8 +1,13 @@
+from __future__ import annotations
+
 import copy
-from collections.abc import Mapping, MutableMapping
+from typing import Mapping, MutableMapping, TypeVar
 
 from django.conf import settings
 
+K = TypeVar("K")
+V = TypeVar("V")
+
 __all__ = ("CanonicalKeyDict", "CanonicalKeyView", "get_canonical_name")
 
 
@@ -80,18 +85,18 @@ class CanonicalKeyView(Mapping):
         return self.data.__repr__()
 
 
-class CanonicalKeyDict(MutableMapping):
-    def __init__(self, data, legacy=None):
+class CanonicalKeyDict(MutableMapping[K, V]):
+    def __init__(self, data: Mapping[K, V], legacy: bool | None = None) -> None:
         self.legacy = legacy
         self.__init(data)
 
-    def __init(self, data):
+    def __init(self, data: Mapping[K, V]) -> None:
         legacy = self.legacy
         if legacy is None:
             legacy = settings.PREFER_CANONICAL_LEGACY_KEYS
         norm_func = get_legacy_name if legacy else get_canonical_name
         self._norm_func = norm_func
-        self.data = {}
+        self.data: dict[K, V] = {}
         for key, value in data.items():
             canonical_key = norm_func(key)
             if key == canonical_key or canonical_key not in self.data:

+ 1 - 1
src/sentry/utils/dates.py

@@ -178,7 +178,7 @@ def outside_retention_with_modified_start(
     organizations retention period. Returns an updated
     start datetime if start is out of retention.
     """
-    retention = quotas.get_event_retention(organization=organization)
+    retention = quotas.backend.get_event_retention(organization=organization)
     if not retention:
         return False, start
 

+ 2 - 2
src/sentry/utils/db.py

@@ -51,8 +51,8 @@ class DjangoAtomicIntegration(Integration):
                 del self._sentry_sdk_span
             return rv
 
-        Atomic.__enter__ = _enter
-        Atomic.__exit__ = _exit
+        Atomic.__enter__ = _enter  # type: ignore[method-assign]
+        Atomic.__exit__ = _exit  # type: ignore[method-assign]
 
 
 def table_exists(name, using=DEFAULT_DB_ALIAS):

+ 3 - 3
src/sentry/utils/performance_issues/detectors/consecutive_db_detector.py

@@ -181,9 +181,9 @@ class ConsecutiveDBSpanDetector(PerformanceDetector):
 
         return self.consecutive_db_spans[0].get("description", "")
 
-    def _sum_span_duration(self, spans: list[Span]) -> int:
+    def _sum_span_duration(self, spans: list[Span]) -> float:
         "Given a list of spans, find the sum of the span durations in milliseconds"
-        sum = 0
+        sum = 0.0
         for span in spans:
             sum += get_span_duration(span).total_seconds() * 1000
         return sum
@@ -219,7 +219,7 @@ class ConsecutiveDBSpanDetector(PerformanceDetector):
             [get_span_duration(span).total_seconds() * 1000 for span in independent_spans]
         )
 
-        sum_of_dependent_span_durations = 0
+        sum_of_dependent_span_durations = 0.0
         for span in consecutive_spans:
             if span not in independent_spans:
                 sum_of_dependent_span_durations += get_span_duration(span).total_seconds() * 1000

+ 2 - 2
src/sentry/utils/performance_issues/detectors/consecutive_http_detector.py

@@ -121,9 +121,9 @@ class ConsecutiveHTTPSpanDetector(PerformanceDetector):
 
         self._reset_variables()
 
-    def _sum_span_duration(self, spans: list[Span]) -> int:
+    def _sum_span_duration(self, spans: list[Span]) -> float:
         "Given a list of spans, find the sum of the span durations in milliseconds"
-        sum = 0
+        sum = 0.0
         for span in spans:
             sum += get_span_duration(span).total_seconds() * 1000
         return sum

+ 1 - 1
src/sentry/utils/performance_issues/detectors/n_plus_one_api_calls_detector.py

@@ -45,7 +45,7 @@ class NPlusOneAPICallsDetector(PerformanceDetector):
     type = DetectorType.N_PLUS_ONE_API_CALLS
     settings_key = DetectorType.N_PLUS_ONE_API_CALLS
 
-    HOST_DENYLIST = []
+    HOST_DENYLIST: list[str] = []
 
     def init(self):
         # TODO: Only store the span IDs and timestamps instead of entire span objects

Some files were not shown because too many files changed in this diff