Skip to content
Open
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
7 changes: 7 additions & 0 deletions docs/releases/3.3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Deprecated APIs are removed in 4.0. Nothing is deprecated in 3.3.0 yet.

## Bug fixes in 3.3.0

### States and events named `prepare`

`prepare` can now be used as a state or event name on both engines. It no longer
overwrites the internal callback group used by `prepare_event()`.

[#734](https://github.com/fgmacedo/python-statemachine/issues/734).

(release-3.3.0-history)=

### Entering a history state by default
Expand Down
2 changes: 1 addition & 1 deletion statemachine/engines/async_.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ async def _get_args_kwargs(

args, kwargs = event_data.args, event_data.extended_kwargs

result = await self.sm._callbacks.async_call(self.sm.prepare.key, *args, **kwargs)
result = await self.sm._callbacks.async_call(self.sm._prepare.key, *args, **kwargs)
for new_kwargs in result:
kwargs.update(new_kwargs)

Expand Down
2 changes: 1 addition & 1 deletion statemachine/engines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def _get_args_kwargs(

args, kwargs = event_data.args, event_data.extended_kwargs

result = self.sm._callbacks.call(self.sm.prepare.key, *args, **kwargs)
result = self.sm._callbacks.call(self.sm._prepare.key, *args, **kwargs)
for new_kwargs in result:
kwargs.update(new_kwargs)

Expand Down
2 changes: 1 addition & 1 deletion statemachine/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(
cls._protected_attrs: set = set()
cls._events_to_update: dict[Event, list[Event]] = {}
cls._specs = CallbackSpecList()
cls.prepare = cls._specs.grouper(CallbackGroup.PREPARE).add(
cls._prepare = cls._specs.grouper(CallbackGroup.PREPARE).add(
"prepare_event", priority=CallbackPriority.GENERIC, is_convention=True
)
cls.add_inherited(bases)
Expand Down
2 changes: 1 addition & 1 deletion statemachine/statemachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class StateChart(Generic[TModel], metaclass=StateMachineMetaclass):
_protected_attrs: set
_specs: CallbackSpecList
_class_listeners: list[Any]
prepare: SpecListGrouper
_prepare: SpecListGrouper

def __init__(
self,
Expand Down
37 changes: 37 additions & 0 deletions tests/test_statemachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,43 @@ class CampaignMachine(StateChart):
deliver = producing.to(closed)


async def test_state_named_prepare(sm_runner):
class Workflow(StateChart):
prepare = State(initial=True)
done = State(final=True)

finish = prepare.to(done)

def prepare_event(self, value=0):
return {"prepared_value": value + 1}

def on_finish(self, prepared_value):
return prepared_value

sm = await sm_runner.start(Workflow)
assert sm.prepare.is_active

assert await sm_runner.send(sm, "finish", value=41) == 42
assert sm.done.is_active


async def test_event_named_prepare(sm_runner):
class Workflow(StateChart):
ready = State(initial=True)
done = State(final=True)

prepare = ready.to(done)

def on_prepare(self):
return 42

sm = await sm_runner.start(Workflow)
assert sm.ready.is_active

assert await sm_runner.send(sm, "prepare") == 42
assert sm.done.is_active


def test_machine_should_activate_initial_state(mocker):
spy = mocker.Mock()

Expand Down