pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.49k stars 524 forks source link

Make counter example use `HierarchicalMachine` explicitly to be more clear #537

Closed MicahLyle closed 3 years ago

MicahLyle commented 3 years ago

If you run it with the regular Machine (imported from transitions) you'll get this error:

In [14]: collector = Machine(states=states, transitions=transitions, initial='waiting')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-cb0d383f0648> in <module>
----> 1 collector = Machine(states=states, transitions=transitions, initial='waiting')

~/.virtualenvs/jst/lib/python3.9/site-packages/transitions/core.py in __init__(self, model, states, initial, transitions, send_event, auto_transitions, ordered_transitions, ignore_invalid_triggers, before_state_change, after_state_change, name, queued, prepare_event, finalize_event, model_attribute, on_exception, **kwargs)
    577
    578         if states is not None:
--> 579             self.add_states(states)
    580
    581         if initial is not None:

~/.virtualenvs/jst/lib/python3.9/site-packages/transitions/core.py in add_states(self, states, on_enter, on_exit, ignore_invalid_triggers, **kwargs)
    806                 if 'ignore_invalid_triggers' not in state:
    807                     state['ignore_invalid_triggers'] = ignore
--> 808                 state = self._create_state(**state)
    809             self.states[state.name] = state
    810             for model in self.models:

~/.virtualenvs/jst/lib/python3.9/site-packages/transitions/core.py in _create_state(cls, *args, **kwargs)
    638     @classmethod
    639     def _create_state(cls, *args, **kwargs):
--> 640         return cls.state_cls(*args, **kwargs)
    641
    642     @property

TypeError: __init__() got an unexpected keyword argument 'children'
aleneum commented 3 years ago

Hello @MicahLyle,

I guess for comprehensibility's sake it is better to use the actual machine classes (e.g. LockedMachine or HierarchicalMachine) in examples instead of importing them as Machine. Thanks for the PR!

coveralls commented 3 years ago

Coverage Status

Coverage remained the same at 98.773% when pulling 700e1c7c7897ac50913a26a9e8b0685254af27ae on MicahLyle:readme-counter-example-hierarchical-machine-more-clear into 203fa8fc898d916d87b71527301a4c1294858789 on pytransitions:master.