2121
2222if False :
2323 from contextlib import ContextManager
24+ from sys import _OptExcInfo
2425
2526 from typing import Union
2627 from typing import Any
2930 from typing import List
3031 from typing import Callable
3132 from typing import Generator
33+ from typing import Type
3234 from typing import overload
3335
3436 from sentry_sdk .integrations import Integration
@@ -39,8 +41,8 @@ def overload(x):
3941 return x
4042
4143
42- _local = ContextVar ("sentry_current_hub" )
43- _initial_client = None
44+ _local = ContextVar ("sentry_current_hub" ) # type: ignore
45+ _initial_client = None # type: Optional[weakref.ReferenceType[Client]]
4446
4547
4648def _should_send_default_pii ():
@@ -53,9 +55,11 @@ def _should_send_default_pii():
5355
5456class _InitGuard (object ):
5557 def __init__ (self , client ):
58+ # type: (Client) -> None
5659 self ._client = client
5760
5861 def __enter__ (self ):
62+ # type: () -> _InitGuard
5963 return self
6064
6165 def __exit__ (self , exc_type , exc_value , tb ):
@@ -106,6 +110,7 @@ def __exit__(self, exc_type, exc_value, tb):
106110
107111class _ScopeManager (object ):
108112 def __init__ (self , hub ):
113+ # type: (Hub) -> None
109114 self ._hub = hub
110115 self ._original_len = len (hub ._stack )
111116 self ._layer = hub ._stack [- 1 ]
@@ -160,8 +165,13 @@ class Hub(with_metaclass(HubMeta)): # type: ignore
160165
161166 _stack = None # type: List[Tuple[Optional[Client], Scope]]
162167
168+ # Mypy doesn't pick up on the metaclass.
169+ if False :
170+ current = None # type: Hub
171+ main = None # type: Hub
172+
163173 def __init__ (self , client_or_hub = None , scope = None ):
164- # type: (Union[Hub, Client], Optional[Any]) -> None
174+ # type: (Optional[ Union[Hub, Client] ], Optional[Any]) -> None
165175 if isinstance (client_or_hub , Hub ):
166176 hub = client_or_hub
167177 client , other_scope = hub ._stack [- 1 ]
@@ -200,7 +210,7 @@ def run(self, callback):
200210 return callback ()
201211
202212 def get_integration (self , name_or_class ):
203- # type: (Union[str, Integration]) -> Any
213+ # type: (Union[str, Type[ Integration] ]) -> Any
204214 """Returns the integration for this hub by name or class. If there
205215 is no client bound or the client does not have that integration
206216 then `None` is returned.
@@ -221,14 +231,15 @@ def get_integration(self, name_or_class):
221231 if rv is not None :
222232 return rv
223233
224- initial_client = _initial_client
225- if initial_client is not None :
226- initial_client = initial_client ()
234+ if _initial_client is not None :
235+ initial_client = _initial_client ()
236+ else :
237+ initial_client = None
227238
228239 if (
229240 initial_client is not None
230241 and initial_client is not client
231- and initial_client .integrations .get (name_or_class ) is not None
242+ and initial_client .integrations .get (integration_name ) is not None
232243 ):
233244 warning = (
234245 "Integration %r attempted to run but it was only "
@@ -257,7 +268,7 @@ def bind_client(self, new):
257268 self ._stack [- 1 ] = (new , top [1 ])
258269
259270 def capture_event (self , event , hint = None ):
260- # type: (Event, Hint) -> Optional[str]
271+ # type: (Event, Optional[ Hint] ) -> Optional[str]
261272 """Captures an event. The return value is the ID of the event.
262273
263274 The event is a dictionary following the Sentry v7/v8 protocol
@@ -309,9 +320,10 @@ def capture_exception(self, error=None):
309320 return None
310321
311322 def _capture_internal_exception (self , exc_info ):
323+ # type: (_OptExcInfo) -> Any
312324 """Capture an exception that is likely caused by a bug in the SDK
313325 itself."""
314- logger .error ("Internal error in sentry_sdk" , exc_info = exc_info )
326+ logger .error ("Internal error in sentry_sdk" , exc_info = exc_info ) # type: ignore
315327
316328 def add_breadcrumb (self , crumb = None , hint = None , ** kwargs ):
317329 # type: (Optional[Breadcrumb], Optional[BreadcrumbHint], **Any) -> None
0 commit comments