Skip to content

ref: Remove Hub.current is not None checks #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 13 additions & 67 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import inspect
from contextlib import contextmanager

from sentry_sdk.hub import Hub
from sentry_sdk.scope import Scope
Expand Down Expand Up @@ -72,10 +71,7 @@ def capture_event(
**scope_args # type: Dict[str, Any]
):
# type: (...) -> Optional[str]
hub = Hub.current
if hub is not None:
return hub.capture_event(event, hint, scope=scope, **scope_args)
return None
return Hub.current.capture_event(event, hint, scope=scope, **scope_args)


@hubmethod
Expand All @@ -86,10 +82,7 @@ def capture_message(
**scope_args # type: Dict[str, Any]
):
# type: (...) -> Optional[str]
hub = Hub.current
if hub is not None:
return hub.capture_message(message, level, scope=scope, **scope_args)
return None
return Hub.current.capture_message(message, level, scope=scope, **scope_args)


@hubmethod
Expand All @@ -99,10 +92,7 @@ def capture_exception(
**scope_args # type: Dict[str, Any]
):
# type: (...) -> Optional[str]
hub = Hub.current
if hub is not None:
return hub.capture_exception(error, scope=scope, **scope_args)
return None
return Hub.current.capture_exception(error, scope=scope, **scope_args)


@hubmethod
Expand All @@ -112,9 +102,7 @@ def add_breadcrumb(
**kwargs # type: Any
):
# type: (...) -> None
hub = Hub.current
if hub is not None:
return hub.add_breadcrumb(crumb, hint, **kwargs)
return Hub.current.add_breadcrumb(crumb, hint, **kwargs)


@overload # noqa
Expand All @@ -136,19 +124,7 @@ def configure_scope(
callback=None, # type: Optional[Callable[[Scope], None]]
):
# type: (...) -> Optional[ContextManager[Scope]]
hub = Hub.current
if hub is not None:
return hub.configure_scope(callback)
elif callback is None:

@contextmanager
def inner():
yield Scope()

return inner()
else:
# returned if user provided callback
return None
return Hub.current.configure_scope(callback)


@overload # noqa
Expand All @@ -170,59 +146,37 @@ def push_scope(
callback=None, # type: Optional[Callable[[Scope], None]]
):
# type: (...) -> Optional[ContextManager[Scope]]
hub = Hub.current
if hub is not None:
return hub.push_scope(callback)
elif callback is None:

@contextmanager
def inner():
yield Scope()

return inner()
else:
# returned if user provided callback
return None
return Hub.current.push_scope(callback)


@scopemethod # noqa
def set_tag(key, value):
# type: (str, Any) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_tag(key, value)
return Hub.current.scope.set_tag(key, value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in the PR description / commit message, I intentionally decided to always use the pattern return Hub.current.method(...), even when the return value is None. The idea behind it is to avoid accidentally returning None when the Hub method returns something else (e.g. as part of a behavior change). In other words, I find this easier to maintain than to selectively use or not the return keyword.



@scopemethod # noqa
def set_context(key, value):
# type: (str, Any) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_context(key, value)
return Hub.current.scope.set_context(key, value)


@scopemethod # noqa
def set_extra(key, value):
# type: (str, Any) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_extra(key, value)
return Hub.current.scope.set_extra(key, value)


@scopemethod # noqa
def set_user(value):
# type: (Dict[str, Any]) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_user(value)
return Hub.current.scope.set_user(value)


@scopemethod # noqa
def set_level(value):
# type: (str) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_level(value)
return Hub.current.scope.set_level(value)


@hubmethod
Expand All @@ -231,18 +185,13 @@ def flush(
callback=None, # type: Optional[Callable[[int, float], None]]
):
# type: (...) -> None
hub = Hub.current
if hub is not None:
return hub.flush(timeout=timeout, callback=callback)
return Hub.current.flush(timeout=timeout, callback=callback)


@hubmethod
def last_event_id():
# type: () -> Optional[str]
hub = Hub.current
if hub is not None:
return hub.last_event_id()
return None
return Hub.current.last_event_id()


@hubmethod
Expand All @@ -251,7 +200,4 @@ def start_span(
**kwargs # type: Any
):
# type: (...) -> Span

# TODO: All other functions in this module check for
# `Hub.current is None`. That actually should never happen?
return Hub.current.start_span(span=span, **kwargs)
6 changes: 2 additions & 4 deletions sentry_sdk/integrations/serverless.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def _capture_and_reraise():
# type: () -> None
exc_info = sys.exc_info()
hub = Hub.current
if hub is not None and hub.client is not None:
if hub.client is not None:
event, hint = event_from_exception(
exc_info,
client_options=hub.client.options,
Expand All @@ -82,6 +82,4 @@ def _capture_and_reraise():

def _flush_client():
# type: () -> None
hub = Hub.current
if hub is not None:
hub.flush()
return Hub.current.flush()