diff options
| author | Louis Burda <dev@sinitax.com> | 2025-11-12 09:41:37 +0100 |
|---|---|---|
| committer | Louis Burda <dev@sinitax.com> | 2025-11-12 09:41:37 +0100 |
| commit | 38bfc3fb54021c9e9a4e9c7bd02d686c873aa134 (patch) | |
| tree | a7095137819228c78b203c58d0fe2913864a8056 /src | |
| parent | 12688e0a31438a7da16ba1831ab3d4c68c6e19c4 (diff) | |
| download | bearcheck-main.tar.gz bearcheck-main.zip | |
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 |
