Browse Source

ref(access): Add type hints to roles/manager.py (#31595)

Ryan Skonnord 3 years ago
parent
commit
24abd34441
2 changed files with 31 additions and 17 deletions
  1. 1 0
      mypy.ini
  2. 30 17
      src/sentry/roles/manager.py

+ 1 - 0
mypy.ini

@@ -50,6 +50,7 @@ files = src/sentry/analytics/,
         src/sentry/processing/realtime_metrics/,
         src/sentry/ratelimits/,
         src/sentry/release_health/,
+        src/sentry/roles/manager.py,
         src/sentry/search/base.py,
         src/sentry/search/events/builder.py,
         src/sentry/search/events/constants.py,

+ 30 - 17
src/sentry/roles/manager.py

@@ -1,8 +1,17 @@
 from collections import OrderedDict
+from typing import Dict, Iterable, Mapping, Optional, Tuple
 
 
 class Role:
-    def __init__(self, priority, id, name, desc="", scopes=(), is_global=False):
+    def __init__(
+        self,
+        priority: int,
+        id: str,
+        name: str,
+        desc: str = "",
+        scopes: Iterable[str] = (),
+        is_global: bool = False,
+    ) -> None:
         assert len(id) <= 32, "Role id must be no more than 32 characters"
 
         self.priority = priority
@@ -12,22 +21,26 @@ class Role:
         self.scopes = frozenset(scopes)
         self.is_global = bool(is_global)
 
-    def __str__(self):
+    def __str__(self) -> str:
         return str(self.name)
 
-    def __repr__(self):
+    def __repr__(self) -> str:
         return f"<Role: {self.id}>"
 
-    def has_scope(self, scope):
+    def has_scope(self, scope: str) -> bool:
         return scope in self.scopes
 
 
 class RoleManager:
-    def __init__(self, config, default=None):
+    def __init__(
+        self,
+        config: Iterable[Mapping[str, str]],
+        default: Optional[str] = None,
+    ) -> None:
         role_list = []
-        self._roles = OrderedDict()
-        for idx, role in enumerate(config):
-            role = Role(idx, **role)
+        self._roles: Dict[str, Role] = OrderedDict()
+        for idx, role_cfg in enumerate(config):
+            role = Role(idx, **role_cfg)
             role_list.append(role)
             self._roles[role.id] = role
 
@@ -40,33 +53,33 @@ class RoleManager:
 
         self._top_dog = role_list[-1]
 
-    def __iter__(self):
+    def __iter__(self) -> Iterable[Role]:
         yield from self._roles.values()
 
-    def can_manage(self, role, other):
+    def can_manage(self, role: str, other: str) -> bool:
         return self.get(role).priority >= self.get(other).priority
 
-    def get(self, id):
+    def get(self, id: str) -> Role:
         return self._roles[id]
 
-    def get_all(self):
+    def get_all(self) -> Iterable[Role]:
         return list(self._roles.values())
 
-    def get_choices(self):
+    def get_choices(self) -> Iterable[Tuple[str, str]]:
         return self._choices
 
-    def get_default(self):
+    def get_default(self) -> Role:
         return self._default
 
-    def get_top_dog(self):
+    def get_top_dog(self) -> Role:
         return self._top_dog
 
-    def with_scope(self, scope):
+    def with_scope(self, scope: str) -> Iterable[Role]:
         for role in self.get_all():
             if role.has_scope(scope):
                 yield role
 
-    def with_any_scope(self, scopes):
+    def with_any_scope(self, scopes: Iterable[str]) -> Iterable[Role]:
         for role in self.get_all():
             if any(role.has_scope(scope) for scope in scopes):
                 yield role