Skip to content
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
16 changes: 8 additions & 8 deletions src/async_kernel/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,10 @@ def load_connection_info(self, info: dict[str, Any]) -> None:
self.session.signature_scheme = info["signature_scheme"]

def __new__(cls, settings: dict | None = None, /) -> Self: # noqa: ARG004
# There is only one instance.
if not (instance := cls._instance):
cls._instance = instance = super().__new__(cls)
return instance
# There is only one instance (including subclasses).
if not (instance := Kernel._instance):
Kernel._instance = instance = super().__new__(cls)
return instance # pyright: ignore[reportReturnType]

def __init__(self, settings: dict | None = None, /) -> None:
if self._initialised:
Expand Down Expand Up @@ -473,15 +473,15 @@ def kernel_info(self) -> dict[str, str | dict[str, str | dict[str, str | int]] |
"kernel_name": self.kernel_name,
}

@classmethod
def stop(cls) -> None:
@staticmethod
def stop() -> None:
"""Stop the running kernel.

Once a kernel is stopped; that instance of the kernel cannot be restarted.
Instead, a new kernel must be started.
"""
if instance := cls._instance:
cls._instance = None
if instance := Kernel._instance:
Kernel._instance = None
instance._stop_event.set()

@asynccontextmanager
Expand Down
18 changes: 18 additions & 0 deletions tests/test_kernel_subclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

import async_kernel


async def test_kernel_subclass(anyio_backend):
# Ensure the subclass correctly overrides the kernel.
async_kernel.Kernel.stop()

class MyKernel(async_kernel.Kernel):
pass

async with MyKernel() as kernel:
assert async_kernel.Kernel._instance is kernel # pyright: ignore[reportPrivateUsage]
assert isinstance(kernel, MyKernel)
assert isinstance(async_kernel.Kernel(), MyKernel)
assert isinstance(async_kernel.utils.get_kernel(), MyKernel)
assert not MyKernel._instance # pyright: ignore[reportPrivateUsage]
Loading