Skip to content

Commit dcee881

Browse files
ref(init): Move sentry_sdk.init out of hub.py
Now that the `Hub`-based API is deprecated, `sentry_sdk.init` should no longer be in `hub.py`. Since it is kind of its own thing, it makes sense to implement `init` in its own file. Closes #3233
1 parent afa88eb commit dcee881

File tree

3 files changed

+64
-59
lines changed

3 files changed

+64
-59
lines changed

sentry_sdk/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from sentry_sdk.hub import Hub, init
1+
from sentry_sdk._init_implementation import init
2+
from sentry_sdk.hub import Hub
23
from sentry_sdk.scope import Scope
34
from sentry_sdk.transport import Transport, HttpTransport
45
from sentry_sdk.client import Client

sentry_sdk/_init_implementation.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import TYPE_CHECKING
2+
3+
import sentry_sdk
4+
5+
if TYPE_CHECKING:
6+
from typing import Any, ContextManager, Optional
7+
from sentry_sdk.consts import ClientConstructor
8+
9+
10+
class _InitGuard:
11+
def __init__(self, client):
12+
# type: (sentry_sdk.Client) -> None
13+
self._client = client
14+
15+
def __enter__(self):
16+
# type: () -> _InitGuard
17+
return self
18+
19+
def __exit__(self, exc_type, exc_value, tb):
20+
# type: (Any, Any, Any) -> None
21+
c = self._client
22+
if c is not None:
23+
c.close()
24+
25+
26+
def _check_python_deprecations():
27+
# type: () -> None
28+
# Since we're likely to deprecate Python versions in the future, I'm keeping
29+
# this handy function around. Use this to detect the Python version used and
30+
# to output logger.warning()s if it's deprecated.
31+
pass
32+
33+
34+
def _init(*args, **kwargs):
35+
# type: (*Optional[str], **Any) -> ContextManager[Any]
36+
"""Initializes the SDK and optionally integrations.
37+
38+
This takes the same arguments as the client constructor.
39+
"""
40+
client = sentry_sdk.Client(*args, **kwargs) # type: ignore
41+
sentry_sdk.Scope.get_global_scope().set_client(client)
42+
_check_python_deprecations()
43+
rv = _InitGuard(client)
44+
return rv
45+
46+
47+
if TYPE_CHECKING:
48+
# Make mypy, PyCharm and other static analyzers think `init` is a type to
49+
# have nicer autocompletion for params.
50+
#
51+
# Use `ClientConstructor` to define the argument types of `init` and
52+
# `ContextManager[Any]` to tell static analyzers about the return type.
53+
54+
class init(ClientConstructor, _InitGuard): # noqa: N801
55+
pass
56+
57+
else:
58+
# Alias `init` for actual usage. Go through the lambda indirection to throw
59+
# PyCharm off of the weakly typed signature (it would otherwise discover
60+
# both the weakly typed signature of `_init` and our faked `init` type).
61+
62+
init = (lambda: _init)()

sentry_sdk/hub.py

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
LogLevelStr,
4545
SamplingContext,
4646
)
47-
from sentry_sdk.consts import ClientConstructor
4847
from sentry_sdk.tracing import TransactionKwargs
4948

5049
T = TypeVar("T")
@@ -69,63 +68,6 @@ def _should_send_default_pii():
6968
return client.should_send_default_pii()
7069

7170

72-
class _InitGuard:
73-
def __init__(self, client):
74-
# type: (Client) -> None
75-
self._client = client
76-
77-
def __enter__(self):
78-
# type: () -> _InitGuard
79-
return self
80-
81-
def __exit__(self, exc_type, exc_value, tb):
82-
# type: (Any, Any, Any) -> None
83-
c = self._client
84-
if c is not None:
85-
c.close()
86-
87-
88-
def _check_python_deprecations():
89-
# type: () -> None
90-
# Since we're likely to deprecate Python versions in the future, I'm keeping
91-
# this handy function around. Use this to detect the Python version used and
92-
# to output logger.warning()s if it's deprecated.
93-
pass
94-
95-
96-
def _init(*args, **kwargs):
97-
# type: (*Optional[str], **Any) -> ContextManager[Any]
98-
"""Initializes the SDK and optionally integrations.
99-
100-
This takes the same arguments as the client constructor.
101-
"""
102-
client = Client(*args, **kwargs) # type: ignore
103-
Scope.get_global_scope().set_client(client)
104-
_check_python_deprecations()
105-
rv = _InitGuard(client)
106-
return rv
107-
108-
109-
from sentry_sdk._types import TYPE_CHECKING
110-
111-
if TYPE_CHECKING:
112-
# Make mypy, PyCharm and other static analyzers think `init` is a type to
113-
# have nicer autocompletion for params.
114-
#
115-
# Use `ClientConstructor` to define the argument types of `init` and
116-
# `ContextManager[Any]` to tell static analyzers about the return type.
117-
118-
class init(ClientConstructor, _InitGuard): # noqa: N801
119-
pass
120-
121-
else:
122-
# Alias `init` for actual usage. Go through the lambda indirection to throw
123-
# PyCharm off of the weakly typed signature (it would otherwise discover
124-
# both the weakly typed signature of `_init` and our faked `init` type).
125-
126-
init = (lambda: _init)()
127-
128-
12971
class HubMeta(type):
13072
@property
13173
def current(cls):

0 commit comments

Comments
 (0)