Browse Source

ref: improve utils typing (#70279)

<!-- Describe your PR here. -->
anthony sottile 10 months ago
parent
commit
74f7b3b921

+ 7 - 0
pyproject.toml

@@ -587,15 +587,22 @@ module = [
     "sentry.tasks.commit_context",
     "sentry.tasks.on_demand_metrics",
     "sentry.tasks.reprocessing2",
+    "sentry.utils.actor",
+    "sentry.utils.assets",
     "sentry.utils.audit",
     "sentry.utils.canonical",
+    "sentry.utils.codeowners",
+    "sentry.utils.colors",
     "sentry.utils.email.*",
+    "sentry.utils.env",
     "sentry.utils.iterators",
     "sentry.utils.locking.backends.redis",
     "sentry.utils.otp",
     "sentry.utils.redis",
     "sentry.utils.redis_metrics",
     "sentry.utils.sms",
+    "sentry.utils.uwsgi",
+    "sentry.utils.zip",
     "sentry_plugins.base",
     "tests.sentry.api.endpoints.issues.*",
     "tests.sentry.grouping.test_fingerprinting",

+ 2 - 1
src/sentry/models/commit.py

@@ -20,6 +20,7 @@ from sentry.db.models import (
 from sentry.utils.groupreference import find_referenced_groups
 
 if TYPE_CHECKING:
+    from sentry.models.group import Group
     from sentry.models.release import Release
 
 
@@ -71,5 +72,5 @@ class Commit(Model):
             return self.key[:7]
         return self.key
 
-    def find_referenced_groups(self):
+    def find_referenced_groups(self) -> set[Group]:
         return find_referenced_groups(self.message, self.organization_id)

+ 5 - 2
src/sentry/models/pullrequest.py

@@ -1,7 +1,7 @@
 from __future__ import annotations
 
 from collections.abc import Mapping, Sequence
-from typing import Any, ClassVar
+from typing import TYPE_CHECKING, Any, ClassVar
 
 from django.contrib.postgres.fields import ArrayField as DjangoArrayField
 from django.db import models
@@ -21,6 +21,9 @@ from sentry.db.models import (
 )
 from sentry.utils.groupreference import find_referenced_groups
 
+if TYPE_CHECKING:
+    from sentry.models.group import Group
+
 
 class PullRequestManager(BaseManager["PullRequest"]):
     def update_or_create(
@@ -79,7 +82,7 @@ class PullRequest(Model):
 
     __repr__ = sane_repr("organization_id", "repository_id", "key")
 
-    def find_referenced_groups(self):
+    def find_referenced_groups(self) -> set[Group]:
         text = f"{self.message} {self.title}"
         return find_referenced_groups(text, self.organization_id)
 

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

@@ -15,7 +15,7 @@ if TYPE_CHECKING:
 
 class ActorTuple(namedtuple("Actor", "id type")):
     @property
-    def identifier(self):
+    def identifier(self) -> str:
         return f"{self.type.__name__.lower()}:{self.id}"
 
     @overload

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

@@ -4,7 +4,7 @@ from sentry_relay.processing import is_codeowners_path_match
 MAX_RAW_LENGTH = 3_000_000
 
 
-def codeowners_match(value, pat):
+def codeowners_match(value: str | None, pat: str) -> bool:
     """A beefed up version of fnmatch.fnmatch"""
     return is_codeowners_path_match(
         value if value is not None else "",

+ 3 - 3
src/sentry/utils/colors.py

@@ -2,7 +2,7 @@ import colorsys
 import hashlib
 
 
-def get_hashed_color(string, l=0.5, s=0.5):  # noqa: E741
-    val = int(hashlib.md5(string.encode("utf-8")).hexdigest()[:3], 16)
-    tup = colorsys.hls_to_rgb(val / 4096.0, l, s)
+def get_hashed_color(string: str) -> str:
+    val = int(hashlib.md5(string.encode()).hexdigest()[:3], 16)
+    tup = colorsys.hls_to_rgb(val / 4096.0, 0.5, 0.5)
     return f"#{int(tup[0] * 255):02x}{int(tup[1] * 255):02x}{int(tup[2] * 255):02x}"

+ 2 - 1
src/sentry/utils/env.py

@@ -1,3 +1,4 @@
+import logging
 import os
 import sys
 
@@ -42,7 +43,7 @@ def gcp_project_id() -> str:
 
 
 # TODO(getsentry/team-ospo#190): Remove once fully deployed.
-def log_gcp_credentials_details(logger) -> None:
+def log_gcp_credentials_details(logger: logging.Logger) -> None:
     if in_test_environment():
         return
 

+ 7 - 2
src/sentry/utils/glob.py

@@ -2,8 +2,13 @@ from sentry_relay.processing import is_glob_match
 
 
 def glob_match(
-    value, pat, doublestar=False, ignorecase=False, path_normalize=False, allow_newline=True
-):
+    value: str | None,
+    pat: str,
+    doublestar: bool = False,
+    ignorecase: bool = False,
+    path_normalize: bool = False,
+    allow_newline: bool = True,
+) -> bool:
     """A beefed up version of fnmatch.fnmatch"""
     return is_glob_match(
         value if value is not None else "",

+ 8 - 2
src/sentry/utils/groupreference.py

@@ -1,4 +1,10 @@
+from __future__ import annotations
+
 import re
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+    from sentry.models.group import Group
 
 _fixes_re = re.compile(
     r"\b(?:Fix|Fixes|Fixed|Close|Closes|Closed|Resolve|Resolves|Resolved):?\s+([A-Za-z0-9_\-\s\,]+)\b",
@@ -7,11 +13,11 @@ _fixes_re = re.compile(
 _short_id_re = re.compile(r"\b([A-Z0-9_-]+-[A-Z0-9]+)\b", re.I)
 
 
-def find_referenced_groups(text, org_id):
+def find_referenced_groups(text: str | None, org_id: int) -> set[Group]:
     from sentry.models.group import Group
 
     if not text:
-        return []
+        return set()
 
     results = set()
     for fmatch in _fixes_re.finditer(text):

+ 0 - 5
src/sentry/utils/support.py

@@ -1,5 +0,0 @@
-def get_support_mail():
-    """Returns the most appropriate support email address"""
-    from sentry.options import get
-
-    return get("system.support-email") or get("system.admin-email") or None

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