Skip to content

Commit

Permalink
Add missing .names() for list_collections() (#278)
Browse files Browse the repository at this point in the history
## Problem

The `list_indexes()` response has a convenience method that lets you
easily get index names by chaining `.names()`. We want the same
ergonomics on `list_collections()`.

## Solution

Add test and adjust model for implementation. Now you should be able to
do:

```python
if index_name not in pc.list_indexes.names():
  # this already was working

if collection_name not in pc.list_collections.names():
  # this is what got added in this diff
```

## Type of Change

- [x] New feature (non-breaking change which adds functionality)
  • Loading branch information
jhamon committed Jan 14, 2024
1 parent e296d22 commit 965631a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pinecone/models/collection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ def __init__(self, collection_list: OpenAPICollectionList):
self.collection_list = collection_list
self.current = 0

def names(self):
return [i['name'] for i in self.collection_list.collections]

def __getitem__(self, key):
return self.collection_list.collections[key]

Expand Down
9 changes: 8 additions & 1 deletion tests/unit/models/test_collection_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ def test_collection_list_getitem(self, collection_list_response):

def test_collection_list_proxies_methods(self, collection_list_response):
# Forward compatibility, in case we add more attributes to IndexList for pagination
assert CollectionList(collection_list_response).collection_list.collections == collection_list_response.collections
assert CollectionList(collection_list_response).collection_list.collections == collection_list_response.collections

def test_when_results_are_empty(self):
assert len(CollectionList(OpenApiCollectionList(collections=[]))) == 0

def test_collection_list_names_syntactic_sugar(self, collection_list_response):
icl = CollectionList(collection_list_response)
assert icl.names() == ['collection1', 'collection2']

0 comments on commit 965631a

Please sign in to comment.