Просмотр исходного кода

ref(utils): Add typing to sentry.utils.hashlib (#38304)

Markus Unterwaditzer 2 лет назад
Родитель
Сommit
f6fdfd45e0
2 измененных файлов с 14 добавлено и 4 удалено
  1. 1 0
      mypy.ini
  2. 13 4
      src/sentry/utils/hashlib.py

+ 1 - 0
mypy.ini

@@ -92,6 +92,7 @@ files = src/sentry/analytics/,
         src/sentry/tasks/update_user_reports.py,
         src/sentry/unmerge.py,
         src/sentry/utils/appleconnect/,
+        src/sentry/utils/hashlib.py,
         src/sentry/utils/suspect_resolutions/commit_correlation.py,
         src/sentry/utils/suspect_resolutions/metric_correlation.py,
         src/sentry/utils/suspect_resolutions/resolved_in_active_release.py,

+ 13 - 4
src/sentry/utils/hashlib.py

@@ -1,24 +1,29 @@
+# This future-import is load-bearing, as `hashlib._Hash` is not actually importable.
+from __future__ import annotations
+
+import hashlib
 from hashlib import md5 as _md5
 from hashlib import sha1 as _sha1
+from typing import Any, Callable, Iterable, Optional
 
 from django.utils.encoding import force_bytes
 
 
-def md5_text(*args):
+def md5_text(*args: Any) -> hashlib._Hash:
     m = _md5()
     for x in args:
         m.update(force_bytes(x, errors="replace"))
     return m
 
 
-def sha1_text(*args):
+def sha1_text(*args: Any) -> hashlib._Hash:
     m = _sha1()
     for x in args:
         m.update(force_bytes(x, errors="replace"))
     return m
 
 
-def hash_value(h, value):
+def hash_value(h: hashlib._Hash, value: Any) -> None:
     if value is None:
         h.update(b"\x00")
     elif value is True:
@@ -44,7 +49,11 @@ def hash_value(h, value):
         raise TypeError("Invalid hash value")
 
 
-def hash_values(values, seed=None, algorithm=_md5):
+def hash_values(
+    values: Iterable[Any],
+    seed: Optional[str] = None,
+    algorithm: Callable[[], hashlib._Hash] = _md5,
+) -> str:
     h = _md5()
     if seed is not None:
         h.update(("%s\xff" % seed).encode("utf-8"))