Skip to content

Commit eef298a

Browse files
committed
ref: Remove Hub.current is not None checks
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.
1 parent be9bfa7 commit eef298a

File tree

2 files changed

+15
-70
lines changed

2 files changed

+15
-70
lines changed

sentry_sdk/api.py

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ def capture_event(
7272
**scope_args # type: Dict[str, Any]
7373
):
7474
# 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
75+
return Hub.current.capture_event(event, hint, scope=scope, **scope_args)
7976

8077

8178
@hubmethod
@@ -86,10 +83,7 @@ def capture_message(
8683
**scope_args # type: Dict[str, Any]
8784
):
8885
# 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
86+
return Hub.current.capture_message(message, level, scope=scope, **scope_args)
9387

9488

9589
@hubmethod
@@ -99,10 +93,7 @@ def capture_exception(
9993
**scope_args # type: Dict[str, Any]
10094
):
10195
# 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
96+
return Hub.current.capture_exception(error, scope=scope, **scope_args)
10697

10798

10899
@hubmethod
@@ -112,9 +103,7 @@ def add_breadcrumb(
112103
**kwargs # type: Any
113104
):
114105
# type: (...) -> None
115-
hub = Hub.current
116-
if hub is not None:
117-
return hub.add_breadcrumb(crumb, hint, **kwargs)
106+
return Hub.current.add_breadcrumb(crumb, hint, **kwargs)
118107

119108

120109
@overload # noqa
@@ -136,19 +125,7 @@ def configure_scope(
136125
callback=None, # type: Optional[Callable[[Scope], None]]
137126
):
138127
# 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
128+
return Hub.current.configure_scope(callback)
152129

153130

154131
@overload # noqa
@@ -170,59 +147,37 @@ def push_scope(
170147
callback=None, # type: Optional[Callable[[Scope], None]]
171148
):
172149
# 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
150+
return Hub.current.push_scope(callback)
186151

187152

188153
@scopemethod # noqa
189154
def set_tag(key, value):
190155
# type: (str, Any) -> None
191-
hub = Hub.current
192-
if hub is not None:
193-
hub.scope.set_tag(key, value)
156+
return Hub.current.scope.set_tag(key, value)
194157

195158

196159
@scopemethod # noqa
197160
def set_context(key, value):
198161
# type: (str, Any) -> None
199-
hub = Hub.current
200-
if hub is not None:
201-
hub.scope.set_context(key, value)
162+
return Hub.current.scope.set_context(key, value)
202163

203164

204165
@scopemethod # noqa
205166
def set_extra(key, value):
206167
# type: (str, Any) -> None
207-
hub = Hub.current
208-
if hub is not None:
209-
hub.scope.set_extra(key, value)
168+
return Hub.current.scope.set_extra(key, value)
210169

211170

212171
@scopemethod # noqa
213172
def set_user(value):
214173
# type: (Dict[str, Any]) -> None
215-
hub = Hub.current
216-
if hub is not None:
217-
hub.scope.set_user(value)
174+
return Hub.current.scope.set_user(value)
218175

219176

220177
@scopemethod # noqa
221178
def set_level(value):
222179
# type: (str) -> None
223-
hub = Hub.current
224-
if hub is not None:
225-
hub.scope.set_level(value)
180+
return Hub.current.scope.set_level(value)
226181

227182

228183
@hubmethod
@@ -231,18 +186,13 @@ def flush(
231186
callback=None, # type: Optional[Callable[[int, float], None]]
232187
):
233188
# type: (...) -> None
234-
hub = Hub.current
235-
if hub is not None:
236-
return hub.flush(timeout=timeout, callback=callback)
189+
return Hub.current.flush(timeout=timeout, callback=callback)
237190

238191

239192
@hubmethod
240193
def last_event_id():
241194
# type: () -> Optional[str]
242-
hub = Hub.current
243-
if hub is not None:
244-
return hub.last_event_id()
245-
return None
195+
return Hub.current.last_event_id()
246196

247197

248198
@hubmethod
@@ -251,7 +201,4 @@ def start_span(
251201
**kwargs # type: Any
252202
):
253203
# type: (...) -> Span
254-
255-
# TODO: All other functions in this module check for
256-
# `Hub.current is None`. That actually should never happen?
257204
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)