Skip to content

Commit f3d069a

Browse files
kesmit13claude
andcommitted
Lazy-load IPython in singlestoredb.utils.events
Defer IPython import and handler registration from module load time to first subscribe() call, preventing IPython from being eagerly imported on every `import singlestoredb`. Also defer _setup_authentication_info_handler() in management/utils.py since it was calling subscribe() at module level. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a7fb74d commit f3d069a

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

singlestoredb/management/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,15 @@ def get_authentication_info(include_env: bool = True) -> Dict[str, Any]:
192192
return get_authentication_info
193193

194194

195-
get_authentication_info = _setup_authentication_info_handler()
195+
_get_authentication_info: Optional[Callable[..., Dict[str, Any]]] = None
196+
197+
198+
def get_authentication_info(include_env: bool = True) -> Dict[str, Any]:
199+
"""Return authentication info, initializing on first call."""
200+
global _get_authentication_info
201+
if _get_authentication_info is None:
202+
_get_authentication_info = _setup_authentication_info_handler()
203+
return _get_authentication_info(include_env=include_env)
196204

197205

198206
def get_token() -> Optional[str]:

singlestoredb/utils/events.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
from typing import Dict
55
from typing import Set
66

7-
try:
8-
from IPython import get_ipython
9-
has_ipython = True
10-
except (ImportError, OSError):
11-
has_ipython = False
12-
137

148
_subscribers: Set[Callable[[Dict[str, Any]], None]] = set()
9+
_initialized = False
10+
11+
12+
def _ensure_initialized() -> None:
13+
"""Lazily detect IPython and register the control handler."""
14+
global _initialized
15+
if _initialized:
16+
return
17+
_initialized = True
18+
19+
try:
20+
from IPython import get_ipython
21+
_handlers = get_ipython().kernel.control_handlers
22+
_handlers['singlestore_portal_request'] = _event_handler
23+
except (ImportError, OSError, AttributeError):
24+
pass
1525

1626

1727
def subscribe(func: Callable[[Dict[str, Any]], None]) -> None:
@@ -24,6 +34,7 @@ def subscribe(func: Callable[[Dict[str, Any]], None]) -> None:
2434
The function to call when an event is received
2535
2636
"""
37+
_ensure_initialized()
2738
_subscribers.add(func)
2839

2940

@@ -54,12 +65,3 @@ def _event_handler(stream: Any, ident: Any, msg: Dict[str, Any]) -> None:
5465

5566
for func in _subscribers:
5667
func(content)
57-
58-
59-
# Inject a control handler to receive SingleStore events
60-
if has_ipython:
61-
try:
62-
_handlers = get_ipython().kernel.control_handlers
63-
_handlers['singlestore_portal_request'] = _event_handler
64-
except AttributeError:
65-
pass

0 commit comments

Comments
 (0)