From 1d86b040746b64d412c1c9ce578639474f171b62 Mon Sep 17 00:00:00 2001 From: Travis Donia Date: Thu, 24 Aug 2023 15:58:09 -0400 Subject: [PATCH 1/3] adds util for checking kwargs, and uses it to check init() --- pinecone/config.py | 3 ++- pinecone/core/utils/check_kwargs.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 pinecone/core/utils/check_kwargs.py diff --git a/pinecone/config.py b/pinecone/config.py index 84b5793a..d60bc0f6 100644 --- a/pinecone/config.py +++ b/pinecone/config.py @@ -15,7 +15,7 @@ from pinecone.core.client.exceptions import ApiKeyError from pinecone.core.api_action import ActionAPI, WhoAmIResponse -from pinecone.core.utils import warn_deprecated +from pinecone.core.utils import warn_deprecated, check_kwargs from pinecone.core.utils.constants import CLIENT_VERSION, PARENT_LOGGER_NAME, DEFAULT_PARENT_LOGGER_LEVEL, \ TCP_KEEPIDLE, TCP_KEEPINTVL, TCP_KEEPCNT 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 :param config: Optional. An INI configuration file. :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. """ + check_kwargs.check_kwargs(init, kwargs) Config.reset(project_name=project_name, api_key=api_key, controller_host=host, environment=environment, openapi_config=openapi_config, config_file=config, **kwargs) if log_level: diff --git a/pinecone/core/utils/check_kwargs.py b/pinecone/core/utils/check_kwargs.py new file mode 100644 index 00000000..750d8ace --- /dev/null +++ b/pinecone/core/utils/check_kwargs.py @@ -0,0 +1,7 @@ + +def check_kwargs(caller, given): + import inspect + argspec = inspect.getfullargspec(caller) + diff = set(given).difference(argspec.args) + if diff: + raise TypeError(caller.__name__ + ' had unexpected keyword argument(s): ' + ', '.join(diff)) \ No newline at end of file From 9757704d2dd0527c6c93d7566f8788b3ed645dfc Mon Sep 17 00:00:00 2001 From: Travis Donia Date: Thu, 24 Aug 2023 16:09:02 -0400 Subject: [PATCH 2/3] move check_kwargs into common util for better sig --- pinecone/config.py | 2 +- pinecone/core/utils/__init__.py | 7 +++++++ pinecone/core/utils/check_kwargs.py | 7 ------- 3 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 pinecone/core/utils/check_kwargs.py diff --git a/pinecone/config.py b/pinecone/config.py index d60bc0f6..4e29f013 100644 --- a/pinecone/config.py +++ b/pinecone/config.py @@ -245,7 +245,7 @@ def init(api_key: str = None, host: str = None, environment: str = None, project :param config: Optional. An INI configuration file. :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. """ - check_kwargs.check_kwargs(init, kwargs) + check_kwargs(init, kwargs) Config.reset(project_name=project_name, api_key=api_key, controller_host=host, environment=environment, openapi_config=openapi_config, config_file=config, **kwargs) if log_level: diff --git a/pinecone/core/utils/__init__.py b/pinecone/core/utils/__init__.py index ba8df7e4..b4ba0d32 100644 --- a/pinecone/core/utils/__init__.py +++ b/pinecone/core/utils/__init__.py @@ -109,3 +109,10 @@ def load_strings_public(proto_arr: 'vector_column_service_pb2.NdArray') -> List[ def warn_deprecated(description: str = '', deprecated_in: str = None, removal_in: str = None): message = f'DEPRECATED since v{deprecated_in} [Will be removed in v{removal_in}]: {description}' warnings.warn(message, FutureWarning) + +def check_kwargs(caller, given): + import inspect + argspec = inspect.getfullargspec(caller) + diff = set(given).difference(argspec.args) + if diff: + raise TypeError(caller.__name__ + ' had unexpected keyword argument(s): ' + ', '.join(diff)) \ No newline at end of file diff --git a/pinecone/core/utils/check_kwargs.py b/pinecone/core/utils/check_kwargs.py deleted file mode 100644 index 750d8ace..00000000 --- a/pinecone/core/utils/check_kwargs.py +++ /dev/null @@ -1,7 +0,0 @@ - -def check_kwargs(caller, given): - import inspect - argspec = inspect.getfullargspec(caller) - diff = set(given).difference(argspec.args) - if diff: - raise TypeError(caller.__name__ + ' had unexpected keyword argument(s): ' + ', '.join(diff)) \ No newline at end of file From bc7c22731f478b0b09135bcb077f383947718806 Mon Sep 17 00:00:00 2001 From: Travis Donia Date: Fri, 25 Aug 2023 15:01:28 -0400 Subject: [PATCH 3/3] add test and turn off traceback for this exception --- pinecone/core/utils/__init__.py | 5 +++-- tests/unit/test_config.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pinecone/core/utils/__init__.py b/pinecone/core/utils/__init__.py index b4ba0d32..cc944eb7 100644 --- a/pinecone/core/utils/__init__.py +++ b/pinecone/core/utils/__init__.py @@ -1,6 +1,8 @@ # # Copyright (c) 2020-2021 Pinecone Systems Inc. All right reserved. # +import inspect +import logging import re import uuid import warnings @@ -111,8 +113,7 @@ def warn_deprecated(description: str = '', deprecated_in: str = None, removal_in warnings.warn(message, FutureWarning) def check_kwargs(caller, given): - import inspect argspec = inspect.getfullargspec(caller) diff = set(given).difference(argspec.args) if diff: - raise TypeError(caller.__name__ + ' had unexpected keyword argument(s): ' + ', '.join(diff)) \ No newline at end of file + logging.exception(caller.__name__ + ' had unexpected keyword argument(s): ' + ', '.join(diff), exc_info=False) \ No newline at end of file diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 5b5dcc26..5251b67c 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -84,6 +84,10 @@ def test_init_with_kwargs(): assert Config.CONTROLLER_HOST == controller_host assert Config.OPENAPI_CONFIG == openapi_config +def test_init_with_mispelled_kwargs(caplog): + pinecone.init(invalid_kwarg="value") + assert 'init had unexpected keyword argument(s): invalid_kwarg' in caplog.text + def test_init_with_file_based_configuration(): """Test that config can be loaded from a file""" env = 'ini-test-env'