Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with may_<strigger> for nested and async machines and slightly improve typing #662

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
]
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion transitions/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions transitions/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions transitions/extensions/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion transitions/extensions/nesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion transitions/extensions/nesting.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down