Skip to content

Commit 8aecc71

Browse files
authored
ref: Remove Hub.current is not None checks (#727)
By construction, Hub.current is never None, such that the expression Hub.current is not None always evaluates to True. This commit simplifies all uses of Hub.current, and in particular chooses to write "return Hub.current.method(...)" for every method, even when the method returns None. The intent is to make it easier to keep the static API matching the Hub behavior. Without this, if a method returns anything other than None the static API would silently drop it, leading to unnecessary debugging time spent trying to identify the culprit. See https://github.com/getsentry/sentry-python/blob/6e378f18919a834d3de50b6f981e332b5094ad83/sentry_sdk/hub.py#L133-L142
1 parent 6e378f1 commit 8aecc71

File tree

2 files changed

+15
-71
lines changed

2 files changed

+15
-71
lines changed

sentry_sdk/api.py

Lines changed: 13 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import inspect
2-
from contextlib import contextmanager
32

43
from sentry_sdk.hub import Hub
54
from 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
189153
def 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
197159
def 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
205165
def 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
213171
def 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
221177
def 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
240192
def 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)

sentry_sdk/integrations/serverless.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _capture_and_reraise():
6969
# type: () -> None
7070
exc_info = sys.exc_info()
7171
hub = Hub.current
72-
if hub is not None and hub.client is not None:
72+
if hub.client is not None:
7373
event, hint = event_from_exception(
7474
exc_info,
7575
client_options=hub.client.options,
@@ -82,6 +82,4 @@ def _capture_and_reraise():
8282

8383
def _flush_client():
8484
# type: () -> None
85-
hub = Hub.current
86-
if hub is not None:
87-
hub.flush()
85+
return Hub.current.flush()

0 commit comments

Comments
 (0)