Skip to content

Commit 25ef493

Browse files
committed
test proper subclassing for hierarchicalstatemachine (#510)
1 parent a900d7d commit 25ef493

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Bugfix #512: Use `model_attribute` consistently in `AsyncMachine` (thanks @thedrow)
88
- Testing now treats most warnings as errors (thanks @thedrow)
99
- As a consequence, `pygraphviz.Agraph` in `diagrams_pygraphviz` are now copied by `transitions` since `AGraph.copy` as of version `1.6` does not close temporary files appropriately
10+
- `HierarchicalMachine` now checks whether `state_cls`, `event_cls` and `transition_cls` have been subclassed from nested base classes (e.g. `NestedState`) to prevent hard to debug inheritance errors
1011

1112
## 0.8.6 (December 2020)
1213

tests/test_nesting.py

+16
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,22 @@ def on_exit_C_1(self):
570570
model.to_C()
571571
self.assertTrue(model.is_C_1())
572572

573+
def test_correct_subclassing(self):
574+
from transitions.core import State
575+
576+
class WrongStateClass(self.machine_cls):
577+
state_cls = State
578+
579+
class MyNestedState(NestedState):
580+
pass
581+
582+
class CorrectStateClass(self.machine_cls):
583+
state_cls = MyNestedState
584+
585+
with self.assertRaises(AssertionError):
586+
m = WrongStateClass()
587+
m = CorrectStateClass()
588+
573589

574590
class TestSeparatorsBase(TestCase):
575591

tests/test_nesting_legacy.py

+3
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def test_child_condition_persistence(self):
100100
def test_get_nested_transitions(self):
101101
pass # not supported by legacy machine
102102

103+
def test_correct_subclassing(self):
104+
pass # not supported by legacy machine
105+
103106

104107
class TestReuseLegacySeparatorDefault(TestReuseSeparatorBase):
105108

transitions/extensions/nesting.py

+3
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ class HierarchicalMachine(Machine):
334334
event_cls = NestedEvent
335335

336336
def __init__(self, *args, **kwargs):
337+
assert issubclass(self.state_cls, NestedState)
338+
assert issubclass(self.event_cls, NestedEvent)
339+
assert issubclass(self.transition_cls, NestedTransition)
337340
self._stack = []
338341
self.scoped = self
339342
_super(HierarchicalMachine, self).__init__(*args, **kwargs)

0 commit comments

Comments
 (0)