Skip to content

Commit e770c5a

Browse files
authored
Improve AttributeError warnings when using deprecated methods (#291)
## Problem In v3.0, many methods have moved from the top-level namespace onto a class instance. We can throw more informative errors to help people migrate. ## Solution Add back stub implementations of methods that were removed. When called, they throw errors with useful documentation. I simplified the expected args for all methods to `*args, **kwargs` which should match no matter what combination of arguments the user has provided. ## Type of Change - [x] New feature (non-breaking change which adds functionality) ## Test Plan `poetry run python3` and then try it: ``` poetry run python3 Python 3.9.16 (main, May 2 2023, 18:16:09) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import pinecone >>> pinecone.list_indexes() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/jhamon/workspace/pinecone-python-client/pinecone/deprecation_warnings.py", line 21, in list_indexes raise AttributeError(_build_class_migration_message('list_indexes', example)) AttributeError: list_indexes is no longer top-level attribute of the pinecone package. To use list_indexes, please create a client instance and call the method there instead. Example: from pinecone import Pinecone pc = Pinecone(api_key='YOUR_API_KEY') index_name = "quickstart" # or your index name if index_name not in pc.list_indexes().names(): # do something >>> pinecone.create_index('quickstart', dimension=8, metric='cosine') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/jhamon/workspace/pinecone-python-client/pinecone/deprecation_warnings.py", line 48, in create_index raise AttributeError(_build_class_migration_message('create_index', example)) AttributeError: create_index is no longer top-level attribute of the pinecone package. To use create_index, please create a client instance and call the method there instead. Example: from pinecone import Pinecone, ServerlessSpec pc = Pinecone(api_key='YOUR_API_KEY') pc.create_index( name='my-index', dimension=1536, metric='euclidean', spec=ServerlessSpec( cloud='aws', region='us-west-2' ) ) >>> pinecone.describe_index('asdf') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/jhamon/workspace/pinecone-python-client/pinecone/deprecation_warnings.py", line 30, in describe_index raise AttributeError(_build_class_migration_message('describe_index', example)) AttributeError: describe_index is no longer top-level attribute of the pinecone package. To use describe_index, please create a client instance and call the method there instead. Example: from pinecone import Pinecone pc = Pinecone(api_key='YOUR_API_KEY') pc.describe_index('my_index') ``` And `pinecone.init()` ``` >>> pinecone.init() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/jhamon/workspace/pinecone-python-client/pinecone/deprecation_warnings.py", line 38, in init raise AttributeError(msg) AttributeError: init is no longer a top-level attribute of the pinecone package. Please create an instance of the Pinecone class instead. Example: import os from pinecone import Pinecone, ServerlessSpec pc = Pinecone( api_key=os.environ.get("PINECONE_API_KEY") ) # Now do stuff if 'my_index' not in pc.list_indexes().names(): pc.create_index( name='my_index', dimension=1536, metric='euclidean', spec=ServerlessSpec( cloud='aws', region='us-west-2' ) ) ```
1 parent c84ea97 commit e770c5a

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

pinecone/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
warnings.filterwarnings("ignore", category=TqdmExperimentalWarning)
88

9+
from .deprecation_warnings import *
910
from .config import *
1011
from .exceptions import *
1112
from .control import *

pinecone/deprecation_warnings.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
def _build_class_migration_message(method_name: str, example: str):
2+
return f"""{method_name} is no longer a top-level attribute of the pinecone package.
3+
4+
To use {method_name}, please create a client instance and call the method there instead.
5+
6+
Example:
7+
{example}
8+
"""
9+
10+
def init(*args, **kwargs):
11+
example = """
12+
import os
13+
from pinecone import Pinecone, ServerlessSpec
14+
15+
pc = Pinecone(
16+
api_key=os.environ.get("PINECONE_API_KEY")
17+
)
18+
19+
# Now do stuff
20+
if 'my_index' not in pc.list_indexes().names():
21+
pc.create_index(
22+
name='my_index',
23+
dimension=1536,
24+
metric='euclidean',
25+
spec=ServerlessSpec(
26+
cloud='aws',
27+
region='us-west-2'
28+
)
29+
)
30+
"""
31+
msg = f"""init is no longer a top-level attribute of the pinecone package.
32+
33+
Please create an instance of the Pinecone class instead.
34+
35+
Example:
36+
{example}
37+
"""
38+
raise AttributeError(msg)
39+
40+
def list_indexes(*args, **kwargs):
41+
example = """
42+
from pinecone import Pinecone
43+
44+
pc = Pinecone(api_key='YOUR_API_KEY')
45+
46+
index_name = "quickstart" # or your index name
47+
48+
if index_name not in pc.list_indexes().names():
49+
# do something
50+
"""
51+
raise AttributeError(_build_class_migration_message('list_indexes', example))
52+
53+
def describe_index(*args, **kwargs):
54+
example = """
55+
from pinecone import Pinecone
56+
57+
pc = Pinecone(api_key='YOUR_API_KEY')
58+
pc.describe_index('my_index')
59+
"""
60+
raise AttributeError(_build_class_migration_message('describe_index', example))
61+
62+
63+
def create_index(*args, **kwargs):
64+
example = """
65+
from pinecone import Pinecone, ServerlessSpec
66+
67+
pc = Pinecone(api_key='YOUR_API_KEY')
68+
pc.create_index(
69+
name='my-index',
70+
dimension=1536,
71+
metric='euclidean',
72+
spec=ServerlessSpec(
73+
cloud='aws',
74+
region='us-west-2'
75+
)
76+
)
77+
"""
78+
raise AttributeError(_build_class_migration_message('create_index', example))
79+
80+
def delete_index(*args, **kwargs):
81+
example = """
82+
from pinecone import Pinecone
83+
84+
pc = Pinecone(api_key='YOUR_API_KEY')
85+
pc.delete_index('my_index')
86+
"""
87+
raise AttributeError(_build_class_migration_message('delete_index', example))
88+
89+
def scale_index(*args, **kwargs):
90+
example = """
91+
from pinecone import Pinecone
92+
93+
pc = Pinecone(api_key='YOUR_API_KEY')
94+
pc.configure_index('my_index', replicas=2)
95+
"""
96+
97+
msg = f"""scale_index is no longer a top-level attribute of the pinecone package.
98+
99+
Please create a client instance and call the configure_index method instead.
100+
101+
Example:
102+
{example}
103+
"""
104+
raise AttributeError(msg)
105+
106+
107+
def create_collection(*args, **kwargs):
108+
example = """
109+
from pinecone import Pinecone
110+
111+
pc = Pinecone(api_key='YOUR_API_KEY')
112+
pc.create_collection(name='my_collection', source='my_index')
113+
"""
114+
raise AttributeError(_build_class_migration_message('create_collection', example))
115+
116+
def list_collections(*args, **kwargs):
117+
example = """
118+
from pinecone import Pinecone
119+
120+
pc = Pinecone(api_key='YOUR_API_KEY')
121+
pc.list_collections()
122+
"""
123+
raise AttributeError(_build_class_migration_message('list_collections', example))
124+
125+
def delete_collection(*args, **kwargs):
126+
example = """
127+
from pinecone import Pinecone
128+
129+
pc = Pinecone(api_key='YOUR_API_KEY')
130+
pc.delete_collection('my_collection')
131+
"""
132+
raise AttributeError(_build_class_migration_message('delete_collection', example))
133+
134+
def describe_collection(*args, **kwargs):
135+
example = """
136+
from pinecone import Pinecone
137+
138+
pc = Pinecone(api_key='YOUR_API_KEY')
139+
pc.describe_collection('my_collection')
140+
"""
141+
raise AttributeError(_build_class_migration_message('describe_collection', example))
142+
143+
144+
def configure_index(*args, **kwargs):
145+
example = """
146+
from pinecone import Pinecone
147+
148+
pc = Pinecone(api_key='YOUR_API_KEY')
149+
pc.configure_index('my_index', replicas=2)
150+
"""
151+
raise AttributeError(_build_class_migration_message('configure_index', example))

0 commit comments

Comments
 (0)