Browse Source

ref: fix a few misc mypy errors (#53892)

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

+ 0 - 9
pyproject.toml

@@ -386,7 +386,6 @@ module = [
     "sentry.api.validators.email",
     "sentry.api.validators.project_codeowners",
     "sentry.api.validators.servicehook",
-    "sentry.apidocs.schema",
     "sentry.auth.access",
     "sentry.auth.helper",
     "sentry.auth.provider",
@@ -571,7 +570,6 @@ module = [
     "sentry.issues.occurrence_consumer",
     "sentry.issues.search",
     "sentry.issues.status_change",
-    "sentry.lint.engine",
     "sentry.mail.adapter",
     "sentry.mail.forms.assigned_to",
     "sentry.mail.forms.member_team",
@@ -623,9 +621,6 @@ module = [
     "sentry.monkey.pickle",
     "sentry.net.http",
     "sentry.net.socket",
-    "sentry.new_migrations.monkey",
-    "sentry.newsletter.dummy",
-    "sentry.nodestore.bigtable.backend",
     "sentry.notifications.additional_attachment_manager",
     "sentry.notifications.helpers",
     "sentry.notifications.notifications.activity.base",
@@ -724,8 +719,6 @@ module = [
     "sentry.search.snuba.backend",
     "sentry.search.snuba.executors",
     "sentry.search.utils",
-    "sentry.security.emails",
-    "sentry.security.utils",
     "sentry.sentry_apps.apps",
     "sentry.sentry_apps.components",
     "sentry.sentry_apps.installations",
@@ -1027,9 +1020,7 @@ module = [
     "tests.sentry.issues.test_ongoing",
     "tests.sentry.issues.test_status_change",
     "tests.sentry.issues.test_utils",
-    "tests.sentry.lang.javascript.test_errorlocale",
     "tests.sentry.lang.javascript.test_processor",
-    "tests.sentry.lang.native.test_processing",
     "tests.sentry.loader.test_dynamic_sdk_options",
     "tests.sentry.logging.test_handler",
     "tests.sentry.mail",

+ 1 - 1
src/sentry/apidocs/schema.py

@@ -18,7 +18,7 @@ class SentrySchema(AutoSchema):
             return docstring[0]
         return super().get_operation_id()
 
-    def get_description(self) -> str:
+    def get_description(self) -> str:  # type: ignore[override]
         """
         Docstring is used as a description for the endpoint. The operation ID is included in this.
         """

+ 2 - 2
src/sentry/lint/engine.py

@@ -7,6 +7,7 @@ Our linter engine needs to run in 3 different scenarios:
 For the js only path, we should not depend on any packages outside the
 python stdlib to prevent the need to install the world just to run eslint.
 """
+from __future__ import annotations
 
 # Import the stdlib json instead of sentry.utils.json, since this command is
 # run in setup.py
@@ -16,7 +17,6 @@ import subprocess
 import sys
 from subprocess import Popen, check_output
 
-os.environ["PYFLAKES_NODOCTEST"] = "1"
 os.environ["SENTRY_PRECOMMIT"] = "1"
 
 
@@ -72,7 +72,7 @@ def get_files_for_list(file_list):
 
 def get_js_files(file_list=None, snapshots=False):
     if snapshots:
-        extensions = (".js", ".jsx", ".ts", ".tsx", ".jsx.snap", ".js.snap")
+        extensions: tuple[str, ...] = (".js", ".jsx", ".ts", ".tsx", ".jsx.snap", ".js.snap")
     else:
         extensions = (".js", ".jsx", ".ts", ".tsx")
 

+ 1 - 1
src/sentry/new_migrations/monkey/__init__.py

@@ -76,7 +76,7 @@ def monkey_migrations():
     from django.db.migrations import executor, migration, writer
 
     # monkeypatch Django's migration executor and template.
-    executor.MigrationExecutor = SentryMigrationExecutor
+    executor.MigrationExecutor = SentryMigrationExecutor  # type: ignore[misc]
     migration.Migration.initial = None
     writer.MIGRATION_TEMPLATE = SENTRY_MIGRATION_TEMPLATE
     models.Field.deconstruct = deconstruct  # type: ignore[method-assign]

+ 29 - 20
src/sentry/newsletter/base.py

@@ -1,3 +1,8 @@
+from __future__ import annotations
+
+from typing import Any, Sequence
+
+from sentry.models.user import User
 from sentry.utils.services import Service
 
 
@@ -19,7 +24,7 @@ class Newsletter(Service):
 
     enabled = False
 
-    def is_enabled(self):
+    def is_enabled(self) -> bool:
         return self.enabled
 
     def optout_email(self, email, **kwargs):
@@ -29,7 +34,7 @@ class Newsletter(Service):
     Replacements for the functions below that only accept a single list_id argument
     """
 
-    def get_default_list_ids(self):
+    def get_default_list_ids(self) -> tuple[int, ...]:
         return self.DEFAULT_LISTS
 
     def get_subscriptions(self, user):
@@ -37,10 +42,11 @@ class Newsletter(Service):
 
     def update_subscriptions(
         self,
-        user,
-        list_ids=None,
-        subscribed=True,
-        create=None,
+        user: User,
+        *,
+        list_ids: Sequence[int] | None = None,
+        subscribed: bool = True,
+        create: bool | None = None,
         verified=None,
         subscribed_date=None,
         unsubscribed_date=None,
@@ -50,13 +56,14 @@ class Newsletter(Service):
 
     def create_or_update_subscriptions(
         self,
-        user,
-        list_ids=None,
-        subscribed=True,
+        user: User,
+        *,
+        list_ids: Sequence[int] | None = None,
+        subscribed: bool = True,
         verified=None,
         subscribed_date=None,
         unsubscribed_date=None,
-        **kwargs,
+        **kwargs: Any,
     ):
         return self.update_subscriptions(
             user=user,
@@ -74,31 +81,33 @@ class Newsletter(Service):
     accept multiple list IDs
     """
 
-    def get_default_list_id(self):
+    def get_default_list_id(self) -> int:
         return self.DEFAULT_LIST_ID
 
     def update_subscription(
         self,
-        user,
-        list_id=None,
-        subscribed=True,
-        create=None,
+        user: User,
+        *,
+        list_id: int | None = None,
+        subscribed: bool = True,
+        create: bool | None = None,
         verified=None,
         subscribed_date=None,
         unsubscribed_date=None,
-        **kwargs,
+        **kwargs: Any,
     ):
         return None
 
     def create_or_update_subscription(
         self,
-        user,
-        list_id=None,
-        subscribed=True,
+        user: User,
+        *,
+        list_id: int | None = None,
+        subscribed: bool = True,
         verified=None,
         subscribed_date=None,
         unsubscribed_date=None,
-        **kwargs,
+        **kwargs: Any,
     ):
         return self.update_subscription(
             user=user,

+ 23 - 6
src/sentry/newsletter/dummy.py

@@ -1,7 +1,12 @@
+from __future__ import annotations
+
 from collections import defaultdict
+from typing import Any, Sequence
 
 from django.utils import timezone
 
+from sentry.models.user import User
+
 from .base import Newsletter
 
 
@@ -65,8 +70,8 @@ class DummyNewsletter(Newsletter):
     store for tracking subscriptions, which means its not suitable for any real production use-case.
     """
 
-    def __init__(self, enabled=False):
-        self._subscriptions = defaultdict(dict)
+    def __init__(self, enabled: bool = False) -> None:
+        self._subscriptions: dict[User, dict[int, NewsletterSubscription]] = defaultdict(dict)
         self._enabled = enabled
 
     def enable(self):
@@ -81,10 +86,16 @@ class DummyNewsletter(Newsletter):
     def is_enabled(self):
         return self._enabled
 
-    def get_subscriptions(self, user):
+    def get_subscriptions(self, user: User):
         return {"subscriptions": list((self._subscriptions.get(user) or {}).values())}
 
-    def update_subscription(self, user, list_id=None, create=False, **kwargs):
+    def update_subscription(
+        self,
+        user: User,
+        list_id: int | None = None,
+        create: bool | None = False,
+        **kwargs: Any,
+    ) -> dict[int, NewsletterSubscription]:
         if not list_id:
             list_id = self.get_default_list_id()
 
@@ -96,7 +107,13 @@ class DummyNewsletter(Newsletter):
 
         return self._subscriptions[user]
 
-    def update_subscriptions(self, user, list_ids=None, create=False, **kwargs):
+    def update_subscriptions(
+        self,
+        user: User,
+        list_ids: Sequence[int] | None = None,
+        create: bool | None = False,
+        **kwargs: Any,
+    ):
         if not list_ids:
             list_ids = self.get_default_list_ids()
 
@@ -105,7 +122,7 @@ class DummyNewsletter(Newsletter):
 
         return self._subscriptions[user]
 
-    def optout_email(self, email, **kwargs):
+    def optout_email(self, email: str, **kwargs: Any) -> None:
         unsubscribe_date = timezone.now()
         for by_list in self._subscriptions.values():
             for subscription in by_list.values():

+ 3 - 1
src/sentry/nodestore/base.py

@@ -1,3 +1,5 @@
+from __future__ import annotations
+
 from threading import local
 
 import sentry_sdk
@@ -147,7 +149,7 @@ class NodeStorage(local, Service):
 
             return rv
 
-    def _get_bytes_multi(self, id_list):
+    def _get_bytes_multi(self, id_list: list[str]) -> dict[str, bytes | None]:
         """
         >>> nodestore._get_bytes_multi(['key1', 'key2')
         {

+ 4 - 2
src/sentry/nodestore/bigtable/backend.py

@@ -1,3 +1,5 @@
+from __future__ import annotations
+
 import os
 
 import sentry_sdk
@@ -59,8 +61,8 @@ class BigtableNodeStorage(NodeStorage):
     def _get_bytes(self, id):
         return self.store.get(id)
 
-    def _get_bytes_multi(self, id_list):
-        rv = {id: None for id in id_list}
+    def _get_bytes_multi(self, id_list: list[str]) -> dict[str, bytes | None]:
+        rv: dict[str, bytes | None] = {id: None for id in id_list}
         rv.update(self.store.get_many(id_list))
         return rv
 

+ 3 - 1
src/sentry/nodestore/django/backend.py

@@ -1,3 +1,5 @@
+from __future__ import annotations
+
 import logging
 import math
 import pickle
@@ -41,7 +43,7 @@ class DjangoNodeStorage(NodeStorage):
         except Node.DoesNotExist:
             return None
 
-    def _get_bytes_multi(self, id_list):
+    def _get_bytes_multi(self, id_list: list[str]) -> dict[str, bytes | None]:
         return {n.id: decompress(n.data) for n in Node.objects.filter(id__in=id_list)}
 
     def delete_multi(self, id_list):

+ 7 - 2
src/sentry/security/emails.py

@@ -1,8 +1,11 @@
+from __future__ import annotations
+
 from datetime import datetime
 from typing import TYPE_CHECKING, Any, Mapping, Optional
 
 from django.utils import timezone
 
+from sentry.services.hybrid_cloud.user.model import RpcUser
 from sentry.utils.email import MessageBuilder
 
 if TYPE_CHECKING:
@@ -10,9 +13,9 @@ if TYPE_CHECKING:
 
 
 def generate_security_email(
-    account: "User",
+    account: User | RpcUser,
     type: str,
-    actor: "User",
+    actor: User | RpcUser,
     ip_address: str,
     context: Optional[Mapping[str, Any]] = None,
     current_datetime: Optional[datetime] = None,
@@ -22,10 +25,12 @@ def generate_security_email(
 
     subject = "Security settings changed"
     if type == "mfa-removed":
+        assert context is not None
         assert "authenticator" in context
         template = "sentry/emails/mfa-removed.txt"
         html_template = "sentry/emails/mfa-removed.html"
     elif type == "mfa-added":
+        assert context is not None
         assert "authenticator" in context
         template = "sentry/emails/mfa-added.txt"
         html_template = "sentry/emails/mfa-added.html"

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