From 3e22b58e1e2e64fcacf84881674f0999d46a5a70 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 7 May 2024 18:43:36 +0200 Subject: [PATCH 1/2] fix may_trigger for nested and async see #594 --- tests/test_async.py | 15 +++++++++++++++ tests/test_nesting.py | 3 +++ transitions/core.py | 2 +- transitions/extensions/asyncio.py | 4 ++-- transitions/extensions/nesting.py | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/tests/test_async.py b/tests/test_async.py index 8957e2b5..4166475b 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -498,6 +498,21 @@ async def run(): asyncio.run(run()) + def test_may_transition_internal(self): + states = ['A', 'B', 'C'] + d = DummyModel() + _ = self.machine_cls(model=d, states=states, transitions=[["go", "A", "B"], ["wait", "B", None]], + initial='A', auto_transitions=False) + + async def run(): + assert await d.may_go() + assert not await d.may_wait() + await d.go() + assert not await d.may_go() + assert await d.may_wait() + + asyncio.run(run()) + @skipIf(asyncio is None or (pgv is None and gv is None), "AsyncGraphMachine requires asyncio and (py)gaphviz") class TestAsyncGraphMachine(TestAsync): diff --git a/tests/test_nesting.py b/tests/test_nesting.py index a982aba0..2a29af0a 100644 --- a/tests/test_nesting.py +++ b/tests/test_nesting.py @@ -910,6 +910,7 @@ def test_machine_may_transitions(self): transitions = [ {'trigger': 'walk', 'source': 'A', 'dest': 'B'}, {'trigger': 'run', 'source': 'B', 'dest': 'C'}, + {'trigger': 'wait', 'source': 'B', 'dest': None}, {'trigger': 'run_fast', 'source': 'C', 'dest': 'C{0}1'.format(self.separator)}, {'trigger': 'sprint', 'source': 'C', 'dest': 'D'} ] @@ -920,11 +921,13 @@ def test_machine_may_transitions(self): assert not m.may_run() assert not m.may_run_fast() assert not m.may_sprint() + assert not m.may_wait() m.walk() assert not m.may_walk() assert m.may_run() assert not m.may_run_fast() + assert m.may_wait() m.run() assert m.may_run_fast() diff --git a/transitions/core.py b/transitions/core.py index 1c6c5786..a1968526 100644 --- a/transitions/core.py +++ b/transitions/core.py @@ -884,7 +884,7 @@ def _can_trigger(self, model, trigger, *args, **kwargs): continue for transition in self.events[trigger_name].transitions[state]: try: - _ = transition.source if transition.dest is None else self.get_state(transition.dest) + _ = self.get_state(transition.dest) if transition.dest is not None else transition.source except ValueError: continue diff --git a/transitions/extensions/asyncio.py b/transitions/extensions/asyncio.py index 7785743a..7f46db28 100644 --- a/transitions/extensions/asyncio.py +++ b/transitions/extensions/asyncio.py @@ -433,7 +433,7 @@ async def _can_trigger(self, model, trigger, *args, **kwargs): continue for transition in self.events[trigger_name].transitions[state]: try: - _ = self.get_state(transition.dest) + _ = self.get_state(transition.dest) if transition.dest is not None else transition.source except ValueError: continue await self.callbacks(self.prepare_event, evt) @@ -559,7 +559,7 @@ async def _can_trigger_nested(self, model, trigger, path, *args, **kwargs): state_name = self.state_cls.separator.join(source_path) for transition in self.events[trigger].transitions.get(state_name, []): try: - _ = self.get_state(transition.dest) + _ = self.get_state(transition.dest) if transition.dest is not None else transition.source except ValueError: continue await self.callbacks(self.prepare_event, evt) diff --git a/transitions/extensions/nesting.py b/transitions/extensions/nesting.py index 9c759453..3935bfae 100644 --- a/transitions/extensions/nesting.py +++ b/transitions/extensions/nesting.py @@ -686,7 +686,7 @@ def _can_trigger_nested(self, model, trigger, path, *args, **kwargs): state_name = self.state_cls.separator.join(source_path) for transition in self.events[trigger].transitions.get(state_name, []): try: - _ = self.get_state(transition.dest) + _ = self.get_state(transition.dest) if transition.dest is not None else transition.source except ValueError: continue self.callbacks(self.prepare_event, evt) From 2f903fee42d6225ac80f7ec5bff49381b51769f0 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Tue, 7 May 2024 18:43:51 +0200 Subject: [PATCH 2/2] slightly improve typing --- Changelog.md | 3 +++ transitions/core.pyi | 4 ++-- transitions/extensions/nesting.pyi | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Changelog.md b/Changelog.md index 7e268b55..97624169 100644 --- a/Changelog.md +++ b/Changelog.md @@ -9,6 +9,9 @@ - '_anchor' suffix has been removed for (py)graphviz cluster node anchors - local testing switched from [tox](https://github.com/tox-dev/tox) to [nox](https://github.com/wntrblm/nox) - PR #633: Remove surrounding whitespace from docstrings (thanks @artofhuman) +- Typing: + + Made machine property mandatory in (Nested)EventData + + Transition.dest may be None ## 0.9.0 (September 2022) diff --git a/transitions/core.pyi b/transitions/core.pyi index 390faf6e..447d48b1 100644 --- a/transitions/core.pyi +++ b/transitions/core.pyi @@ -51,7 +51,7 @@ class Transition: dynamic_methods: List[str] condition_cls: Type[Condition] source: str - dest: str + dest: Optional[str] prepare: CallbackList before: CallbackList after: CallbackList @@ -70,7 +70,7 @@ TransitionConfig = Union[Sequence[Union[str, Any]], Dict[str, Any], Transition] class EventData: state: Optional[State] event: Optional[Event] - machine: Optional[Machine] + machine: Machine model: object args: Iterable[Any] kwargs: Dict[str, Any] diff --git a/transitions/extensions/nesting.pyi b/transitions/extensions/nesting.pyi index 70ba2377..64f9f6aa 100644 --- a/transitions/extensions/nesting.pyi +++ b/transitions/extensions/nesting.pyi @@ -23,7 +23,7 @@ class NestedEvent(Event): class NestedEventData(EventData): state: Optional[NestedState] event: Optional[NestedEvent] - machine: Optional[HierarchicalMachine] + machine: HierarchicalMachine transition: Optional[NestedTransition] source_name: Optional[str] source_path: Optional[List[str]]