Skip to content

Commit 984290d

Browse files
ref(starlette): Add package to typing group (#6728)
Add `starlette` to `typing` dependencies and resolve errors.
1 parent 4b98ef1 commit 984290d

4 files changed

Lines changed: 42 additions & 20 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ typing = [
6767
"httpx2",
6868
"botocore-stubs",
6969
"werkzeug",
70+
"starlette>=1.3.1",
7071
]
7172
test = [
7273
"dataclasses ; python_full_version < '3.7'",

sentry_sdk/integrations/mcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
if TYPE_CHECKING:
5656
from typing import Any, Callable, ContextManager, Optional, Tuple, Union
5757

58-
from starlette.types import Receive, Scope, Send # type: ignore[import-not-found]
58+
from starlette.types import Receive, Scope, Send
5959

6060
from sentry_sdk.traces import StreamedSpan
6161
from sentry_sdk.tracing import Span

sentry_sdk/integrations/starlette.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,39 @@
4040
)
4141

4242
if TYPE_CHECKING:
43-
from typing import Any, Awaitable, Callable, Container, Dict, Optional, Tuple, Union
43+
from typing import (
44+
Any,
45+
Awaitable,
46+
Callable,
47+
Container,
48+
Dict,
49+
Optional,
50+
Tuple,
51+
Union,
52+
)
4453

4554
from sentry_sdk._types import Event, HttpStatusCodeRange
46-
4755
try:
48-
import starlette # type: ignore
56+
import starlette
4957
from starlette import __version__ as STARLETTE_VERSION
50-
from starlette.applications import Starlette # type: ignore
51-
from starlette.datastructures import ( # type: ignore
58+
from starlette.applications import Starlette
59+
from starlette.datastructures import (
5260
UploadFile,
5361
)
54-
from starlette.middleware import Middleware # type: ignore
55-
from starlette.middleware.authentication import ( # type: ignore
62+
from starlette.middleware import Middleware
63+
from starlette.middleware.authentication import (
5664
AuthenticationMiddleware,
5765
)
58-
from starlette.requests import Request # type: ignore
59-
from starlette.routing import Match # type: ignore
60-
from starlette.types import ASGIApp, Receive, Send # type: ignore
66+
from starlette.requests import Request
67+
from starlette.routing import Match
68+
from starlette.types import ASGIApp, Receive, Send
6169
from starlette.types import Scope as StarletteScope
6270
except ImportError:
6371
raise DidNotEnable("Starlette is not installed")
6472

6573
try:
6674
# Starlette 0.20
67-
from starlette.middleware.exceptions import ExceptionMiddleware # type: ignore
75+
from starlette.middleware.exceptions import ExceptionMiddleware
6876
except ImportError:
6977
# Startlette 0.19.1
7078
from starlette.exceptions import ExceptionMiddleware # type: ignore
@@ -156,8 +164,10 @@ def setup_once() -> None:
156164
patch_templates()
157165

158166

159-
def _enable_span_for_middleware(middleware_class: "Any") -> type:
160-
old_call = middleware_class.__call__
167+
def _enable_span_for_middleware(
168+
middleware_class: "Any",
169+
) -> "Any":
170+
old_call: "Callable[..., Awaitable[Any]]" = middleware_class.__call__
161171

162172
async def _create_span_call(
163173
app: "Any",
@@ -430,7 +440,7 @@ def _sentry_middleware_init(
430440
if cls == ExceptionMiddleware:
431441
patch_exception_middleware(cls)
432442

433-
Middleware.__init__ = _sentry_middleware_init
443+
Middleware.__init__ = _sentry_middleware_init # type: ignore[method-assign]
434444

435445

436446
def patch_asgi_app(root_path_in_path: "_RootPathInPath") -> None:
@@ -462,7 +472,7 @@ async def _sentry_patched_asgi_app(
462472

463473
return await middleware(scope, receive, send)
464474

465-
Starlette.__call__ = _sentry_patched_asgi_app
475+
Starlette.__call__ = _sentry_patched_asgi_app # type: ignore[method-assign]
466476

467477

468478
# This was vendored in from Starlette to support Starlette 0.19.1 because
@@ -662,7 +672,7 @@ def patch_templates() -> None:
662672

663673
# https://github.com/Kludex/starlette/commit/96479daca2e4bd8157f68d914fd162aa94eff73a
664674
try:
665-
from starlette.templating import Jinja2Templates # type: ignore
675+
from starlette.templating import Jinja2Templates
666676
except ImportError:
667677
return
668678

@@ -692,7 +702,7 @@ def add_sentry_trace_meta(request: "Request") -> "Dict[str, Any]":
692702

693703
return old_jinja2templates_init(self, *args, **kwargs)
694704

695-
Jinja2Templates.__init__ = _sentry_jinja2templates_init
705+
Jinja2Templates.__init__ = _sentry_jinja2templates_init # type: ignore[method-assign]
696706

697707

698708
class StarletteRequestExtractor:
@@ -701,8 +711,6 @@ class StarletteRequestExtractor:
701711
(like form data or cookies) and adds it to the Sentry event.
702712
"""
703713

704-
request: "Request" = None
705-
706714
def __init__(self: "StarletteRequestExtractor", request: "Request") -> None:
707715
self.request = request
708716

uv.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)