diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bearcheck/__init__.py | 4 | ||||
| -rw-r--r-- | src/bearcheck/check.py | 15 |
2 files changed, 11 insertions, 8 deletions
diff --git a/src/bearcheck/__init__.py b/src/bearcheck/__init__.py index cd44b79..84306c6 100644 --- a/src/bearcheck/__init__.py +++ b/src/bearcheck/__init__.py @@ -1,3 +1,3 @@ -from .check import bearcheck, beartest, Check +from .check import bearcheck, beartest, Check, CheckType -__all__ = ["bearcheck", "beartest", "Check"] +__all__ = ["bearcheck", "beartest", "Check", "CheckType"] diff --git a/src/bearcheck/check.py b/src/bearcheck/check.py index 82599ea..f3515e6 100644 --- a/src/bearcheck/check.py +++ b/src/bearcheck/check.py @@ -1,27 +1,30 @@ -from typing import Any, cast +from typing import Any, Generic, TypeVar, cast from beartype.door import die_if_unbearable, is_bearable -class Check[A]: # type: ignore +CheckType = TypeVar("CheckType") + + +class Check(Generic[CheckType]): # type: ignore pass -def bearcheck[T](a: Any, check: type[Check[T]]) -> T: +def bearcheck(a: Any, check: type[Check[CheckType]]) -> CheckType: args = getattr(check, "__args__", None) if args is None: if not isinstance(check, Check): raise ValueError(f"Not a valid Check class: {check!r}") raise ValueError("Check class must be templated e.g. Check[int | str]") die_if_unbearable(a, args[0]) - return cast(T, a) + return cast(CheckType, a) -def beartest[T](a: Any, check: type[Check[T]]) -> T | None: +def beartest(a: Any, check: type[Check[CheckType]]) -> CheckType | None: args = getattr(check, "__args__", None) if args is None: if not isinstance(check, Check): raise ValueError(f"Not a valid Check class: {check!r}") raise ValueError("Check class must be templated e.g. Check[int | str]") if is_bearable(a, args[0]): - return cast(T, a) + return cast(CheckType, a) return None |
