Skip to content

Commit 1ee620a

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 06d5da1 commit 1ee620a

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")
@@ -59,63 +58,6 @@ def overload(x):
5958
_local = ContextVar("sentry_current_hub")
6059

6160

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

0 commit comments

Comments
 (0)