Skip to content

Commit

Permalink
Switch to black formatter and isort
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybchris committed Nov 9, 2023
1 parent f734e29 commit 5babe88
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 56 deletions.
28 changes: 15 additions & 13 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 5 additions & 3 deletions hume/_batch/batch_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
10 changes: 6 additions & 4 deletions hume/_common/config_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 9 additions & 2 deletions hume/_common/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
8 changes: 4 additions & 4 deletions hume/_common/retry_utils.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions hume/_stream/hume_stream_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

try:
import websockets

HAS_WEBSOCKETS = True
except ModuleNotFoundError:
HAS_WEBSOCKETS = False
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions hume/_stream/stream_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

try:
from websockets.client import WebSocketClientProtocol

HAS_WEBSOCKETS = True
except ModuleNotFoundError:
HAS_WEBSOCKETS = False
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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}."
)
4 changes: 2 additions & 2 deletions hume/models/config/model_config_base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 12 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -118,8 +126,3 @@ disable = [

[tool.pylint.reports]
output-format = "colorized"

[tool.yapf]
based_on_style = "pep8"
column_limit = 119
indent_width = 4

0 comments on commit 5babe88

Please sign in to comment.