Elffuss BraidHub

kikocis/lancedb public

chore: make remote client a context manager (#1648)

Allow `RemoteLanceDBClient` to be used as context manager
Changeset7f4901a34f5bd5c63191da0f755097ff24aae0bbb18338acf88af9bf677c6f4a
AuthorLei Xu <lei@lancedb.com> · 2024-09-13 22:08 -07:00
CommitterGitHub <noreply@github.com> · 2024-09-13 22:08 -07:00
Parents313b0430812e

Browse the files at this changeset

2 files changed

modified python/python/lancedb/remote/client.py +7 −0
@@ -79,6 +79,13 @@
7979 or f"https://{self.db_name}.{self.region}.api.lancedb.com"
8080 )
8181
82+ def __enter__(self):
83+ return self
84+
85+ def __exit__(self, exc_type, exc_value, traceback):
86+ self.close()
87+ return False # Do not suppress exceptions
88+
8289 def close(self):
8390 self.session.close()
8491 self.closed = True
modified python/python/tests/test_remote_client.py +16 −14
@@ -74,21 +74,23 @@
7474 await mock_server.start()
7575
7676 try:
77- client = RestfulLanceDBClient("lancedb+http://localhost:8111")
78- df = (
79- await client.query(
80- "test_table",
81- VectorQuery(
82- vector=np.random.rand(128).tolist(),
83- k=10,
84- _metric="L2",
85- columns=["id", "vector"],
86- ),
87- )
88- ).to_pandas()
77+ with RestfulLanceDBClient("lancedb+http://localhost:8111") as client:
78+ df = (
79+ await client.query(
80+ "test_table",
81+ VectorQuery(
82+ vector=np.random.rand(128).tolist(),
83+ k=10,
84+ _metric="L2",
85+ columns=["id", "vector"],
86+ ),
87+ )
88+ ).to_pandas()
8989
90- assert "vector" in df.columns
91- assert "id" in df.columns
90+ assert "vector" in df.columns
91+ assert "id" in df.columns
92+
93+ assert client.closed
9294 finally:
9395 # make sure we don't leak resources
9496 await mock_server.stop()