Skip to content

Commit 9074caa

Browse files
Daniel MeloThyrst
Daniel Melo
authored andcommitted
use Mapping instead of Dict, Sequence instead of List
1 parent 17b33e7 commit 9074caa

File tree

3 files changed

+47
-47
lines changed

3 files changed

+47
-47
lines changed

pinecone/core/client/api_client.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ def __call_api(
119119
self,
120120
resource_path: str,
121121
method: str,
122-
path_params: typing.Optional[typing.Dict[str, typing.Any]] = None,
123-
query_params: typing.Optional[typing.List[typing.Tuple[str, typing.Any]]] = None,
124-
header_params: typing.Optional[typing.Dict[str, typing.Any]] = None,
122+
path_params: typing.Optional[typing.Mapping[str, typing.Any]] = None,
123+
query_params: typing.Optional[typing.Sequence[typing.Tuple[str, typing.Any]]] = None,
124+
header_params: typing.Optional[typing.Mapping[str, typing.Any]] = None,
125125
body: typing.Optional[typing.Any] = None,
126-
post_params: typing.Optional[typing.List[typing.Tuple[str, typing.Any]]] = None,
127-
files: typing.Optional[typing.Dict[str, typing.List[io.IOBase]]] = None,
126+
post_params: typing.Optional[typing.Sequence[typing.Tuple[str, typing.Any]]] = None,
127+
files: typing.Optional[typing.Mapping[str, typing.Sequence[io.IOBase]]] = None,
128128
response_type: typing.Optional[typing.Tuple[typing.Any]] = None,
129-
auth_settings: typing.Optional[typing.List[str]] = None,
129+
auth_settings: typing.Optional[typing.Sequence[str]] = None,
130130
_return_http_data_only: typing.Optional[bool] = None,
131-
collection_formats: typing.Optional[typing.Dict[str, str]] = None,
131+
collection_formats: typing.Optional[typing.Mapping[str, str]] = None,
132132
_preload_content: bool = True,
133133
_request_timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
134134
_host: typing.Optional[str] = None,
@@ -281,9 +281,9 @@ def sanitize_for_serialization(cls, obj):
281281
return obj.isoformat()
282282
elif isinstance(obj, ModelSimple):
283283
return cls.sanitize_for_serialization(obj.value)
284-
elif isinstance(obj, (list, tuple)):
284+
elif isinstance(obj, typing.Sequence):
285285
return [cls.sanitize_for_serialization(item) for item in obj]
286-
if isinstance(obj, dict):
286+
if isinstance(obj, typing.Mapping):
287287
return {key: cls.sanitize_for_serialization(val) for key, val in obj.items()}
288288
raise PineconeApiValueError('Unable to prepare type {} for serialization'.format(obj.__class__.__name__))
289289

@@ -336,17 +336,17 @@ def call_api(
336336
self,
337337
resource_path: str,
338338
method: str,
339-
path_params: typing.Optional[typing.Dict[str, typing.Any]] = None,
340-
query_params: typing.Optional[typing.List[typing.Tuple[str, typing.Any]]] = None,
341-
header_params: typing.Optional[typing.Dict[str, typing.Any]] = None,
339+
path_params: typing.Optional[typing.Mapping[str, typing.Any]] = None,
340+
query_params: typing.Optional[typing.Sequence[typing.Tuple[str, typing.Any]]] = None,
341+
header_params: typing.Optional[typing.Mapping[str, typing.Any]] = None,
342342
body: typing.Optional[typing.Any] = None,
343-
post_params: typing.Optional[typing.List[typing.Tuple[str, typing.Any]]] = None,
344-
files: typing.Optional[typing.Dict[str, typing.List[io.IOBase]]] = None,
343+
post_params: typing.Optional[typing.Sequence[typing.Tuple[str, typing.Any]]] = None,
344+
files: typing.Optional[typing.Mapping[str, typing.Sequence[io.IOBase]]] = None,
345345
response_type: typing.Optional[typing.Tuple[typing.Any]] = None,
346-
auth_settings: typing.Optional[typing.List[str]] = None,
346+
auth_settings: typing.Optional[typing.Sequence[str]] = None,
347347
async_req: typing.Optional[bool] = None,
348348
_return_http_data_only: typing.Optional[bool] = None,
349-
collection_formats: typing.Optional[typing.Dict[str, str]] = None,
349+
collection_formats: typing.Optional[typing.Mapping[str, str]] = None,
350350
_preload_content: bool = True,
351351
_request_timeout: typing.Optional[typing.Union[int, float, typing.Tuple]] = None,
352352
_host: typing.Optional[str] = None,
@@ -498,7 +498,7 @@ def parameters_to_tuples(self, params, collection_formats):
498498
new_params = []
499499
if collection_formats is None:
500500
collection_formats = {}
501-
for k, v in params.items() if isinstance(params, dict) else params: # noqa: E501
501+
for k, v in params.items() if isinstance(params, typing.Mapping) else params: # noqa: E501
502502
if k in collection_formats:
503503
collection_format = collection_formats[k]
504504
if collection_format == 'multi':
@@ -524,7 +524,7 @@ def get_file_data_and_close_file(file_instance: io.IOBase) -> bytes:
524524
file_instance.close()
525525
return file_data
526526

527-
def files_parameters(self, files: typing.Optional[typing.Dict[str, typing.List[io.IOBase]]] = None):
527+
def files_parameters(self, files: typing.Optional[typing.Mapping[str, typing.Sequence[io.IOBase]]] = None):
528528
"""Builds form parameters.
529529
530530
:param files: None or a dict with key=param_name and

pinecone/data/index.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from tqdm.autonotebook import tqdm
22

33
from collections.abc import Iterable
4-
from typing import Union, List, Tuple, Optional, Dict, Any
4+
from typing import Mapping, Union, List, Tuple, Optional, Dict, Any
55

66
from pinecone.config import ConfigBuilder
77

@@ -74,12 +74,12 @@ def __init__(
7474
api_key: str,
7575
host: str,
7676
pool_threads: Optional[int] = 1,
77-
additional_headers: Optional[Dict[str, str]] = {},
77+
additional_headers: Optional[Mapping[str, str]] = {},
7878
**kwargs
7979
):
8080
self._config = ConfigBuilder.build(api_key=api_key, host=host, **kwargs)
81-
82-
api_client = ApiClient(configuration=self._config.openapi_config,
81+
82+
api_client = ApiClient(configuration=self._config.openapi_config,
8383
pool_threads=pool_threads)
8484

8585
# Configure request headers
@@ -90,7 +90,7 @@ def __init__(
9090

9191
self._api_client = api_client
9292
self._vector_api = VectorOperationsApi(api_client=api_client)
93-
93+
9494
def __enter__(self):
9595
return self
9696

@@ -245,7 +245,7 @@ def delete(
245245
ids: Optional[List[str]] = None,
246246
delete_all: Optional[bool] = None,
247247
namespace: Optional[str] = None,
248-
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None,
248+
filter: Optional[Mapping[str, Union[str, float, int, bool, List, dict]]] = None,
249249
**kwargs,
250250
) -> Dict[str, Any]:
251251
"""
@@ -272,7 +272,7 @@ def delete(
272272
Default is False.
273273
namespace (str): The namespace to delete vectors from [optional]
274274
If not specified, the default namespace is used.
275-
filter (Dict[str, Union[str, float, int, bool, List, dict]]):
275+
filter (Mapping[str, Union[str, float, int, bool, List, dict]]):
276276
If specified, the metadata filter here will be used to select the vectors to delete.
277277
This is mutually exclusive with specifying ids to delete in the ids param or using delete_all=True.
278278
See https://www.pinecone.io/docs/metadata-filtering/.. [optional]
@@ -330,10 +330,10 @@ def query(
330330
vector: Optional[List[float]] = None,
331331
id: Optional[str] = None,
332332
namespace: Optional[str] = None,
333-
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None,
333+
filter: Optional[Mapping[str, Union[str, float, int, bool, List, dict]]] = None,
334334
include_values: Optional[bool] = None,
335335
include_metadata: Optional[bool] = None,
336-
sparse_vector: Optional[Union[SparseValues, Dict[str, Union[List[float], List[int]]]]] = None,
336+
sparse_vector: Optional[Union[SparseValues, Mapping[str, Union[List[float], List[int]]]]] = None,
337337
**kwargs,
338338
) -> QueryResponse:
339339
"""
@@ -362,17 +362,17 @@ def query(
362362
top_k (int): The number of results to return for each query. Must be an integer greater than 1.
363363
namespace (str): The namespace to fetch vectors from.
364364
If not specified, the default namespace is used. [optional]
365-
filter (Dict[str, Union[str, float, int, bool, List, dict]):
365+
filter (Mapping[str, Union[str, float, int, bool, List, dict]):
366366
The filter to apply. You can use vector metadata to limit your search.
367367
See https://www.pinecone.io/docs/metadata-filtering/.. [optional]
368368
include_values (bool): Indicates whether vector values are included in the response.
369369
If omitted the server will use the default value of False [optional]
370370
include_metadata (bool): Indicates whether metadata is included in the response as well as the ids.
371371
If omitted the server will use the default value of False [optional]
372-
sparse_vector: (Union[SparseValues, Dict[str, Union[List[float], List[int]]]]): sparse values of the query vector.
372+
sparse_vector: (Union[SparseValues, Mapping[str, Union[List[float], List[int]]]]): sparse values of the query vector.
373373
Expected to be either a SparseValues object or a dict of the form:
374374
{'indices': List[int], 'values': List[float]}, where the lists each have the same length.
375-
375+
376376
Returns: QueryResponse object which contains the list of the closest vectors as ScoredVector objects,
377377
and namespace name.
378378
"""
@@ -414,9 +414,9 @@ def update(
414414
self,
415415
id: str,
416416
values: Optional[List[float]] = None,
417-
set_metadata: Optional[Dict[str, Union[str, float, int, bool, List[int], List[float], List[str]]]] = None,
417+
set_metadata: Optional[Mapping[str, Union[str, float, int, bool, List[int], List[float], List[str]]]] = None,
418418
namespace: Optional[str] = None,
419-
sparse_values: Optional[Union[SparseValues, Dict[str, Union[List[float], List[int]]]]] = None,
419+
sparse_values: Optional[Union[SparseValues, Mapping[str, Union[List[float], List[int]]]]] = None,
420420
**kwargs,
421421
) -> Dict[str, Any]:
422422
"""
@@ -438,10 +438,10 @@ def update(
438438
Args:
439439
id (str): Vector's unique id.
440440
values (List[float]): vector values to set. [optional]
441-
set_metadata (Dict[str, Union[str, float, int, bool, List[int], List[float], List[str]]]]):
441+
set_metadata (Mapping[str, Union[str, float, int, bool, List[int], List[float], List[str]]]]):
442442
metadata to set for vector. [optional]
443443
namespace (str): Namespace name where to update the vector.. [optional]
444-
sparse_values: (Dict[str, Union[List[float], List[int]]]): sparse values to update for the vector.
444+
sparse_values: (Mapping[str, Union[List[float], List[int]]]): sparse values to update for the vector.
445445
Expected to be either a SparseValues object or a dict of the form:
446446
{'indices': List[int], 'values': List[float]} where the lists each have the same length.
447447
@@ -472,7 +472,7 @@ def update(
472472

473473
@validate_and_convert_errors
474474
def describe_index_stats(
475-
self, filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None, **kwargs
475+
self, filter: Optional[Mapping[str, Union[str, float, int, bool, List, dict]]] = None, **kwargs
476476
) -> DescribeIndexStatsResponse:
477477
"""
478478
The DescribeIndexStats operation returns statistics about the index's contents.
@@ -485,7 +485,7 @@ def describe_index_stats(
485485
>>> index.describe_index_stats(filter={'key': 'value'})
486486
487487
Args:
488-
filter (Dict[str, Union[str, float, int, bool, List, dict]]):
488+
filter (Mapping[str, Union[str, float, int, bool, List, dict]]):
489489
If this parameter is present, the operation only returns statistics for vectors that satisfy the filter.
490490
See https://www.pinecone.io/docs/metadata-filtering/.. [optional]
491491
@@ -509,15 +509,15 @@ def _parse_non_empty_args(args: List[Tuple[str, Any]]) -> Dict[str, Any]:
509509

510510
@staticmethod
511511
def _parse_sparse_values_arg(
512-
sparse_values: Optional[Union[SparseValues, Dict[str, Union[List[float], List[int]]]]]
512+
sparse_values: Optional[Union[SparseValues, Mapping[str, Union[List[float], List[int]]]]]
513513
) -> Optional[SparseValues]:
514514
if sparse_values is None:
515515
return None
516516

517517
if isinstance(sparse_values, SparseValues):
518518
return sparse_values
519519

520-
if not isinstance(sparse_values, dict) or "indices" not in sparse_values or "values" not in sparse_values:
520+
if not isinstance(sparse_values, Mapping) or "indices" not in sparse_values or "values" not in sparse_values:
521521
raise ValueError(
522522
"Invalid sparse values argument. Expected a dict of: {'indices': List[int], 'values': List[float]}."
523523
f"Received: {sparse_values}"

pinecone/grpc/index_grpc.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Optional, Dict, Union, List, Tuple, Any, TypedDict, cast
2+
from typing import Mapping, Optional, Dict, Union, List, Tuple, Any, TypedDict, cast
33

44
from google.protobuf import json_format
55

@@ -209,7 +209,7 @@ def delete(
209209
ids: Optional[List[str]] = None,
210210
delete_all: Optional[bool] = None,
211211
namespace: Optional[str] = None,
212-
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None,
212+
filter: Optional[Mapping[str, Union[str, float, int, bool, List, dict]]] = None,
213213
async_req: bool = False,
214214
**kwargs,
215215
) -> Union[DeleteResponse, PineconeGrpcFuture]:
@@ -235,7 +235,7 @@ def delete(
235235
Default is False.
236236
namespace (str): The namespace to delete vectors from [optional]
237237
If not specified, the default namespace is used.
238-
filter (Dict[str, Union[str, float, int, bool, List, dict]]):
238+
filter (Mapping[str, Union[str, float, int, bool, List, dict]]):
239239
If specified, the metadata filter here will be used to select the vectors to delete.
240240
This is mutually exclusive with specifying ids to delete in the ids param or using delete_all=True.
241241
See https://www.pinecone.io/docs/metadata-filtering/.. [optional]
@@ -293,7 +293,7 @@ def query(
293293
id: Optional[str] = None,
294294
namespace: Optional[str] = None,
295295
top_k: Optional[int] = None,
296-
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None,
296+
filter: Optional[Mapping[str, Union[str, float, int, bool, List, dict]]] = None,
297297
include_values: Optional[bool] = None,
298298
include_metadata: Optional[bool] = None,
299299
sparse_vector: Optional[Union[GRPCSparseValues, SparseVectorTypedDict]] = None,
@@ -323,7 +323,7 @@ def query(
323323
top_k (int): The number of results to return for each query. Must be an integer greater than 1.
324324
namespace (str): The namespace to fetch vectors from.
325325
If not specified, the default namespace is used. [optional]
326-
filter (Dict[str, Union[str, float, int, bool, List, dict]]):
326+
filter (Mapping[str, Union[str, float, int, bool, List, dict]]):
327327
The filter to apply. You can use vector metadata to limit your search.
328328
See https://www.pinecone.io/docs/metadata-filtering/.. [optional]
329329
include_values (bool): Indicates whether vector values are included in the response.
@@ -372,7 +372,7 @@ def update(
372372
id: str,
373373
async_req: bool = False,
374374
values: Optional[List[float]] = None,
375-
set_metadata: Optional[Dict[str, Union[str, float, int, bool, List[int], List[float], List[str]]]] = None,
375+
set_metadata: Optional[Mapping[str, Union[str, float, int, bool, List[int], List[float], List[str]]]] = None,
376376
namespace: Optional[str] = None,
377377
sparse_values: Optional[Union[GRPCSparseValues, SparseVectorTypedDict]] = None,
378378
**kwargs,
@@ -396,7 +396,7 @@ def update(
396396
async_req (bool): If True, the update operation will be performed asynchronously.
397397
Defaults to False. [optional]
398398
values (List[float]): vector values to set. [optional]
399-
set_metadata (Dict[str, Union[str, float, int, bool, List[int], List[float], List[str]]]]):
399+
set_metadata (Mapping[str, Union[str, float, int, bool, List[int], List[float], List[str]]]]):
400400
metadata to set for vector. [optional]
401401
namespace (str): Namespace name where to update the vector.. [optional]
402402
sparse_values: (Dict[str, Union[List[float], List[int]]]): sparse values to update for the vector.
@@ -430,7 +430,7 @@ def update(
430430
return self._wrap_grpc_call(self.stub.Update, request, timeout=timeout)
431431

432432
def describe_index_stats(
433-
self, filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None, **kwargs
433+
self, filter: Optional[Mapping[str, Union[str, float, int, bool, List, dict]]] = None, **kwargs
434434
) -> DescribeIndexStatsResponse:
435435
"""
436436
The DescribeIndexStats operation returns statistics about the index's contents.
@@ -441,7 +441,7 @@ def describe_index_stats(
441441
>>> index.describe_index_stats(filter={'key': 'value'})
442442
443443
Args:
444-
filter (Dict[str, Union[str, float, int, bool, List, dict]]):
444+
filter (Mapping[str, Union[str, float, int, bool, List, dict]]):
445445
If this parameter is present, the operation only returns statistics for vectors that satisfy the filter.
446446
See https://www.pinecone.io/docs/metadata-filtering/.. [optional]
447447

0 commit comments

Comments
 (0)