feat: add map(), and_then(), or_else() in UniqueType
This commit is contained in:
parent
e065b51490
commit
346140d675
1 changed files with 38 additions and 1 deletions
|
|
@ -1,6 +1,8 @@
|
|||
from typing import Any, TypeVar, final
|
||||
from typing import Any, Callable, TypeGuard, TypeVar, final
|
||||
|
||||
T = TypeVar("T")
|
||||
V = TypeVar("V")
|
||||
R = TypeVar("R")
|
||||
|
||||
|
||||
@final
|
||||
|
|
@ -36,6 +38,41 @@ class UniqueType(type):
|
|||
def __call__(cls: type[T]) -> T:
|
||||
return cls.__new__(cls) # noqa: E1120 # pylint: disable=E1120
|
||||
|
||||
def __hash__(cls) -> int:
|
||||
return id(cls)
|
||||
|
||||
def itis(cls: type[T], value: Any) -> TypeGuard[T]:
|
||||
return value is cls or isinstance(value, cls)
|
||||
|
||||
def unwrap(cls: type[T], value: V | type[T] | T, /) -> V:
|
||||
if value is cls or isinstance(value, cls):
|
||||
raise TypeError(f"Expected {cls}, got {value}")
|
||||
return value # type: ignore
|
||||
|
||||
def map(
|
||||
cls: type[T],
|
||||
value: V | type[T] | T,
|
||||
and_then: Callable[[V], R],
|
||||
or_else: Callable[[type[T]], R],
|
||||
) -> R:
|
||||
if value is cls or isinstance(value, cls):
|
||||
return and_then(value) # type: ignore
|
||||
return or_else(cls)
|
||||
|
||||
def and_then(
|
||||
cls: type[T], value: V | type[T] | T, func: Callable[[V], R]
|
||||
) -> type[T] | R:
|
||||
if value is cls or isinstance(value, cls):
|
||||
return cls
|
||||
return func(value) # type: ignore
|
||||
|
||||
def or_else(
|
||||
cls: type[T], value: V | type[T] | T, func: Callable[[type[T]], R]
|
||||
) -> R | V:
|
||||
if value is cls or isinstance(value, cls):
|
||||
return func(cls)
|
||||
return value # type: ignore
|
||||
|
||||
|
||||
class Unique(metaclass=UniqueType): # noqa: R0903 # pylint: disable=R0903
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue