Skip to content

Commit 0b5e426

Browse files
committed
Fix mypy errors for python 3.9
1 parent 48aba57 commit 0b5e426

File tree

4 files changed

+40
-46
lines changed

4 files changed

+40
-46
lines changed

src/humanloop/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def agent():
395395
attributes=attributes,
396396
)
397397

398-
def pull(self, path: str | None = None, environment: str | None = None) -> Tuple[List[str], List[str]]:
398+
def pull(self, path: Optional[str] = None, environment: Optional[str] = None) -> Tuple[List[str], List[str]]:
399399
"""Pull Prompt and Agent files from Humanloop to local filesystem.
400400
401401
This method will:

src/humanloop/sync/sync_client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from pathlib import Path
3-
from typing import List, Tuple, TYPE_CHECKING
3+
from typing import List, Optional, Tuple, TYPE_CHECKING, Union
44
from functools import lru_cache
55
import typing
66
import time
@@ -203,7 +203,7 @@ def _save_serialized_file(
203203
logger.error(f"Failed to write {file_type} {file_path} to disk: {str(e)}")
204204
raise
205205

206-
def _pull_file(self, path: str, environment: str | None = None) -> bool:
206+
def _pull_file(self, path: str, environment: Optional[str] = None) -> bool:
207207
"""Pull a specific file from Humanloop to local filesystem.
208208
209209
Returns:
@@ -236,8 +236,8 @@ def _pull_file(self, path: str, environment: str | None = None) -> bool:
236236

237237
def _pull_directory(
238238
self,
239-
path: str | None = None,
240-
environment: str | None = None,
239+
path: Optional[str] = None,
240+
environment: Optional[str] = None,
241241
) -> Tuple[List[str], List[str]]:
242242
"""Sync Prompt and Agent files from Humanloop to local filesystem.
243243
@@ -316,7 +316,7 @@ def _pull_directory(
316316

317317
return successful_files, failed_files
318318

319-
def pull(self, path: str | None = None, environment: str | None = None) -> Tuple[List[str], List[str]]:
319+
def pull(self, path: Optional[str] = None, environment: Optional[str] = None) -> Tuple[List[str], List[str]]:
320320
"""Pull files from Humanloop to local filesystem.
321321
322322
If the path ends with .prompt or .agent, pulls that specific file.
@@ -343,7 +343,9 @@ def pull(self, path: str | None = None, environment: str | None = None) -> Tuple
343343
)
344344

345345
try:
346-
if normalized_path is None or path is None: # path being None means normalized_path is None, but we check both for improved type safety
346+
if (
347+
normalized_path is None or path is None
348+
): # path being None means normalized_path is None, but we check both for improved type safety
347349
# Pull all files from the root
348350
logger.debug("Pulling all files from root")
349351
successful_files, failed_files = self._pull_directory(

tests/custom/integration/conftest.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import io
21
import os
32
import time
3+
import typing
44
import uuid
5-
from contextlib import contextmanager, redirect_stdout
5+
from collections.abc import Generator
66
from dataclasses import dataclass
7-
from typing import ContextManager, Generator, List, TextIO, Union
7+
from typing import Union
88

99
import dotenv
1010
import pytest
@@ -21,17 +21,6 @@ class ResourceIdentifiers:
2121
file_path: str
2222

2323

24-
@pytest.fixture()
25-
def capture_stdout() -> ContextManager[TextIO]:
26-
@contextmanager
27-
def _context_manager():
28-
f = io.StringIO()
29-
with redirect_stdout(f):
30-
yield f
31-
32-
return _context_manager # type: ignore [return-value]
33-
34-
3524
@pytest.fixture(scope="session")
3625
def openai_key() -> str:
3726
dotenv.load_dotenv()
@@ -44,26 +33,26 @@ def openai_key() -> str:
4433
def sdk_test_dir(get_humanloop_client: GetHumanloopClientFn) -> Generator[str, None, None]:
4534
humanloop_client = get_humanloop_client()
4635

36+
def _get_subclient(file_type: str):
37+
try:
38+
return {
39+
"agent": humanloop_client.agents,
40+
"prompt": humanloop_client.prompts,
41+
"dataset": humanloop_client.datasets,
42+
"evaluator": humanloop_client.evaluators,
43+
"flow": humanloop_client.flows,
44+
"tool": humanloop_client.tools,
45+
}[file_type]
46+
except KeyError:
47+
raise NotImplementedError(f"Unknown file type: {file_type}")
48+
4749
def cleanup_directory(directory_id: str):
4850
directory_response = humanloop_client.directories.get(id=directory_id)
4951
for subdirectory in directory_response.subdirectories:
5052
cleanup_directory(subdirectory.id)
5153
for file in directory_response.files:
52-
match file.type:
53-
case "agent":
54-
humanloop_client.agents.delete(id=file.id)
55-
case "prompt":
56-
humanloop_client.prompts.delete(id=file.id)
57-
case "dataset":
58-
humanloop_client.datasets.delete(id=file.id)
59-
case "evaluator":
60-
humanloop_client.evaluators.delete(id=file.id)
61-
case "flow":
62-
humanloop_client.flows.delete(id=file.id)
63-
case "tool":
64-
humanloop_client.tools.delete(id=file.id)
65-
case _:
66-
raise ValueError(f"Unknown file type: {file.type}")
54+
subclient = _get_subclient(typing.cast(str, file.type))
55+
subclient.delete(id=file.id)
6756
humanloop_client.directories.delete(id=directory_response.id)
6857

6958
path = f"SDK_INTEGRATION_TEST_{uuid.uuid4()}"
@@ -211,7 +200,7 @@ def syncable_files_fixture(
211200
sdk_test_dir: str,
212201
) -> Generator[list[SyncableFile], None, None]:
213202
"""Creates a predefined structure of files in Humanloop for testing sync."""
214-
files: List[SyncableFile] = [
203+
files: list[SyncableFile] = [
215204
SyncableFile(
216205
path="prompts/gpt-4",
217206
type="prompt",

tests/custom/integration/test_sync.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import typing
12
from pathlib import Path
23
from typing import List, Union
34

@@ -80,15 +81,17 @@ def test_overload_with_local_files(
8081
# WHEN calling with an invalid path
8182
# THEN it should raise HumanloopRuntimeError
8283
with pytest.raises(HumanloopRuntimeError):
83-
sub_client: Union[PromptsClient, AgentsClient]
84-
match test_file.type:
85-
case "prompt":
86-
sub_client = humanloop_client.prompts
87-
case "agent":
88-
sub_client = humanloop_client.agents
89-
case _:
90-
raise ValueError(f"Invalid file type: {test_file.type}")
91-
sub_client.call(path="invalid/path")
84+
try:
85+
sub_client: Union[PromptsClient, AgentsClient] = typing.cast(
86+
Union[PromptsClient, AgentsClient],
87+
{
88+
"prompt": humanloop_client.prompts,
89+
"agent": humanloop_client.agents,
90+
}[test_file.type],
91+
)
92+
sub_client.call(path="invalid/path")
93+
except KeyError:
94+
raise NotImplementedError(f"Unknown file type: {test_file.type}")
9295

9396

9497
def test_overload_log_with_local_files(

0 commit comments

Comments
 (0)