Skip to content

Commit

Permalink
Merge pull request #191 from tiqi-group/fix/client_context_manager
Browse files Browse the repository at this point in the history
Fix: client context manager
  • Loading branch information
mosmuell authored Dec 2, 2024
2 parents 7b786be + 27a832b commit 36ab8ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/pydase/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import threading
import urllib.parse
from types import TracebackType
from typing import TYPE_CHECKING, Any, TypedDict, cast

import socketio # type: ignore
Expand Down Expand Up @@ -109,10 +110,14 @@ def __init__(
self.connect(block_until_connected=block_until_connected)

def __enter__(self) -> Self:
self.connect(block_until_connected=True)
return self

def __del__(self) -> None:
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
self.disconnect()

def connect(self, block_until_connected: bool = True) -> None:
Expand Down
12 changes: 12 additions & 0 deletions tests/client/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,15 @@ def test_tab_completion(pydase_client: pydase.Client) -> None:
"sub_service",
]
)


def test_context_manager(pydase_client: pydase.Client) -> None:
client = pydase.Client(url="ws://localhost:9999")

assert client.proxy.connected

with client:
client.proxy.my_property = 1337.01
assert client.proxy.my_property == 1337.01

assert not client.proxy.connected

0 comments on commit 36ab8ab

Please sign in to comment.