Skip to content

Commit 2ea7e1d

Browse files
authored
Improve output from list/describe actions on indexes and collections (#387)
## Problem When running in notebooks, output from the `list_indexes` and `list_collections` commands created confusion because the `__repr__` representation for these objects showed an object with a top-level key that implied the response should be interacted with like a dictionary. This expectation contradicts with the way the `__iter__` implementations on these results objects are set up to enable iterating over results without drilling down. The origin of the complexity here is that the backing APIs used to return simple arrays of names that could be easily iterated over, and earlier this year they become more fleshed out responses. Returning more data is useful in some situations, but in trying to smooth out the impact of the API change and maintain a similar way of interacting with the results, we accidentally opened up this inconsistency in the experience. ## Solution - For these actions, migrate from output by `pprint.pformat` to `json.dumps`. For deeply nested objects, this produces a result that is easier to read. - Stop sorting keys alphabetically. The way they are returned from the API makes the most sense (name first). - Remove top-level keys from the printed output, which created wrong expectations about how to interact with the results object. Now list_indexes looks like an array, and you index into it like an array. Ditto for collections. ### Before <img alt="Screenshot 2024-08-28 at 1 38 27 PM" src="https://github.com/user-attachments/assets/a7931ebe-ffd9-4197-be26-aa030592e62d"> ![Collections output](https://github.com/user-attachments/assets/466d7a75-69c6-41d9-b75c-bbcc797ef315) ### After <img width="706" alt="Screenshot 2024-08-28 at 1 29 00 PM" src="https://github.com/user-attachments/assets/5c47e1db-19d3-44e7-bed9-bed27f823d02"> ![Screenshot 2024-08-28 at 1 42 05 PM](https://github.com/user-attachments/assets/fb8eee8e-6b7c-442e-878e-ddaad635d317) ![Screenshot 2024-08-28 at 3 28 44 PM](https://github.com/user-attachments/assets/e33b1858-0718-4e36-8e7d-af41b6457784) ## Type of Change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update - [ ] Infrastructure change (CI configs, etc) - [ ] Non-code change (docs, etc) - [X] None of the above: UX improvement, but should be no functional change
1 parent 1d0b686 commit 2ea7e1d

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

pinecone/control/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
from .pinecone import Pinecone
2+
3+
from .repr_overrides import install_repr_overrides
4+
5+
install_repr_overrides()

pinecone/control/repr_overrides.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pinecone.models.index_model import IndexModel
2+
from pinecone.core.openapi.control.models import CollectionModel
3+
4+
import json
5+
6+
7+
def install_repr_overrides():
8+
"""
9+
The generator code uses pprint.pformat to format the repr output
10+
which looks really poor when printing a list of large objects
11+
in a notebook setting. We override it here for a few select models
12+
instead of modifying the generator code because the more compact output
13+
from pprint.pformat seems better for data plane objects such as lists of
14+
query results.
15+
"""
16+
for model in [IndexModel, CollectionModel]:
17+
model.__repr__ = lambda self: json.dumps(self.to_dict(), indent=4, sort_keys=False)

pinecone/models/collection_list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from pinecone.core.openapi.control.models import (
23
CollectionList as OpenAPICollectionList,
34
)
@@ -28,7 +29,7 @@ def __str__(self):
2829
return str(self.collection_list)
2930

3031
def __repr__(self):
31-
return repr(self.collection_list)
32+
return json.dumps([c.to_dict() for c in self.collection_list.collections], indent=4)
3233

3334
def __getattr__(self, attr):
3435
return getattr(self.collection_list, attr)

pinecone/models/index_list.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from pinecone.core.openapi.control.models import IndexList as OpenAPIIndexList
23
from .index_model import IndexModel
34

@@ -21,10 +22,10 @@ def __iter__(self):
2122
return iter(self.indexes)
2223

2324
def __str__(self):
24-
return str(self.index_list)
25+
return str(self.indexes)
2526

2627
def __repr__(self):
27-
return repr(self.index_list)
28+
return json.dumps([i.to_dict() for i in self.indexes], indent=4)
2829

2930
def __getattr__(self, attr):
3031
return getattr(self.index_list, attr)

pinecone/models/index_model.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ def __init__(self, index: OpenAPIIndexModel):
99
def __str__(self):
1010
return str(self.index)
1111

12-
def __repr__(self):
13-
return repr(self.index)
14-
1512
def __getattr__(self, attr):
1613
return getattr(self.index, attr)
1714

1815
def __getitem__(self, key):
1916
return self.__getattr__(key)
17+
18+
def to_dict(self):
19+
return self.index.to_dict()

0 commit comments

Comments
 (0)