Merge 8c27cca65bf11e814488d3ac98849947fd5fd034 into 279133e3107392276dc509148da1f41bfb532c7e

This commit is contained in:
Steven Van Ingelgem 2024-09-17 23:40:35 +08:00 committed by GitHub
commit 2e63314696
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 8 deletions

53
tests/test_utils.py Normal file
View File

@ -0,0 +1,53 @@
import pytest
from typing import Optional
from whisper.utils import optional_float, optional_int, str2bool
@pytest.mark.parametrize(("provided", "expected"), [
("TRUE", True),
("True", True),
("true", True),
("YES", True),
("Yes", True),
("yes", True),
("Y", True),
("y", True),
("1", True),
("FALSE", False),
("False", False),
("false", False),
("NO", False),
("No", False),
("no", False),
("N", False),
("n", False),
("0", False),
])
def test_str2bool(provided: str, expected: bool) -> None:
assert str2bool(provided) is expected
def test_str2bool_faulty_argument() -> None:
with pytest.raises(ValueError, match="Expected one of"):
str2bool("boom")
@pytest.mark.parametrize(("provided", "expected"), [
("1", 1),
("None", None),
("none", None),
])
def test_optional_int(provided: str, expected: Optional[int]) -> None:
assert optional_int(provided) == expected
@pytest.mark.parametrize(("provided", "expected"), [
("1.23", 1.23),
("1", 1),
("None", None),
("none", None),
])
def test_optional_float(provided: str, expected: Optional[float]) -> None:
assert optional_float(provided) == expected

View File

@ -26,20 +26,21 @@ def exact_div(x, y):
return x // y return x // y
def str2bool(string): def str2bool(string: str) -> bool:
str2val = {"True": True, "False": False} if string.lower() in {'true', 'yes', 'y', '1'}:
if string in str2val: return True
return str2val[string] if string.lower() in {'false', 'no', 'n', '0'}:
else: return False
raise ValueError(f"Expected one of {set(str2val.keys())}, got {string}")
raise ValueError(f"Expected one of true/yes/1 or false/no/0, but got {string}")
def optional_int(string): def optional_int(string):
return None if string == "None" else int(string) return None if string.lower() == "none" else int(string)
def optional_float(string): def optional_float(string):
return None if string == "None" else float(string) return None if string.lower() == "none" else float(string)
def compression_ratio(text) -> float: def compression_ratio(text) -> float: