Skip to content

Commit 531a5f2

Browse files
committed
feat!: change States.from_enum default to use_enum_instance=True
Fulfills the deprecation promise from v2.3.3. The enum instance is now used as the state value by default. Pass use_enum_instance=False to get the previous behavior of using the raw enum value.
1 parent f44e8ce commit 531a5f2

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

statemachine/states.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def items(self):
8383
return self._states.items()
8484

8585
@classmethod
86-
def from_enum(cls, enum_type: EnumType, initial, final=None, use_enum_instance: bool = False):
86+
def from_enum(cls, enum_type: EnumType, initial, final=None, use_enum_instance: bool = True):
8787
"""
8888
Creates a new instance of the ``States`` class from an enumeration.
8989
@@ -93,10 +93,10 @@ def from_enum(cls, enum_type: EnumType, initial, final=None, use_enum_instance:
9393
... pending = 1
9494
... completed = 2
9595
96-
A :ref:`StateMachine` that uses this enum can be declared as follows:
96+
A :ref:`StateChart` that uses this enum can be declared as follows:
9797
98-
>>> from statemachine import StateMachine
99-
>>> class ApprovalMachine(StateMachine):
98+
>>> from statemachine import StateChart
99+
>>> class ApprovalMachine(StateChart):
100100
...
101101
... _ = States.from_enum(Status, initial=Status.pending, final=Status.completed)
102102
...
@@ -107,7 +107,7 @@ def from_enum(cls, enum_type: EnumType, initial, final=None, use_enum_instance:
107107
108108
.. tip::
109109
When you assign the result of ``States.from_enum`` to a class-level variable in your
110-
:ref:`StateMachine`, you're all set. You can use any name for this variable. In this
110+
:ref:`StateChart`, you're all set. You can use any name for this variable. In this
111111
example, we used ``_`` to show that the name doesn't matter. The metaclass will inspect
112112
the variable of type :ref:`States (class)` and automatically assign the inner
113113
:ref:`State` instances to the state machine.
@@ -128,25 +128,25 @@ def from_enum(cls, enum_type: EnumType, initial, final=None, use_enum_instance:
128128
True
129129
130130
>>> sm.current_state_value
131-
2
131+
<Status.completed: 2>
132132
133-
If you need to use the enum instance as the state value, you can set the
134-
``use_enum_instance=True``:
133+
If you need to use the raw enum value instead of the enum instance, you can set
134+
``use_enum_instance=False``:
135135
136-
>>> states = States.from_enum(Status, initial=Status.pending, use_enum_instance=True)
136+
>>> states = States.from_enum(Status, initial=Status.pending, use_enum_instance=False)
137137
>>> states.completed.value
138-
<Status.completed: 2>
138+
2
139139
140-
.. deprecated:: 2.3.3
140+
.. versionchanged:: 3.0.0
141141
142-
On the next major release, ``use_enum_instance=True`` will be the default.
142+
The default changed from ``False`` to ``True``.
143143
144144
Args:
145145
enum_type: An enumeration containing the states of the machine.
146146
initial: The initial state of the machine.
147147
final: A set of final states of the machine.
148148
use_enum_instance: If ``True``, the value of the state will be the enum item instance,
149-
otherwise the enum item value. Defaults to ``False``.
149+
otherwise the enum item value. Defaults to ``True``.
150150
151151
Returns:
152152
A new instance of the :ref:`States (class)`.

tests/examples/enum_campaign_machine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class CampaignMachine(StateChart):
2727
CampaignStatus,
2828
initial=CampaignStatus.DRAFT,
2929
final=CampaignStatus.CLOSED,
30-
use_enum_instance=True,
3130
)
3231

3332
add_job = states.DRAFT.to(states.DRAFT) | states.PRODUCING.to(states.PRODUCING)

0 commit comments

Comments
 (0)