11import inspect
2- from contextlib import contextmanager
32
43from sentry_sdk .hub import Hub
54from sentry_sdk .scope import Scope
@@ -72,10 +71,7 @@ def capture_event(
7271 ** scope_args # type: Dict[str, Any]
7372):
7473 # type: (...) -> Optional[str]
75- hub = Hub .current
76- if hub is not None :
77- return hub .capture_event (event , hint , scope = scope , ** scope_args )
78- return None
74+ return Hub .current .capture_event (event , hint , scope = scope , ** scope_args )
7975
8076
8177@hubmethod
@@ -86,10 +82,7 @@ def capture_message(
8682 ** scope_args # type: Dict[str, Any]
8783):
8884 # type: (...) -> Optional[str]
89- hub = Hub .current
90- if hub is not None :
91- return hub .capture_message (message , level , scope = scope , ** scope_args )
92- return None
85+ return Hub .current .capture_message (message , level , scope = scope , ** scope_args )
9386
9487
9588@hubmethod
@@ -99,10 +92,7 @@ def capture_exception(
9992 ** scope_args # type: Dict[str, Any]
10093):
10194 # type: (...) -> Optional[str]
102- hub = Hub .current
103- if hub is not None :
104- return hub .capture_exception (error , scope = scope , ** scope_args )
105- return None
95+ return Hub .current .capture_exception (error , scope = scope , ** scope_args )
10696
10797
10898@hubmethod
@@ -112,9 +102,7 @@ def add_breadcrumb(
112102 ** kwargs # type: Any
113103):
114104 # type: (...) -> None
115- hub = Hub .current
116- if hub is not None :
117- return hub .add_breadcrumb (crumb , hint , ** kwargs )
105+ return Hub .current .add_breadcrumb (crumb , hint , ** kwargs )
118106
119107
120108@overload # noqa
@@ -136,19 +124,7 @@ def configure_scope(
136124 callback = None , # type: Optional[Callable[[Scope], None]]
137125):
138126 # type: (...) -> Optional[ContextManager[Scope]]
139- hub = Hub .current
140- if hub is not None :
141- return hub .configure_scope (callback )
142- elif callback is None :
143-
144- @contextmanager
145- def inner ():
146- yield Scope ()
147-
148- return inner ()
149- else :
150- # returned if user provided callback
151- return None
127+ return Hub .current .configure_scope (callback )
152128
153129
154130@overload # noqa
@@ -170,59 +146,37 @@ def push_scope(
170146 callback = None , # type: Optional[Callable[[Scope], None]]
171147):
172148 # type: (...) -> Optional[ContextManager[Scope]]
173- hub = Hub .current
174- if hub is not None :
175- return hub .push_scope (callback )
176- elif callback is None :
177-
178- @contextmanager
179- def inner ():
180- yield Scope ()
181-
182- return inner ()
183- else :
184- # returned if user provided callback
185- return None
149+ return Hub .current .push_scope (callback )
186150
187151
188152@scopemethod # noqa
189153def set_tag (key , value ):
190154 # type: (str, Any) -> None
191- hub = Hub .current
192- if hub is not None :
193- hub .scope .set_tag (key , value )
155+ return Hub .current .scope .set_tag (key , value )
194156
195157
196158@scopemethod # noqa
197159def set_context (key , value ):
198160 # type: (str, Any) -> None
199- hub = Hub .current
200- if hub is not None :
201- hub .scope .set_context (key , value )
161+ return Hub .current .scope .set_context (key , value )
202162
203163
204164@scopemethod # noqa
205165def set_extra (key , value ):
206166 # type: (str, Any) -> None
207- hub = Hub .current
208- if hub is not None :
209- hub .scope .set_extra (key , value )
167+ return Hub .current .scope .set_extra (key , value )
210168
211169
212170@scopemethod # noqa
213171def set_user (value ):
214172 # type: (Dict[str, Any]) -> None
215- hub = Hub .current
216- if hub is not None :
217- hub .scope .set_user (value )
173+ return Hub .current .scope .set_user (value )
218174
219175
220176@scopemethod # noqa
221177def set_level (value ):
222178 # type: (str) -> None
223- hub = Hub .current
224- if hub is not None :
225- hub .scope .set_level (value )
179+ return Hub .current .scope .set_level (value )
226180
227181
228182@hubmethod
@@ -231,18 +185,13 @@ def flush(
231185 callback = None , # type: Optional[Callable[[int, float], None]]
232186):
233187 # type: (...) -> None
234- hub = Hub .current
235- if hub is not None :
236- return hub .flush (timeout = timeout , callback = callback )
188+ return Hub .current .flush (timeout = timeout , callback = callback )
237189
238190
239191@hubmethod
240192def last_event_id ():
241193 # type: () -> Optional[str]
242- hub = Hub .current
243- if hub is not None :
244- return hub .last_event_id ()
245- return None
194+ return Hub .current .last_event_id ()
246195
247196
248197@hubmethod
@@ -251,7 +200,4 @@ def start_span(
251200 ** kwargs # type: Any
252201):
253202 # type: (...) -> Span
254-
255- # TODO: All other functions in this module check for
256- # `Hub.current is None`. That actually should never happen?
257203 return Hub .current .start_span (span = span , ** kwargs )
0 commit comments