Fix typing and minor issues

This commit is contained in:
Andras Schmelczer 2022-07-07 14:12:44 +02:00
parent 2db2253578
commit 72ab627a34
54 changed files with 635 additions and 589 deletions

0
tests/__init__.py Normal file
View file

View file

@ -1,9 +0,0 @@
from great_ai import GreatAI
def test_process_batch() -> None:
@GreatAI.create(return_raw_result=True)
def f(x):
return x + 2
assert f.process_batch([3, 9, 34]) == [5, 11, 36]

View file

@ -1,13 +1,7 @@
from asyncio import sleep
import pytest
from great_ai import (
ArgumentValidationError,
GreatAI,
WrongDecoratorOrderError,
parameter,
)
from great_ai import GreatAI, WrongDecoratorOrderError, parameter
@pytest.mark.asyncio
@ -17,28 +11,28 @@ async def test_create_trivial_cases() -> None:
await sleep(0.5)
return f"Hello {name}!"
assert (await hello_world_1("andras").output) == "Hello andras!"
assert (await hello_world_1("andras")).output == "Hello andras!"
@GreatAI.create
async def hello_world_2(name: str) -> str:
await sleep(0.5)
return f"Hello {name}!"
assert (await hello_world_2("andras").output) == "Hello andras!"
assert (await hello_world_2("andras")).output == "Hello andras!"
@GreatAI.create()
async def hello_world_3(name: str) -> str:
await sleep(0.5)
return f"Hello {name}!"
assert (await hello_world_3("andras").output) == "Hello andras!"
assert (await hello_world_3("andras")).output == "Hello andras!"
@GreatAI.create()
async def hello_world_4(name):
async def hello_world_4(name): # type: ignore
await sleep(0.5)
return f"Hello {name}!"
assert (await hello_world_4("andras").output) == "Hello andras!"
assert (await hello_world_4("andras")).output == "Hello andras!"
@pytest.mark.asyncio
@ -49,14 +43,23 @@ async def test_with_parameter() -> None:
await sleep(0.5)
return f"Hello {name}!"
assert (await hello_world("andras").output) == "Hello andras!"
with pytest.raises(ArgumentValidationError):
await hello_world("short")
@pytest.mark.asyncio
async def test_with_parameters() -> None:
@GreatAI.create
@parameter("name", validator=lambda v: len(v) > 5)
@parameter("unused", disable_logging=True)
async def hello_world(name: str, unused) -> str: # type: ignore
await sleep(0.5)
return f"Hello {name}!"
assert (await hello_world("andras", "fr")).output == "Hello andras!"
def test_wrong_order() -> None:
with pytest.raises(WrongDecoratorOrderError):
@parameter("name", validator=lambda v: len(v) > 5)
@GreatAI.create
def hello_world(name: str) -> str:
async def hello_world(name: str) -> str:
return f"Hello {name}!"

View file

