Skip to content

Commit 8ffdc19

Browse files
committed
ref: More type information
1 parent f1ef8bf commit 8ffdc19

File tree

13 files changed

+135
-84
lines changed

13 files changed

+135
-84
lines changed

mypy.ini

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
[mypy]
22
allow_redefinition = True
3+
disallow_any_generics = True
4+
warn_redundant_casts = True
5+
6+
[mypy-sentry_sdk.integrations.*]
7+
disallow_any_generics = False
8+
9+
[mypy-sentry_sdk.utils]
10+
disallow_any_generics = False

sentry_sdk/client.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
from sentry_sdk.utils import ContextVar
1919

2020
if False:
21-
from sentry_sdk.consts import ClientOptions
22-
from sentry_sdk.scope import Scope
2321
from typing import Any
22+
from typing import Callable
2423
from typing import Dict
2524
from typing import Optional
2625

26+
from sentry_sdk.consts import ClientOptions
27+
from sentry_sdk.scope import Scope
28+
from sentry_sdk.utils import Event, Hint
29+
2730

2831
_client_init_debug = ContextVar("client_init_debug")
2932

@@ -94,11 +97,11 @@ def dsn(self):
9497

9598
def _prepare_event(
9699
self,
97-
event, # type: Dict[str, Any]
98-
hint, # type: Optional[Dict[str, Any]]
100+
event, # type: Event
101+
hint, # type: Optional[Hint]
99102
scope, # type: Optional[Scope]
100103
):
101-
# type: (...) -> Optional[Dict[str, Any]]
104+
# type: (...) -> Optional[Event]
102105
if event.get("timestamp") is None:
103106
event["timestamp"] = datetime.utcnow()
104107

