Sequence
String together Assert and Modify annotations.
Basic example
The example
from typing_exe.annotations import Sequence, Assert, Modify
from typing_exe.decorators import execute_annotations, cleanup_annotations
@cleanup_annotations
@execute_annotations
def foo(
a: Sequence[
int,
Assert[lambda a: type(a) is int, lambda a: a != 0],
Modify[lambda a: abs(a)],
Assert[lambda a: a > 5]
]
):
...
Explanation
When foo gets executed, the following will happen:
- The first
Assertwill checka's type, then check that it is not zero - The
Modifywill then return the absolute value ofa - The second
Assertwill check thatais now greater than five - The function-body will be executed
Description
The first entry to the Sequence.__getitem__-method can either be a typehint or an
Assert or Modify.
All other entries have to be either an Assert or a Modify.
Here are some legal calls to Sequence:
from typing_exe.annotations import Sequence, Assert, Modify
Sequence[int, Assert[...], Modify[...]]
Sequence[Assert[...], Modify[...]]
Sequence[Modify[...], Modify[...], Modify[...]]
Sequence[str, Modify[...]]
Sequence[Assert[...]] # not very useful