mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-04-20 08:20:02 +02:00
Merge pull request #191 from tiqi-group/fix/client_context_manager
Fix: client context manager
This commit is contained in:
commit
36ab8ab68b
@ -3,6 +3,7 @@ import logging
|
||||
import sys
|
||||
import threading
|
||||
import urllib.parse
|
||||
from types import TracebackType
|
||||
from typing import TYPE_CHECKING, Any, TypedDict, cast
|
||||
|
||||
import socketio # type: ignore
|
||||
@ -109,10 +110,14 @@ class Client:
|
||||
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:
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user