From 5babe88e85e9a3f8553f60883282931e80e29434 Mon Sep 17 00:00:00 2001 From: Chris Gregory Date: Thu, 9 Nov 2023 13:02:17 -0800 Subject: [PATCH] Switch to black formatter and isort --- .vscode/settings.json | 28 +++++++++++---------- hume/_batch/batch_job.py | 8 +++--- hume/_common/config_base.py | 10 +++++--- hume/_common/config_utils.py | 11 +++++++-- hume/_common/retry_utils.py | 8 +++--- hume/_stream/hume_stream_client.py | 18 ++++++++------ hume/_stream/stream_socket.py | 33 ++++++++++++++++--------- hume/models/config/model_config_base.py | 4 +-- pyproject.toml | 21 +++++++++------- 9 files changed, 85 insertions(+), 56 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index c1bcf94c..70e15ce9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,19 +1,21 @@ { "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, "[markdown]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, + "[python]": { "editor.defaultFormatter": "ms-python.black-formatter" }, "[toml]": { "editor.defaultFormatter": "tamasfe.even-better-toml" }, - "prettier.printWidth": 120, + "editor.codeActionsOnSave": { "source.organizeImports": true }, + "black-formatter.args": ["--config", "pyproject.toml"], + "flake8.args": ["--max-line-length", "120"], + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.detectIndentation": true, "editor.formatOnSave": true, - "python.formatting.provider": "yapf", - "python.formatting.yapfPath": "yapf", - "python.formatting.yapfArgs": ["--style", "{based_on_style: pep8, indent_width: 4, column_limit: 119}"], - "python.linting.enabled": true, - "python.linting.flake8Args": ["--max-line-length=119"], - "python.linting.flake8Enabled": true, - "python.linting.pylintEnabled": true, - "python.linting.pylintPath": "pylint", - "python.linting.pylintArgs": ["--rcfile=../src/pyproject.toml"], - "python.testing.cwd": "tests", - "python.testing.pytestEnabled": true, - "python.testing.unittestEnabled": false + "editor.insertSpaces": true, + "editor.rulers": [120], + "isort.args": ["--settings-file", "pyproject.toml"], + "mypy-type-checker.args": ["--config-file", "pyproject.toml"], + "prettier.printWidth": 120, + "pylint.path": ["pylint"], + "pylint.args": ["--rcfile", "pyproject.toml"], + "python.analysis.extraPaths": ["tests"], + "python.testing.cwd": "tests" } diff --git a/hume/_batch/batch_job.py b/hume/_batch/batch_job.py index e0ba03e6..3f1f587b 100644 --- a/hume/_batch/batch_job.py +++ b/hume/_batch/batch_job.py @@ -5,7 +5,7 @@ from hume._batch.batch_job_details import BatchJobDetails from hume._batch.batch_job_status import BatchJobStatus -from hume._common.retry_utils import retry, RetryIterError +from hume._common.retry_utils import RetryIterError, retry from hume.error.hume_client_exception import HumeClientException if TYPE_CHECKING: @@ -15,8 +15,10 @@ class BatchJob: """Batch job.""" - TIMEOUT_MESSAGE = ("Connection to API has been terminated after {}s, but your job will continue to run. " - "Get a reference to your job with `client.get_job('{}')` at any time.") + TIMEOUT_MESSAGE = ( + "Connection to API has been terminated after {}s, but your job will continue to run. " + "Get a reference to your job with `client.get_job('{}')` at any time." + ) def __init__(self, client: "HumeBatchClient", job_id: str): """Construct a BatchJob. diff --git a/hume/_common/config_base.py b/hume/_common/config_base.py index 6860821f..dd180575 100644 --- a/hume/_common/config_base.py +++ b/hume/_common/config_base.py @@ -38,10 +38,12 @@ def from_dict(cls, request_dict: Dict[str, Any]) -> T: if param not in class_fields: removal_params.append(param) class_name = cls.__name__ - warnings.warn(f"Got an unknown parameter `{param}` when loading `{class_name}`. " - "Your installed version of the Python SDK may be out of date " - "with the latest Hume APIs. " - "Run `pip install --upgrade hume` to get the latest version of the Python SDK.") + warnings.warn( + f"Got an unknown parameter `{param}` when loading `{class_name}`. " + "Your installed version of the Python SDK may be out of date " + "with the latest Hume APIs. " + "Run `pip install --upgrade hume` to get the latest version of the Python SDK." + ) for removal_param in removal_params: request_dict.pop(removal_param) diff --git a/hume/_common/config_utils.py b/hume/_common/config_utils.py index 38ac38b9..9ee860cd 100644 --- a/hume/_common/config_utils.py +++ b/hume/_common/config_utils.py @@ -3,8 +3,15 @@ from hume.error.hume_client_exception import HumeClientException from hume.models import ModelType -from hume.models.config import (BurstConfig, FaceConfig, FacemeshConfig, LanguageConfig, ModelConfigBase, NerConfig, - ProsodyConfig) +from hume.models.config import ( + BurstConfig, + FaceConfig, + FacemeshConfig, + LanguageConfig, + ModelConfigBase, + NerConfig, + ProsodyConfig, +) def config_from_model_type(model_type: ModelType) -> Type[ModelConfigBase]: diff --git a/hume/_common/retry_utils.py b/hume/_common/retry_utils.py index 5f7f71f8..acc32710 100644 --- a/hume/_common/retry_utils.py +++ b/hume/_common/retry_utils.py @@ -1,13 +1,14 @@ """Function retry utilities.""" import logging import time -from typing import Optional, cast, Callable, Type, TypeVar +from typing import Callable, Optional, Type, TypeVar, cast + from typing_extensions import ParamSpec from hume.error.hume_client_exception import HumeClientException -P = ParamSpec('P') # Parameter type variable for decorated function -R = TypeVar('R') # Return type variable for decorated function +P = ParamSpec("P") # Parameter type variable for decorated function +R = TypeVar("R") # Return type variable for decorated function logger = logging.getLogger(__name__) @@ -43,7 +44,6 @@ def retry( """ def decorator_func(decorated_func: Callable[P, R]) -> Callable[P, R]: - def func_wrapper(*args: P.args, **kwargs: P.kwargs) -> R: # If the decorated function has kwargs that match the retry decorator kwargs, # then those values override the retry kwargs. diff --git a/hume/_stream/hume_stream_client.py b/hume/_stream/hume_stream_client.py index e8c9655a..4bdbf242 100644 --- a/hume/_stream/hume_stream_client.py +++ b/hume/_stream/hume_stream_client.py @@ -11,6 +11,7 @@ try: import websockets + HAS_WEBSOCKETS = True except ModuleNotFoundError: HAS_WEBSOCKETS = False @@ -54,9 +55,11 @@ def __init__( close_timeout (Optional[int]): Time in seconds before canceling socket close operation. """ if not HAS_WEBSOCKETS: - raise HumeClientException("The websockets package is required to use HumeStreamClient. " - "Run `pip install \"hume[stream]\"` to install a version compatible with the" - "Hume Python SDK.") + raise HumeClientException( + "The websockets package is required to use HumeStreamClient. " + 'Run `pip install "hume[stream]"` to install a version compatible with the' + "Hume Python SDK." + ) self._open_timeout = open_timeout self._close_timeout = close_timeout @@ -91,10 +94,11 @@ async def connect( try: # pylint: disable=no-member async with websockets.connect( # type: ignore[attr-defined] - endpoint, - extra_headers=self._get_client_headers(), - close_timeout=self._close_timeout, - open_timeout=self._open_timeout) as protocol: + endpoint, + extra_headers=self._get_client_headers(), + close_timeout=self._close_timeout, + open_timeout=self._open_timeout, + ) as protocol: yield StreamSocket(protocol, configs, stream_window_ms=stream_window_ms) except websockets.exceptions.InvalidStatusCode as exc: status_code: int = exc.status_code diff --git a/hume/_stream/stream_socket.py b/hume/_stream/stream_socket.py index 518330e7..72be5360 100644 --- a/hume/_stream/stream_socket.py +++ b/hume/_stream/stream_socket.py @@ -10,6 +10,7 @@ try: from websockets.client import WebSocketClientProtocol + HAS_WEBSOCKETS = True except ModuleNotFoundError: HAS_WEBSOCKETS = False @@ -40,9 +41,11 @@ def __init__( HumeClientException: If there is an error processing media over the socket connection. """ if not HAS_WEBSOCKETS: - raise HumeClientException("The websockets package is required to use HumeStreamClient. " - "Run `pip install \"hume[stream]\"` to install a version compatible with the" - "Hume Python SDK.") + raise HumeClientException( + "The websockets package is required to use HumeStreamClient. " + 'Run `pip install "hume[stream]"` to install a version compatible with the' + "Hume Python SDK." + ) self._protocol = protocol self._configs = configs @@ -67,7 +70,7 @@ async def send_file( Returns: Any: Response from the streaming API. """ - with Path(filepath).open('rb') as f: + with Path(filepath).open("rb") as f: bytes_data = base64.b64encode(f.read()) return await self.send_bytes(bytes_data, configs=configs) @@ -147,17 +150,21 @@ async def send_facemesh( n_faces = len(landmarks) if n_faces > self._FACE_LIMIT: - raise HumeClientException("Number of faces sent in facemesh payload was greater " - f"than the limit of {self._FACE_LIMIT}, found {n_faces}.") + raise HumeClientException( + "Number of faces sent in facemesh payload was greater " + f"than the limit of {self._FACE_LIMIT}, found {n_faces}." + ) if n_faces == 0: raise HumeClientException("No faces sent in facemesh payload.") n_landmarks = len(landmarks[0]) if n_landmarks != self._N_LANDMARKS: - raise HumeClientException(f"Number of MediaPipe landmarks per face must be exactly {self._N_LANDMARKS}, " - f"found {n_landmarks}.") + raise HumeClientException( + f"Number of MediaPipe landmarks per face must be exactly {self._N_LANDMARKS}, " f"found {n_landmarks}." + ) if len(landmarks[0][0]) != self._N_SPATIAL: - raise HumeClientException("Invalid facemesh payload detected. " - "Each facemesh landmark should be an (x, y, z) point.") + raise HumeClientException( + "Invalid facemesh payload detected. " "Each facemesh landmark should be an (x, y, z) point." + ) landmarks_str = json.dumps(landmarks) bytes_data = base64.b64encode(landmarks_str.encode("utf-8")) @@ -244,5 +251,7 @@ def _validate_configs_with_model_type( if not isinstance(config, config_type): config_name = config_type.__name__ invalid_config_name = config.__class__.__name__ - raise HumeClientException(f"{config_method} configured with {invalid_config_name}. " - f"{method_name} is only supported when using a {config_name}.") + raise HumeClientException( + f"{config_method} configured with {invalid_config_name}. " + f"{method_name} is only supported when using a {config_name}." + ) diff --git a/hume/models/config/model_config_base.py b/hume/models/config/model_config_base.py index 833b7740..d889c38c 100644 --- a/hume/models/config/model_config_base.py +++ b/hume/models/config/model_config_base.py @@ -1,9 +1,9 @@ """Abstract base class for model configurations.""" -from abc import abstractmethod, ABC +from abc import ABC, abstractmethod from dataclasses import dataclass from typing import Generic, TypeVar -from hume._common.config_base import ConfigBase +from hume._common.config_base import ConfigBase from hume.models import ModelType T = TypeVar("T") # Type for subclasses of ModelConfigBase diff --git a/pyproject.toml b/pyproject.toml index 9aa23066..3800787a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,14 +34,15 @@ repository = "https://github.com/HumeAI/hume-python-sdk" version = "0.4.1" [tool.poetry.dependencies] +jupyter = { version = "^1.0.0", optional = true } +pydub = { version = "^0.25.1", optional = true } python = ">=3.8.1,<4" requests = "^2.28.2" typing-extensions = "^4.3.0" websockets = { version = "^10.3", optional = true } -jupyter = { version = "^1.0.0", optional = true } -pydub = { version = "^0.25.1", optional = true } [tool.poetry.dev-dependencies] +black = "^23.9.1" covcheck = { version = "^0.4.3", extras = ["toml"] } flake8 = "^6.0.0" ipykernel = "^6.22.0" @@ -59,7 +60,6 @@ testbook = "^0.4.2" types-requests = "^2.25.11" types-setuptools = "^57.4.4" types-toml = "^0.10.1" -yapf = "^0.32.0" [tool.poetry.group.docs] optional = true @@ -71,13 +71,16 @@ mkdocs-material = "^9.1.9" mkdocstrings = { version = "^0.21.2", extras = ["python"] } [tool.poetry.extras] -stream = ["websockets"] examples = ["jupyter", "pydub"] +stream = ["websockets"] [build-system] build-backend = "poetry.core.masonry.api" requires = ["poetry-core>=1.0.0"] +[tool.black] +line-length = 120 + [tool.covcheck.group.unit.coverage] branch = 63.0 line = 80.0 @@ -90,6 +93,11 @@ line = 87.0 ignore = "" # Required to disable default ignores max-line-length = 119 +[tool.isort] +line_length = 120 +profile = "black" +src_paths = "*" + [tool.mypy] disallow_incomplete_defs = true disallow_untyped_calls = true @@ -118,8 +126,3 @@ disable = [ [tool.pylint.reports] output-format = "colorized" - -[tool.yapf] -based_on_style = "pep8" -column_limit = 119 -indent_width = 4