Skip to content

Commit

Permalink
Add Black Formatting and Linting (#355)
Browse files Browse the repository at this point in the history
## Problem

PRs often include formatting changes that makes it difficult to identify
changes in functionality

## Solution

- Format all code with `black`
- Introduce linting `black` linting workflow

## 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: (explain here)
  • Loading branch information
gdj0nes authored Jun 20, 2024
1 parent 701f6b6 commit 320afc1
Show file tree
Hide file tree
Showing 152 changed files with 6,608 additions and 5,880 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: "Lint"
on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: psf/black@stable
17 changes: 9 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black
rev: 23.9.1
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.2
hooks:
- id: black
- id: black
language_version: python3.12
3 changes: 2 additions & 1 deletion pinecone/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
.. include:: ../README.md
"""

import warnings
from tqdm import TqdmExperimentalWarning

Expand All @@ -17,4 +18,4 @@
IndexModel,
)

from .utils import __version__
from .utils import __version__
4 changes: 2 additions & 2 deletions pinecone/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
from .config import ConfigBuilder, Config
from .pinecone_config import PineconeConfig

if (os.getenv('PINECONE_DEBUG') != None):
logging.basicConfig(level=logging.DEBUG)
if os.getenv("PINECONE_DEBUG") != None:
logging.basicConfig(level=logging.DEBUG)
22 changes: 13 additions & 9 deletions pinecone/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pinecone.utils import normalize_host
from pinecone.utils.constants import SOURCE_TAG


class Config(NamedTuple):
api_key: str = ""
host: str = ""
Expand All @@ -17,6 +18,7 @@ class Config(NamedTuple):
additional_headers: Optional[Dict[str, str]] = {}
source_tag: Optional[str] = None


class ConfigBuilder:
"""
Expand Down Expand Up @@ -56,27 +58,29 @@ def build(
raise PineconeConfigurationError("You haven't specified a host.")

return Config(api_key, host, proxy_url, proxy_headers, ssl_ca_certs, ssl_verify, additional_headers, source_tag)

@staticmethod
def build_openapi_config(
config: Config, openapi_config: Optional[OpenApiConfiguration] = None, **kwargs
) -> OpenApiConfiguration:
if openapi_config:
openapi_config = OpenApiConfigFactory.copy(openapi_config=openapi_config, api_key=config.api_key, host=config.host)
openapi_config = OpenApiConfigFactory.copy(
openapi_config=openapi_config, api_key=config.api_key, host=config.host
)
elif openapi_config is None:
openapi_config = OpenApiConfigFactory.build(api_key=config.api_key, host=config.host)

# Check if value passed before overriding any values present
# in the openapi_config. This means if the user has passed
# in the openapi_config. This means if the user has passed
# an openapi_config object and a kwarg for the same setting,
# the kwarg will take precedence.
if (config.proxy_url):
if config.proxy_url:
openapi_config.proxy = config.proxy_url
if (config.proxy_headers):
if config.proxy_headers:
openapi_config.proxy_headers = config.proxy_headers
if (config.ssl_ca_certs):
if config.ssl_ca_certs:
openapi_config.ssl_ca_cert = config.ssl_ca_certs
if (config.ssl_verify != None):
if config.ssl_verify != None:
openapi_config.verify_ssl = config.ssl_verify
return openapi_config

return openapi_config
10 changes: 5 additions & 5 deletions pinecone/config/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def build(cls, api_key: str, host: Optional[str] = None, **kwargs):
openapi_config.ssl_ca_cert = certifi.where()
openapi_config.socket_options = cls._get_socket_options()
return openapi_config

@classmethod
def copy(cls, openapi_config: OpenApiConfiguration, api_key: str, host: str) -> OpenApiConfiguration:
'''
Copy a user-supplied openapi configuration and update it with the user's api key and host.
"""
Copy a user-supplied openapi configuration and update it with the user's api key and host.
If they have not specified other socket configuration, we will use the default values.
We expect these objects are being passed mainly a vehicle for proxy configuration, so
We expect these objects are being passed mainly a vehicle for proxy configuration, so
we don't modify those settings.
'''
"""
copied = copy.deepcopy(openapi_config)

copied.api_key = {"ApiKeyAuth": api_key}
Expand Down
19 changes: 12 additions & 7 deletions pinecone/config/pinecone_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,21 @@
DEFAULT_CONTROLLER_HOST = "https://api.pinecone.io"


class PineconeConfig():
class PineconeConfig:
@staticmethod
def build(api_key: Optional[str] = None, host: Optional[str] = None, additional_headers: Optional[Dict[str, str]] = {}, **kwargs) -> Config:
def build(
api_key: Optional[str] = None,
host: Optional[str] = None,
additional_headers: Optional[Dict[str, str]] = {},
**kwargs,
) -> Config:
host = host or kwargs.get("host") or os.getenv("PINECONE_CONTROLLER_HOST") or DEFAULT_CONTROLLER_HOST
headers_json = os.getenv("PINECONE_ADDITIONAL_HEADERS")
if headers_json:
try:
headers = json.loads(headers_json)
additional_headers = additional_headers or headers
except Exception as e:
logger.warn(f'Ignoring PINECONE_ADDITIONAL_HEADERS: {e}')
try:
headers = json.loads(headers_json)
additional_headers = additional_headers or headers
except Exception as e:
logger.warn(f"Ignoring PINECONE_ADDITIONAL_HEADERS: {e}")

return ConfigBuilder.build(api_key=api_key, host=host, additional_headers=additional_headers, **kwargs)
2 changes: 1 addition & 1 deletion pinecone/control/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .pinecone import Pinecone
from .pinecone import Pinecone
5 changes: 4 additions & 1 deletion pinecone/control/index_host_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pinecone.core.client.exceptions import PineconeException
from pinecone.utils import normalize_host


class SingletonMeta(type):
_instances: Dict[str, str] = {}

Expand Down Expand Up @@ -42,5 +43,7 @@ def get_host(self, api: IndexOperationsApi, config: Config, index_name: str) ->
description = api.describe_index(index_name)
self.set_host(config, index_name, description.host)
if not self.key_exists(key):
raise PineconeException(f"Could not get host for index: {index_name}. Call describe_index('{index_name}') to check the current status.")
raise PineconeException(
f"Could not get host for index: {index_name}. Call describe_index('{index_name}') to check the current status."
)
return self._indexHosts[key]
6 changes: 3 additions & 3 deletions pinecone/control/langchain_import_warnings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from pinecone.utils import docslinks

KB_ARTICLE = docslinks['LANGCHAIN_IMPORT_KB_ARTICLE']
GITHUB_REPO = docslinks['GITHUB_REPO']
KB_ARTICLE = docslinks["LANGCHAIN_IMPORT_KB_ARTICLE"]
GITHUB_REPO = docslinks["GITHUB_REPO"]


def _build_langchain_attribute_error_message(method_name: str):
return f"""{method_name} is not a top-level attribute of the Pinecone class provided by pinecone's official python package developed at {GITHUB_REPO}. You may have a name collision with an export from another dependency in your project that wraps Pinecone functionality and exports a similarly named class. Please refer to the following knowledge base article for more information: {KB_ARTICLE}
"""

Loading

0 comments on commit 320afc1

Please sign in to comment.