diff --git a/src/pynyaa/_enums.py b/src/pynyaa/_enums.py index f8ddeaf..1ad0daf 100644 --- a/src/pynyaa/_enums.py +++ b/src/pynyaa/_enums.py @@ -6,7 +6,7 @@ from pynyaa._compat import IntEnum, StrEnum from pynyaa._types import CategoryID, CategoryLiteral, SortByLiteral -from pynyaa._utils import get_category_id_from_name +from pynyaa._utils import _get_category_id_from_name class BaseStrEnum(StrEnum): @@ -17,7 +17,7 @@ def _missing_(cls, value: object) -> Self: for member in cls: if member.value.casefold() == str(value).casefold(): return member - message = f"'{value}' is not a valid {type(cls)}" + message = f"'{value}' is not a valid {cls.__name__}" raise ValueError(message) @@ -63,7 +63,7 @@ def id(self) -> CategoryID: This ID corresponds to the category as seen in the URL `https://nyaa.si/?f=0&c=1_2&q=`, where `c=1_2` is the ID for `Anime - English-translated`. """ - return get_category_id_from_name(self.value) + return _get_category_id_from_name(self.value) @overload @classmethod diff --git a/src/pynyaa/_utils.py b/src/pynyaa/_utils.py index 4f78984..cadd299 100644 --- a/src/pynyaa/_utils.py +++ b/src/pynyaa/_utils.py @@ -14,6 +14,8 @@ P = ParamSpec("P") T = TypeVar("T") + # functools.cache destroys the signature of whatever it wraps, so we use this to fix it. + # This is to only "fool" typecheckers and IDEs, this doesn't exist at runtime. def cache(user_function: Callable[P, T], /) -> Callable[P, T]: ... # type: ignore @@ -22,16 +24,16 @@ def get_user_cache_path() -> Path: @overload -def get_category_id_from_name(key: CategoryName) -> CategoryID: ... +def _get_category_id_from_name(key: CategoryName) -> CategoryID: ... @overload -def get_category_id_from_name(key: str) -> CategoryID: ... +def _get_category_id_from_name(key: str) -> CategoryID: ... @cache -def get_category_id_from_name(key: CategoryName | str) -> str: - mapping = { +def _get_category_id_from_name(key: CategoryName | str) -> CategoryID: + mapping: dict[str, CategoryID] = { # All, c=0_0 "All": "0_0", # Anime, c=1_X diff --git a/tests/test_enums.py b/tests/test_enums.py index 2d22e55..1089644 100644 --- a/tests/test_enums.py +++ b/tests/test_enums.py @@ -5,6 +5,16 @@ from pynyaa import Category, Filter, SortBy +def test_category_value_error() -> None: + with pytest.raises(ValueError): + Category.get("asdadadsad", "invalid default") + + +def test_sortby_value_error() -> None: + with pytest.raises(ValueError): + SortBy.get("asdadadsad", "invalid default") + + @pytest.mark.parametrize( "category, expected_id", [