Skip to content

Commit

Permalink
feat(experimental): removed expect_override (#676) and added `trigg…
Browse files Browse the repository at this point in the history
…er` and `with_trigger_decorator`

expect_override (introduced recently in #676) was revoked as a heterogenous model structure is not to be expecred
  • Loading branch information
aleneum committed Jun 12, 2024
1 parent 7bc12dd commit 14ae332
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
36 changes: 33 additions & 3 deletions transitions/experimental/decoration.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
def expect_override(func):
setattr(func, "expect_override", True)
return func
from transitions.core import listify
from collections import deque

_attribute_name = "transition_definitions"


def with_trigger_decorator(cls):
add_model = getattr(cls, "add_model")

def add_model_override(self, model, initial=None) -> None:
self.model_override = True
for model in listify(model):
model = self if model == "self" else model
for name, method in model.__class__.__dict__.items():
for spec in getattr(method, _attribute_name, []):
self.add_transition(name, **spec)
add_model(self, model, initial)

setattr(cls, 'add_model', add_model_override)
return cls


def trigger(source, dest, conditions=None, unless=None, before=None, after=None, prepare=None):
def _outer(trigger_func):
if not hasattr(trigger_func, _attribute_name):
setattr(trigger_func, _attribute_name, deque())
getattr(trigger_func, _attribute_name).appendleft({
"source": source, "dest": dest, "conditions": conditions, "unless": unless, "before": before,
"after": after, "prepare": prepare
})
return trigger_func

return _outer
11 changes: 8 additions & 3 deletions transitions/experimental/decoration.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from typing import Callable, ParamSpec
from typing import Union, Type, Callable
from transitions.core import StateIdentifier, CallbacksArg, CallbackFunc, Machine

P = ParamSpec("P")

def with_trigger_decorator(cls: Type[Machine]) -> Type[Machine]: ...

def expect_override(func: Callable[P, bool | None]) -> Callable[P, bool | None]: ...
def trigger(source: Union[StateIdentifier, list[StateIdentifier]],
dest: StateIdentifier | None = ...,
conditions: CallbacksArg = ..., unless: CallbacksArg = ...,
before: CallbacksArg = ..., after: CallbacksArg = ...,
prepare: CallbacksArg = ...) -> Callable[[CallbackFunc], CallbackFunc]: ...

0 comments on commit 14ae332

Please sign in to comment.