Skip to content

Commit 0d0a390

Browse files
committed
also implement singleton patton with lazy loading similar to Settings class
1 parent cde0aae commit 0d0a390

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

openml/_api/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22

33

44
def set_api_version(version: str, *, strict: bool = False) -> None:
5-
api_context.set_version(version=version, strict=strict)
6-
7-
8-
api_context = APIContext()
5+
APIContext.get().set_version(version=version, strict=strict)

openml/_api/runtime/core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,29 @@ def build_backend(version: str, *, strict: bool) -> APIBackend:
9494

9595

9696
class APIContext:
97+
"""Context for API version management.
98+
99+
Uses singleton pattern to avoid circular imports. The instance is created
100+
lazily on first access via get(), not at module load time.
101+
"""
102+
103+
_instance: APIContext | None = None
104+
97105
def __init__(self) -> None:
98106
self._backend = build_backend("v1", strict=False)
99107

108+
@classmethod
109+
def get(cls) -> APIContext:
110+
"""Get the singleton instance, creating it on first access."""
111+
if cls._instance is None:
112+
cls._instance = cls()
113+
return cls._instance
114+
115+
@classmethod
116+
def reset(cls) -> None:
117+
"""Reset the singleton instance. Useful for testing."""
118+
cls._instance = None
119+
100120
def set_version(self, version: str, *, strict: bool = False) -> None:
101121
self._backend = build_backend(version=version, strict=strict)
102122

0 commit comments

Comments
 (0)