Skip to content

Commit 2e606bb

Browse files
authored
adds util for checking kwargs, and uses it to check init() (#198)
## Problem Per #196 a typo in init's kwargs results in a default that might not be expected. ## Solution Utils now includes check_kwargs, which takes a caller + the given kwargs, and fails with a traceback if they don't match. ## Open Questions cc @jhamon @austin-denoble @rohanshah18 1) Where else should we use check_kwargs? 2) Is the traceback useful? in this instance it risks leaking api keys into logs, so for `init` in particular i question its value. ## Type of Change - [x] 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) - [ ] None of the above: (explain here) ## Test Plan `pinecone.init(not_a_kwarg='foo')` should fail
1 parent f6bda12 commit 2e606bb

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

pinecone/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from pinecone.core.client.exceptions import ApiKeyError
1717
from pinecone.core.api_action import ActionAPI, WhoAmIResponse
18-
from pinecone.core.utils import warn_deprecated
18+
from pinecone.core.utils import warn_deprecated, check_kwargs
1919
from pinecone.core.utils.constants import CLIENT_VERSION, PARENT_LOGGER_NAME, DEFAULT_PARENT_LOGGER_LEVEL, \
2020
TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT
2121
from pinecone.core.client.configuration import Configuration as OpenApiConfiguration
@@ -245,6 +245,7 @@ def init(api_key: str = None, host: str = None, environment: str = None, project
245245
:param config: Optional. An INI configuration file.
246246
:param log_level: Deprecated since v2.0.2 [Will be removed in v3.0.0]; use the standard logging module to manage logger "pinecone" instead.
247247
"""
248+
check_kwargs(init, kwargs)
248249
Config.reset(project_name=project_name, api_key=api_key, controller_host=host, environment=environment,
249250
openapi_config=openapi_config, config_file=config, **kwargs)
250251
if log_level:

pinecone/core/utils/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#
22
# Copyright (c) 2020-2021 Pinecone Systems Inc. All right reserved.
33
#
4+
import inspect
5+
import logging
46
import re
57
import uuid
68
import warnings
@@ -109,3 +111,9 @@ def load_strings_public(proto_arr: 'vector_column_service_pb2.NdArray') -> List[
109111
def warn_deprecated(description: str = '', deprecated_in: str = None, removal_in: str = None):
110112
message = f'DEPRECATED since v{deprecated_in} [Will be removed in v{removal_in}]: {description}'
111113
warnings.warn(message, FutureWarning)
114+
115+
def check_kwargs(caller, given):
116+
argspec = inspect.getfullargspec(caller)
117+
diff = set(given).difference(argspec.args)
118+
if diff:
119+
logging.exception(caller.__name__ + ' had unexpected keyword argument(s): ' + ', '.join(diff), exc_info=False)

tests/unit/test_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ def test_init_with_kwargs():
8484
assert Config.CONTROLLER_HOST == controller_host
8585
assert Config.OPENAPI_CONFIG == openapi_config
8686

87+
def test_init_with_mispelled_kwargs(caplog):
88+
pinecone.init(invalid_kwarg="value")
89+
assert 'init had unexpected keyword argument(s): invalid_kwarg' in caplog.text
90+
8791
def test_init_with_file_based_configuration():
8892
"""Test that config can be loaded from a file"""
8993
env = 'ini-test-env'

0 commit comments

Comments
 (0)