2828 from typing import Callable
2929 from typing import Generator
3030 from typing import Type
31+ from typing import TypeVar
3132 from typing import overload
3233
3334 from sentry_sdk .integrations import Integration
3435 from sentry_sdk .utils import Event , Hint , Breadcrumb , BreadcrumbHint
36+
37+ T = TypeVar ("T" )
3538else :
3639
3740 def overload (x ):
41+ # type: (T) -> T
3842 return x
3943
4044
@@ -60,12 +64,15 @@ def __enter__(self):
6064 return self
6165
6266 def __exit__ (self , exc_type , exc_value , tb ):
67+ # type: (Any, Any, Any) -> None
6368 c = self ._client
6469 if c is not None :
6570 c .close ()
6671
6772
6873def init (* args , ** kwargs ):
74+ # type: (*str, **Any) -> ContextManager[Any]
75+ # TODO: https://github.com/getsentry/sentry-python/issues/272
6976 """Initializes the SDK and optionally integrations.
7077
7178 This takes the same arguments as the client constructor.
@@ -92,16 +99,19 @@ def current(self):
9299
93100 @property
94101 def main (self ):
102+ # type: () -> Hub
95103 """Returns the main instance of the hub."""
96104 return GLOBAL_HUB
97105
98106
99107class _HubManager (object ):
100108 def __init__ (self , hub ):
109+ # type: (Hub) -> None
101110 self ._old = Hub .current
102111 _local .set (hub )
103112
104113 def __exit__ (self , exc_type , exc_value , tb ):
114+ # type: (Any, Any, Any) -> None
105115 _local .set (self ._old )
106116
107117
@@ -119,6 +129,7 @@ def __enter__(self):
119129 return scope
120130
121131 def __exit__ (self , exc_type , exc_value , tb ):
132+ # type: (Any, Any, Any) -> None
122133 current_len = len (self ._hub ._stack )
123134 if current_len < self ._original_len :
124135 logger .error (
@@ -200,6 +211,7 @@ def __exit__(
200211 _local .set (old )
201212
202213 def run (self , callback ):
214+ # type: (Callable[[], T]) -> T
203215 """Runs a callback in the context of the hub. Alternatively the
204216 with statement can be used on the hub directly.
205217 """
@@ -260,6 +272,7 @@ def last_event_id(self):
260272 return self ._last_event_id
261273
262274 def bind_client (self , new ):
275+ # type: (Optional[Client]) -> None
263276 """Binds a new client to the hub."""
264277 top = self ._stack [- 1 ]
265278 self ._stack [- 1 ] = (new , top [1 ])
@@ -361,16 +374,19 @@ def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
361374 scope ._breadcrumbs .popleft ()
362375
363376 @overload # noqa
364- def push_scope (self ):
365- # type: () -> ContextManager[Scope]
377+ def push_scope (self , callback = None ):
378+ # type: (Optional[None] ) -> ContextManager[Scope]
366379 pass
367380
368381 @overload # noqa
369382 def push_scope (self , callback ):
370383 # type: (Callable[[Scope], None]) -> None
371384 pass
372385
373- def push_scope (self , callback = None ): # noqa
386+ def push_scope ( # noqa
387+ self , callback = None # type: Optional[Callable[[Scope], None]]
388+ ):
389+ # type: (...) -> Optional[ContextManager[Scope]]
374390 """Pushes a new layer on the scope stack. Returns a context manager
375391 that should be used to pop the scope again. Alternatively a callback
376392 can be provided that is executed in the context of the scope.
@@ -390,23 +406,28 @@ def push_scope(self, callback=None): # noqa
390406 scope = push_scope
391407
392408 def pop_scope_unsafe (self ):
409+ # type: () -> Tuple[Optional[Client], Scope]
393410 """Pops a scope layer from the stack. Try to use the context manager
394411 `push_scope()` instead."""
395412 rv = self ._stack .pop ()
396413 assert self ._stack , "stack must have at least one layer"
397414 return rv
398415
399416 @overload # noqa
400- def configure_scope (self ):
401- # type: () -> ContextManager[Scope]
417+ def configure_scope (self , callback = None ):
418+ # type: (Optional[None] ) -> ContextManager[Scope]
402419 pass
403420
404421 @overload # noqa
405422 def configure_scope (self , callback ):
406423 # type: (Callable[[Scope], None]) -> None
407424 pass
408425
409- def configure_scope (self , callback = None ): # noqa
426+ def configure_scope ( # noqa
427+ self , callback = None # type: Optional[Callable[[Scope], None]]
428+ ): # noqa
429+ # type: (...) -> Optional[ContextManager[Scope]]
430+
410431 """Reconfigures the scope."""
411432
412433 client , scope = self ._stack [- 1 ]
@@ -418,6 +439,7 @@ def configure_scope(self, callback=None): # noqa
418439
419440 @contextmanager
420441 def inner ():
442+ # type: () -> Generator[Scope, None, None]
421443 if client is not None :
422444 yield scope
423445 else :
0 commit comments