fix: the bug of async connection context manager (#1333)

- add `return` for `__enter__`

The buggy code didn't return the object, therefore it will always return
None within a context manager:

```python
with await lancedb.connect_async("./.lancedb") as db:
        # db is always None
```

(BTW, why not to design an async context manager?)

- add a unit test for Async connection context manager

- update return type of `AsyncConnection.open_table` to `AsyncTable`

Although type annotation doesn't affect the functionality, it is helpful
for IDEs.
This commit is contained in:
zhongpu
2024-05-30 00:33:32 +08:00
committed by GitHub
parent 2f4b70ecfe
commit 3bb7c546d7
2 changed files with 9 additions and 2 deletions

View File

@@ -509,7 +509,7 @@ class AsyncConnection(object):
return self._inner.__repr__()
def __enter__(self):
self
return self
def __exit__(self, *_):
self.close()
@@ -779,7 +779,7 @@ class AsyncConnection(object):
name: str,
storage_options: Optional[Dict[str, str]] = None,
index_cache_size: Optional[int] = None,
) -> Table:
) -> AsyncTable:
"""Open a Lance Table in the database.
Parameters

View File

@@ -296,6 +296,13 @@ async def test_close(tmp_path):
await db.table_names()
@pytest.mark.asyncio
async def test_context_manager(tmp_path):
with await lancedb.connect_async(tmp_path) as db:
assert db.is_open()
assert not db.is_open()
@pytest.mark.asyncio
async def test_create_mode_async(tmp_path):
db = await lancedb.connect_async(tmp_path)