Skip to content

Commit ec225e0

Browse files
authored
fix: emit DeprecationWarning on current_state setter (#604) (#605)
1 parent ee3607a commit ec225e0

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

docs/releases/3.1.0.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,9 @@ machine instance concurrently. This is now documented in the
165165
[#601](https://github.com/fgmacedo/python-statemachine/pull/601),
166166
fixes [#600](https://github.com/fgmacedo/python-statemachine/issues/600).
167167

168+
- `current_state` setter now emits `DeprecationWarning` consistently with the getter.
169+
Previously only reading `current_state` triggered the warning; assigning to it was silent.
170+
The docstring also now includes a `deprecated` directive for Sphinx autodoc.
171+
[#604](https://github.com/fgmacedo/python-statemachine/issues/604).
172+
168173
## Misc in 3.1.0

statemachine/statemachine.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ def current_state_value(self, value):
395395
def current_state(self) -> "State | MutableSet[State]":
396396
"""Get/Set the current :ref:`state`.
397397
398-
This is a low level API, that can be to assign any valid state
399-
completely bypassing all the hooks and validations.
398+
.. deprecated:: 3.0.0
399+
Use :attr:`configuration` / :attr:`configuration_values` instead.
400400
"""
401401
warnings.warn(
402402
"""Property `current_state` is deprecated in favor of `configuration`.""",
@@ -406,7 +406,12 @@ def current_state(self) -> "State | MutableSet[State]":
406406
return self._config.current_state
407407

408408
@current_state.setter
409-
def current_state(self, value): # pragma: no cover
409+
def current_state(self, value):
410+
warnings.warn(
411+
"""Property `current_state` is deprecated in favor of `configuration`.""",
412+
DeprecationWarning,
413+
stacklevel=2,
414+
)
410415
self.current_state_value = value.value
411416

412417
@property

tests/test_statemachine_compat.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,28 @@ class SM(StateMachine):
356356
sm = SM()
357357
with pytest.warns(DeprecationWarning, match="current_state"):
358358
_ = sm.current_state # noqa: F841
359+
360+
def test_current_state_setter_emits_warning(self):
361+
class SM(StateMachine):
362+
s1 = State(initial=True)
363+
s2 = State(final=True)
364+
365+
go = s1.to(s2)
366+
367+
sm = SM()
368+
with pytest.warns(DeprecationWarning, match="current_state"):
369+
sm.current_state = sm.s2
370+
assert sm.s2 in sm.configuration
371+
372+
def test_current_state_setter_changes_state(self):
373+
class SM(StateMachine):
374+
s1 = State(initial=True)
375+
s2 = State(final=True)
376+
377+
go = s1.to(s2)
378+
379+
sm = SM()
380+
with warnings.catch_warnings():
381+
warnings.simplefilter("ignore", DeprecationWarning)
382+
sm.current_state = sm.s2
383+
assert sm.s2 in sm.configuration

0 commit comments

Comments
 (0)