@@ -158,7 +161,7 @@ def _prepare_event(
158161
return event
159162

160163
def _is_ignored_error(self, event, hint):
161-
# type: (Dict[str, Any], Dict[str, Any]) -> bool
164+
# type: (Event, Hint) -> bool
162165
exc_info = hint.get("exc_info")
163166
if exc_info is None:
164167
return False
@@ -180,8 +183,8 @@ def _is_ignored_error(self, event, hint):
180183

181184
def _should_capture(
182185
self,
183-
event, # type: Dict[str, Any]
184-
hint, # type: Dict[str, Any]
186+
event, # type: Event
187+
hint, # type: Hint
185188
scope=None, # type: Scope
186189
):
187190
# type: (...) -> bool
@@ -227,6 +230,7 @@ def capture_event(self, event, hint=None, scope=None):
227230
return rv
228231

229232
def close(self, timeout=None, callback=None):
233+
# type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
230234
"""
231235
Close the client and shut down the transport. Arguments have the same
232236
semantics as `self.flush()`.
@@ -237,6 +241,7 @@ def close(self, timeout=None, callback=None):
237241
self.transport = None
238242

239243
def flush(self, timeout=None, callback=None):
244+
# type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
240245
"""
241246
Wait `timeout` seconds for the current events to be sent. If no
242247
`timeout` is provided, the `shutdown_timeout` option value is used.

sentry_sdk/consts.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
from typing import Callable
77
from typing import Union
88
from typing import List
9+
from typing import Type
910

1011
from sentry_sdk.transport import Transport
1112
from sentry_sdk.integrations import Integration
1213

14+
from sentry_sdk.utils import Event, EventProcessor, BreadcrumbProcessor
15+
1316
ClientOptions = TypedDict(
1417
"ClientOptions",
1518
{
@@ -25,15 +28,17 @@
2528
"in_app_exclude": List[str],
2629
"default_integrations": bool,
2730
"dist": Optional[str],
28-
"transport": Optional[Union[Transport, type, Callable]],
31+
"transport": Optional[
32+
Union[Transport, Type[Transport], Callable[[Event], None]]
33+
],
2934
"sample_rate": int,
3035
"send_default_pii": bool,
3136
"http_proxy": Optional[str],
3237
"https_proxy": Optional[str],
3338
"ignore_errors": List[type],
3439
"request_bodies": str,
35-
"before_send": Optional[Callable],
36-
"before_breadcrumb": Optional[Callable],
40+
"before_send": Optional[EventProcessor],
41+
"before_breadcrumb": Optional[BreadcrumbProcessor],
3742
"debug": bool,
3843
"attach_stacktrace": bool,
3944
"ca_certs": Optional[str],

sentry_sdk/hub.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717

1818

1919
if False:
20+
from contextlib import ContextManager
21+
2022
from typing import Union
2123
from typing import Any
2224
from typing import Optional
23-
from typing import Dict
2425
from typing import Tuple
2526
from typing import List
2627
from typing import Callable
28+
from typing import Generator
2729
from typing import overload
28-
from contextlib import ContextManager
30+
2931
from sentry_sdk.integrations import Integration
32+
from sentry_sdk.utils import Event, Hint, Breadcrumb, BreadcrumbHint
3033
else:
3134

3235
def overload(x):
@@ -251,7 +254,7 @@ def bind_client(self, new):
251254
self._stack[-1] = (new, top[1])
252255

253256
def capture_event(self, event, hint=None):
254-
# type: (Dict[str, Any], Dict[str, Any]) -> Optional[str]
257+
# type: (Event, Hint) -> Optional[str]
255258
"""Captures an event. The return value is the ID of the event.
256259
257260
The event is a dictionary following the Sentry v7/v8 protocol
@@ -268,7 +271,7 @@ def capture_event(self, event, hint=None):
268271
return None
269272

270273
def capture_message(self, message, level=None):
271-
# type: (str, Optional[Any]) -> Optional[str]
274+
# type: (str, Optional[str]) -> Optional[str]
272275
"""Captures a message. The message is just a string. If no level
273276
is provided the default level is `info`.
274277
"""
@@ -308,7 +311,7 @@ def _capture_internal_exception(self, exc_info):
308311
logger.error("Internal error in sentry_sdk", exc_info=exc_info)
309312

310313
def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
311-
# type: (Dict[str, Any], Dict[str, Any], **Any) -> None
314+
# type: (Optional[Breadcrumb], Optional[BreadcrumbHint], **Any) -> None
312315
"""Adds a breadcrumb. The breadcrumbs are a dictionary with the
313316
data as the sentry v7/v8 protocol expects. `hint` is an optional
314317
value that can be used by `before_breadcrumb` to customize the
@@ -319,26 +322,27 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
319322
logger.info("Dropped breadcrumb because no client bound")
320323
return
321324

322-
crumb = dict(crumb or ()) # type: Dict[str, Any]
325+
crumb = dict(crumb or ()) # type: Breadcrumb
323326
crumb.update(kwargs)
324327
if not crumb:
325328
return
326329

327-
hint = dict(hint or ())
330+
hint = dict(hint or ()) # type: Hint
328331

329332
if crumb.get("timestamp") is None:
330333
crumb["timestamp"] = datetime.utcnow()
331334
if crumb.get("type") is None:
332335
crumb["type"] = "default"
333336

334-
original_crumb = crumb
335337
if client.options["before_breadcrumb"] is not None:
336-
crumb = client.options["before_breadcrumb"](crumb, hint)
338+
new_crumb = client.options["before_breadcrumb"](crumb, hint)
339+
else:
340+
new_crumb = crumb
337341

338-
if crumb is not None:
339-
scope._breadcrumbs.append(crumb)
342+
if new_crumb is not None:
343+
scope._breadcrumbs.append(new_crumb)
340344
else:
341-
logger.info("before breadcrumb dropped breadcrumb (%s)", original_crumb)
345+
logger.info("before breadcrumb dropped breadcrumb (%s)", crumb)
342346

343347
max_breadcrumbs = client.options["max_breadcrumbs"] # type: int
344348
while len(scope._breadcrumbs) > max_breadcrumbs:
@@ -410,12 +414,14 @@ def inner():
410414
return inner()
411415

412416
def flush(self, timeout=None, callback=None):
417+
# type: (Optional[float], Optional[Callable[[int, float], None]]) -> None
413418
"""Alias for self.client.flush"""
414419
client, scope = self._stack[-1]
415420
if client is not None:
416421
return client.flush(timeout=timeout, callback=callback)
417422

418423
def iter_trace_propagation_headers(self):
424+
# type: () -> Generator[Tuple[str, str], None, None]
419425
client, scope = self._stack[-1]
420426
if scope._span is None:
421427
return

sentry_sdk/integrations/argv.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from sentry_sdk.scope import add_global_event_processor
88

99
if False:
10-
from typing import Any
11-
from typing import Dict
10+
from typing import Optional
11+
12+
from sentry_sdk.utils import Event, Hint
1213

1314

1415
class ArgvIntegration(Integration):
@@ -19,7 +20,7 @@ def setup_once():
1920
# type: () -> None
2021
@add_global_event_processor
2122
def processor(event, hint):
22-
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
23+
# type: (Event, Optional[Hint]) -> Optional[Event]
2324
if Hub.current.get_integration(ArgvIntegration) is not None:
2425
extra = event.setdefault("extra", {})
2526
# If some event processor decided to set extra to e.g. an

sentry_sdk/integrations/dedupe.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
from sentry_sdk.scope import add_global_event_processor
55

66
if False:
7-
from typing import Any
8-
from typing import Dict
97
from typing import Optional
108

9+
from sentry_sdk.utils import Event, Hint
10+
1111

1212
class DedupeIntegration(Integration):
1313
identifier = "dedupe"
@@ -21,13 +21,21 @@ def setup_once():
2121
# type: () -> None
2222
@add_global_event_processor
2323
def processor(event, hint):
24-
# type: (Dict[str, Any], Dict[str, Any]) -> Optional[Dict[str, Any]]
24+
# type: (Event, Optional[Hint]) -> Optional[Event]
25+
if hint is None:
26+
return event
27+
2528
integration = Hub.current.get_integration(DedupeIntegration)
26-
if integration is not None:
27-
exc_info = hint.get("exc_info", None)
28-
if exc_info is not None:
29-
exc = exc_info[1]
30-
if integration._last_seen.get(None) is exc:
31-
return None
32-
integration._last_seen.set(exc)
29+
30+
if integration is None:
31+
return event
32+
33+
exc_info = hint.get("exc_info", None)
34+
if exc_info is None:
35+
return event
36+
37+
exc = exc_info[1]
38+
if integration._last_seen.get(None) is exc:
39+
return None
40+
integration._last_seen.set(exc)
3341
return event

sentry_sdk/integrations/django/__init__.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010

1111
if False:
1212
from typing import Any
13+
from typing import Callable
1314
from typing import Dict
15+
from typing import List
16+
from typing import Optional
1417
from typing import Tuple
1518
from typing import Union
16-
from sentry_sdk.integrations.wsgi import _ScopedResponse
17-
from typing import Callable
19+
1820
from django.core.handlers.wsgi import WSGIRequest # type: ignore
1921
from django.http.response import HttpResponse # type: ignore
2022
from django.http.request import QueryDict # type: ignore
2123
from django.utils.datastructures import MultiValueDict # type: ignore
22-
from typing import List
24+
25+
from sentry_sdk.integrations.wsgi import _ScopedResponse
26+
from sentry_sdk.utils import Event, Hint
2327

2428

2529
try:
@@ -124,7 +128,10 @@ def sentry_patched_get_response(self, request):
124128

125129
@add_global_event_processor
126130
def process_django_templates(event, hint):
127-
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
131+
# type: (Event, Optional[Hint]) -> Optional[Event]
132+
if hint is None:
133+
return event
134+
128135
exc_info = hint.get("exc_info", None)
129136

130137
if exc_info is None:

sentry_sdk/integrations/modules.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from typing import Tuple
1111
from typing import Iterator
1212

13+
from sentry_sdk.utils import Event
14+
1315
_installed_modules = None
1416

1517

@@ -40,7 +42,7 @@ def setup_once():
4042
# type: () -> None
4143
@add_global_event_processor
4244
def processor(event, hint):
43-
# type: (Dict[str, Any], Dict[str, Any]) -> Dict[str, Any]
45+
# type: (Event, Any) -> Dict[str, Any]
4446
if Hub.current.get_integration(ModulesIntegration) is not None:
4547
event["modules"] = dict(_get_installed_modules())
4648
return event

sentry_sdk/integrations/sanic.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
from sanic.handlers import ErrorHandler # type: ignore
2020

2121
if False:
22-
from sanic.request import Request # type: ignore
23-
2422
from typing import Any
2523
from typing import Callable
26-
from typing import Dict
2724
from typing import Optional
2825
from typing import Union
2926
from typing import Tuple
30-
from sanic.request import RequestParameters
27+
28+
from sanic.request import Request, RequestParameters # type: ignore
29+
30+
from sentry_sdk.utils import Event, EventProcessor, Hint
3131

3232

3333
class SanicIntegration(Integration):
@@ -143,12 +143,12 @@ def _capture_exception(exception):
143143

144144

145145
def _make_request_processor(weak_request):
146-
# type: (Callable[[], Request]) -> Callable
146+
# type: (Callable[[], Request]) -> EventProcessor
147147
def sanic_processor(event, hint):
148-
# type: (Dict[str, Any], Dict[str, Any]) -> Optional[Dict[str, Any]]
148+
# type: (Event, Optional[Hint]) -> Optional[Event]
149149

150150
try:
151-
if issubclass(hint["exc_info"][0], SanicException):
151+
if hint and issubclass(hint["exc_info"][0], SanicException):
152152
return None
153153
except KeyError:
154154
pass

0 commit comments

Comments
 (0)