Browse Source

Add TYPE_CHECKING when possible

David Burke 8 months ago
parent
commit
831ab75f4c

+ 8 - 6
apps/event_ingest/javascript_event_processor.py

@@ -9,7 +9,10 @@ from symbolic import SourceMapView, SourceView
 from apps.files.models import File
 from sentry.utils.safe import get_path
 
-from .schema import IssueEventSchema, StackTrace, StackTraceFrame
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+    from .schema import IssueEventSchema, StackTrace, StackTraceFrame
 
 UNKNOWN_MODULE = "<unknown module>"
 CLEAN_MODULE_RE = re.compile(
@@ -61,17 +64,17 @@ class JavascriptEventProcessor:
     Based partially on sentry/lang/javascript/processor.py
     """
 
-    def __init__(self, release_id: int, data: IssueEventSchema):
+    def __init__(self, release_id: int, data: "IssueEventSchema"):
         self.release_id = release_id
         self.data = data
 
-    def get_stacktraces(self) -> list[StackTrace]:
+    def get_stacktraces(self) -> list["StackTrace"]:
         data = self.data
         if data.exception and not isinstance(data.exception, list):
             return [e.stacktrace for e in data.exception.values if e.stacktrace]
         return []
 
-    def get_valid_frames(self, stacktraces) -> list[StackTraceFrame]:
+    def get_valid_frames(self, stacktraces) -> list["StackTraceFrame"]:
         frames = [stacktrace.frames for stacktrace in stacktraces]
 
         merged = list(itertools.chain(*frames))
@@ -160,8 +163,7 @@ class JavascriptEventProcessor:
             past_lines = min(len(source), token.src_line + 5)
             frame.context_line = source[token.src_line]
             frame.pre_context = source[pre_lines : token.src_line]
-            frame.post_context = source[token.src_line + 1: past_lines]
-
+            frame.post_context = source[token.src_line + 1 : past_lines]
 
     def transform(self):
         stacktraces = self.get_stacktraces()

+ 6 - 5
apps/event_ingest/utils.py

@@ -1,19 +1,20 @@
 import hashlib
-from typing import List, Optional, Union
+from typing import TYPE_CHECKING, List, Optional, Union
 
 from django.core.cache import cache
 
-from apps.issue_events.models import IssueEventType
-
 from .schema import EventMessage
 
+if TYPE_CHECKING:
+    from apps.issue_events.models import IssueEventType
+
 
-def default_hash_input(title: str, culprit: str, type: IssueEventType) -> str:
+def default_hash_input(title: str, culprit: str, type: "IssueEventType") -> str:
     return title + culprit + str(type)
 
 
 def generate_hash(
-    title: str, culprit: str, type: IssueEventType, extra: Optional[List[str]] = None
+    title: str, culprit: str, type: "IssueEventType", extra: Optional[List[str]] = None
 ) -> str:
     """Generate insecure hash used for grouping issues"""
     if extra:

+ 6 - 2
apps/importer/importer.py

@@ -1,3 +1,5 @@
+from typing import TYPE_CHECKING
+
 import aiohttp
 import tablib
 from asgiref.sync import sync_to_async
@@ -8,13 +10,15 @@ from apps.organizations_ext.admin import OrganizationResource, OrganizationUserR
 from apps.organizations_ext.models import OrganizationUser, OrganizationUserRole
 from apps.projects.admin import ProjectKeyResource, ProjectResource
 from apps.projects.models import Project
-from apps.shared.types import TypeJson
 from apps.teams.admin import TeamResource
 from apps.users.admin import UserResource
 from apps.users.models import User
 
 from .exceptions import ImporterException
 
+if TYPE_CHECKING:
+    from apps.shared.types import TypeJson
+
 
 class GlitchTipImporter:
     """
@@ -59,7 +63,7 @@ class GlitchTipImporter:
         await self.import_projects()
         await self.import_teams()
 
-    async def get(self, url: str) -> TypeJson:
+    async def get(self, url: str) -> "TypeJson":
         async with aiohttp.ClientSession() as session:
             async with session.get(url, headers=self.headers) as res:
                 return await res.json()

+ 9 - 7
glitchtip/api/pagination.py

@@ -1,11 +1,10 @@
 import inspect
 from abc import abstractmethod
 from functools import partial, wraps
-from typing import Any, AsyncGenerator, Callable, List, Type, Union
+from typing import TYPE_CHECKING, Any, AsyncGenerator, Callable, List, Type, Union
 from urllib import parse
 
 from asgiref.sync import sync_to_async
-from django.db.models import QuerySet
 from django.http import HttpRequest, HttpResponse
 from django.utils.module_loading import import_string
 from ninja.conf import settings as ninja_settings
@@ -19,18 +18,21 @@ from ninja.utils import (
 
 from .cursor_pagination import CursorPagination, _clamp, _reverse_order
 
+if TYPE_CHECKING:
+    from django.db.models import QuerySet
+
 
 class AsyncPaginationBase(PaginationBase):
     @abstractmethod
     async def apaginate_queryset(
         self,
-        queryset: QuerySet,
+        queryset: "QuerySet",
         pagination: Any,
         **params: Any,
     ) -> Any:
         pass  # pragma: no cover
 
-    async def _aitems_count(self, queryset: QuerySet) -> int:
+    async def _aitems_count(self, queryset: "QuerySet") -> int:
         try:
             return await queryset.all().acount()
         except AttributeError:
@@ -45,7 +47,7 @@ class AsyncLinkHeaderPagination(CursorPagination):
 
     async def apaginate_queryset(
         self,
-        queryset: QuerySet,
+        queryset: "QuerySet",
         pagination: CursorPagination.Input,
         request: HttpRequest,
         response: HttpResponse,
@@ -163,7 +165,7 @@ class AsyncLinkHeaderPagination(CursorPagination):
 
         return page
 
-    async def _aitems_count(self, queryset: QuerySet) -> int:
+    async def _aitems_count(self, queryset: "QuerySet") -> int:
         return await queryset.order_by()[: self.max_hits].acount()  # type: ignore
 
 
@@ -187,7 +189,7 @@ def _inject_pagination(
                 items, pagination=pagination_params, request=request, **kwargs
             )
 
-            async def evaluate(results: Union[List, QuerySet]) -> AsyncGenerator:
+            async def evaluate(results: Union[List, "QuerySet"]) -> AsyncGenerator:
                 for result in results:
                     yield result
 

+ 0 - 1
pyproject.toml

@@ -66,7 +66,6 @@ tblib = "^3.0.0"
 
 [tool.ruff.lint]
 extend-select = ["I"]
-flake8-type-checking.quote-annotations = false
 
 [tool.ruff.lint.per-file-ignores]
 "*/migrations/*.py" = ["I"]