@ -1,7 +1,6 @@
from functools import lru_cache
import pytest
from great_ai import (
ArgumentValidationError,
GreatAI,
@ -18,7 +17,7 @@ def test_create_trivial_cases() -> None:
assert hello_world_1("andras").output == "Hello andras!"
@GreatAI.create
def hello_world_2(name):
def hello_world_2(name): # type: ignore
return f"Hello {name}!"
assert hello_world_2("andras").output == "Hello andras!"
@ -30,7 +29,7 @@ def test_create_trivial_cases() -> None:
assert hello_world_3("andras").output == "Hello andras!"
@GreatAI.create()
def hello_world_4(name):
def hello_world_4(name): # type: ignore
return f"Hello {name}!"
assert hello_world_4("andras").output == "Hello andras!"
@ -39,17 +38,17 @@ def test_create_trivial_cases() -> None:
def test_create_with_other_decorator() -> None:
@GreatAI.create
@lru_cache
def hello_world(name: str) -> str:
def hello_world_1(name: str) -> str:
return f"Hello {name}!"
assert hello_world("andras").output == "Hello andras!"
assert hello_world_1("andras").output == "Hello andras!"
@lru_cache
@GreatAI.create()
def hello_world(name: str) -> str:
def hello_world_2(name: str) -> str:
return f"Hello {name}!"
assert hello_world("andras").output == "Hello andras!"
assert hello_world_2("andras").output == "Hello andras!"
def test_with_parameter() -> None:
@ -63,6 +62,8 @@ def test_with_parameter() -> None:
with pytest.raises(ArgumentValidationError):
hello_world("short")
def test_wrong_order() -> None:
with pytest.raises(WrongDecoratorOrderError):
@parameter("name", validator=lambda v: len(v) > 5)

22
tests/test_great_ai.py Normal file
View file

@ -0,0 +1,22 @@
from asyncio import sleep
import pytest
from great_ai import GreatAI
def test_process_batch() -> None:
@GreatAI.create
def f(x: int) -> int:
return x + 2
assert [v.output for v in f.process_batch([3, 9, 34])] == [5, 11, 36]
@pytest.mark.asyncio
async def test_process_batch_async() -> None:
@GreatAI.create
async def f(x: int) -> int:
await sleep(0.2)
return x + 2
assert [v.output for v in f.process_batch([3, 9, 34])] == [5, 11, 36]

View file

@ -1,5 +1,4 @@
import pytest
from great_ai.utilities import chunk
@ -23,7 +22,7 @@ def test_bad_argument() -> None:
def test_generator() -> None:
def my_generator():
def my_generator(): # type: ignore
for i in range(1, 11):
yield i

View file

@ -2,7 +2,6 @@ import os
from pathlib import Path
import pytest
from great_ai.utilities import ConfigFile
DATA_PATH = Path(__file__).parent.resolve() / "data"

View file

@ -20,11 +20,11 @@ def test_is_english() -> None:
assert not is_english(None)
def english_name_of_language() -> None:
def test_english_name_of_language() -> None:
assert english_name_of_language("en") == "English"
assert english_name_of_language("hu") == "Hungarian"
assert english_name_of_language("zh") == "Chinese"
assert english_name_of_language("zh-TW") == "Chinese"
assert english_name_of_language("zh-TW") == "Chinese (Taiwan)"
assert english_name_of_language("und") == "Unknown language"
assert english_name_of_language("") == "Unknown language"
assert english_name_of_language(None) == "Unknown language"

View file

@ -1,11 +1,13 @@
import pytest
from typing import Any, Iterable
import pytest
from great_ai.utilities import WorkerException, parallel_map
from typing_extensions import Never
COUNT = int(1e5) + 3
def test_simple_case_with_progress_bar() -> None:
def test_simple_case() -> None:
assert list(parallel_map(lambda v: v**2, range(COUNT), concurrency=4)) == [
v**2 for v in range(COUNT)
]
@ -14,7 +16,7 @@ def test_simple_case_with_progress_bar() -> None:
def test_with_iterable() -> None:
from time import sleep
def my_generator():
def my_generator() -> Iterable[int]:
for i in range(10):
yield i
sleep(0.1)
@ -26,12 +28,6 @@ def test_with_iterable() -> None:
)
def test_simple_case_without_progress_bar() -> None:
assert list(parallel_map(lambda v: v**2, range(COUNT), concurrency=2)) == [
v**2 for v in range(COUNT)
]
def test_simple_case_invalid_values() -> None:
with pytest.raises(AssertionError):
list(parallel_map(lambda v: v**2, range(COUNT), concurrency=0))
@ -41,7 +37,7 @@ def test_simple_case_invalid_values() -> None:
def test_this_process_exception() -> None:
def my_generator():
def my_generator() -> Iterable[int]:
yield 1
yield 2
yield 3
@ -59,7 +55,7 @@ def test_this_process_exception() -> None:
def test_ignore_this_process_exception() -> None:
def my_generator():
def my_generator() -> Iterable[float]:
yield 1
yield 2
yield 3
@ -74,19 +70,10 @@ def test_ignore_this_process_exception() -> None:
ignore_exceptions=True,
)
) == [1, 4]
assert list(
parallel_map(
lambda v: v**2,
my_generator(),
concurrency=1,
chunk_size=2,
ignore_exceptions=True,
)
) == [1, 4, 9]
def test_worker_process_exception() -> None:
def oh_no(_):
def oh_no(_: Any) -> Never:
raise ValueError("hi")
with pytest.raises(WorkerException):
@ -97,7 +84,7 @@ def test_worker_process_exception() -> None:
def test_ignore_worker_process_exception() -> None:
def oh_no(_):
def oh_no(_: Any) -> Never:
raise ValueError("hi")
assert (

View file

@ -1,11 +1,13 @@
import pytest
from typing import Any, Iterable
import pytest
from great_ai.utilities import WorkerException, threaded_parallel_map
from typing_extensions import Never
COUNT = int(1e5) + 3
def test_simple_case_with_progress_bar() -> None:
def test_simple_case() -> None:
assert list(
threaded_parallel_map(lambda v: v**2, range(COUNT), concurrency=4)
) == [v**2 for v in range(COUNT)]
@ -14,7 +16,7 @@ def test_simple_case_with_progress_bar() -> None:
def test_with_iterable() -> None:
from time import sleep
def my_generator():
def my_generator() -> Iterable[int]:
for i in range(10):
yield i
sleep(0.1)
@ -27,12 +29,6 @@ def test_with_iterable() -> None:
)
def test_simple_case_without_progress_bar() -> None:
assert list(
threaded_parallel_map(lambda v: v**2, range(COUNT), concurrency=2)
) == [v**2 for v in range(COUNT)]
def test_simple_case_invalid_values() -> None:
with pytest.raises(AssertionError):
list(threaded_parallel_map(lambda v: v**2, range(COUNT), concurrency=0))
@ -42,7 +38,7 @@ def test_simple_case_invalid_values() -> None:
def test_this_worker_exception() -> None:
def my_generator():
def my_generator() -> Iterable[int]:
yield 1
yield 2
yield 3
@ -64,7 +60,7 @@ def test_this_worker_exception() -> None:
def test_ignore_this_worker_exception() -> None:
def my_generator():
def my_generator() -> Iterable[float]:
yield 1
yield 2
yield 3
@ -78,20 +74,14 @@ def test_ignore_this_worker_exception() -> None:
chunk_size=2,
ignore_exceptions=True,
)
) == [1, 4]
assert list(
threaded_parallel_map(
lambda v: v**2,
my_generator(),
concurrency=1,
chunk_size=2,
ignore_exceptions=True,
)
) == [1, 4, 9]
) == [
1,
4,
] # the second chunk is ruined because of the error
def test_worker_worker_exception() -> None:
def oh_no(_):
def oh_no(_: Any) -> Never:
raise ValueError("hi")
with pytest.raises(WorkerException):
@ -102,7 +92,7 @@ def test_worker_worker_exception() -> None:
def test_ignore_worker_worker_exception() -> None:
def oh_no(_):
def oh_no(_: Any) -> Never:
raise ValueError("hi")
assert (