1
Fork 0
mirror of https://github.com/Steffo99/async-chain.git synced 2024-11-24 22:14:21 +00:00

Add typing types

This commit is contained in:
Steffo 2021-06-14 04:39:43 +02:00
parent 82fc6a84ec
commit 221f954f2e
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 22 additions and 18 deletions

View file

@ -32,20 +32,23 @@ class Chain(metaclass=abc.ABCMeta):
raise NotImplementedError() raise NotImplementedError()
StartingType = t.TypeVar("StartingType")
class ChainStart(Chain): class ChainStart(Chain):
__slots__ = ("__start__",) __slots__ = ("__start__",)
def __init__(self, start: t.Any): def __init__(self, start: StartingType):
super().__init__() super().__init__()
self.__start__: t.Any = start self.__start__: StartingType = start
def __repr__(self): def __repr__(self) -> str:
return f"<Chain {self.__display__()}>" return f"<Chain {self.__display__()}>"
def __display__(self): def __display__(self) -> str:
return f"{self.__start__!r}" return f"{self.__start__!r}"
async def __evaluate__(self): async def __evaluate__(self) -> StartingType:
return self.__start__ return self.__start__
@ -56,7 +59,7 @@ class ChainNode(Chain, metaclass=abc.ABCMeta):
super().__init__() super().__init__()
self.__previous__: Chain = previous self.__previous__: Chain = previous
def __repr__(self): def __repr__(self) -> str:
return f"<Chain {self.__display__()}>" return f"<Chain {self.__display__()}>"
@ -67,10 +70,10 @@ class ChainGetAttr(ChainNode):
super().__init__(previous=previous) super().__init__(previous=previous)
self.__item__: str = item self.__item__: str = item
def __display__(self): def __display__(self) -> str:
return f"{self.__previous__.__display__()}.{self.__item__!s}" return f"{self.__previous__.__display__()}.{self.__item__!s}"
async def __evaluate__(self): async def __evaluate__(self) -> t.Any:
previous = await self.__previous__.__evaluate__() previous = await self.__previous__.__evaluate__()
current = getattr(previous, self.__item__) current = getattr(previous, self.__item__)
@ -88,10 +91,10 @@ class ChainGetItem(ChainNode):
super().__init__(previous=previous) super().__init__(previous=previous)
self.__item__: t.Any = item self.__item__: t.Any = item
def __display__(self): def __display__(self) -> str:
return f"{self.__previous__.__display__()}[{self.__item__!r}]" return f"{self.__previous__.__display__()}[{self.__item__!r}]"
async def __evaluate__(self): async def __evaluate__(self) -> t.Any:
previous = await self.__previous__.__evaluate__() previous = await self.__previous__.__evaluate__()
current = previous[self.__item__] current = previous[self.__item__]
@ -105,18 +108,18 @@ class ChainGetItem(ChainNode):
class ChainCall(ChainNode): class ChainCall(ChainNode):
__slots__ = ("__args__", "__kwargs__",) __slots__ = ("__args__", "__kwargs__",)
def __init__(self, previous: Chain, args, kwargs): def __init__(self, previous: Chain, args: t.Collection[t.Any], kwargs: t.Mapping[str, t.Any]):
super().__init__(previous=previous) super().__init__(previous=previous)
self.__args__ = args self.__args__: t.Collection[t.Any] = args
self.__kwargs__ = kwargs self.__kwargs__: t.Mapping[str, t.Any] = kwargs
def __display__(self): def __display__(self) -> str:
args = map(lambda a: f"{a!r}", self.__args__) args = map(lambda a: f"{a!r}", self.__args__)
kwargs = map(lambda k, v: f"{k!s}={v!r}", self.__kwargs__) kwargs = map(lambda k, v: f"{k!s}={v!r}", self.__kwargs__)
allargs = ", ".join([*args, *kwargs]) allargs: str = ", ".join([*args, *kwargs])
return f"{self.__previous__.__display__()}({allargs})" return f"{self.__previous__.__display__()}({allargs})"
async def __evaluate__(self): async def __evaluate__(self) -> t.Any:
previous = await self.__previous__.__evaluate__() previous = await self.__previous__.__evaluate__()
current = previous(*self.__args__, **self.__kwargs__) current = previous(*self.__args__, **self.__kwargs__)

View file

@ -1,4 +1,5 @@
import functools import functools
import typing as t
from .chain import ChainStart from .chain import ChainStart
@ -6,10 +7,10 @@ class FunctionWrapper:
def __init__(self, func): def __init__(self, func):
self._func = func self._func = func
def __repr__(self): def __repr__(self) -> str:
return self._func.__name__ return self._func.__name__
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs) -> t.Any:
return self._func(*args, **kwargs) return self._func(*args, **kwargs)