Skip to content

Commit 4be43e2

Browse files
committed
feat: State.Builder mimic the State API to help mypy
1 parent fc9a9e6 commit 4be43e2

3 files changed

Lines changed: 23 additions & 7 deletions

File tree

statemachine/state.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ def __new__( # type: ignore [misc]
3232
return State(name=name, substates=substates, **kwargs)
3333

3434

35-
class NestedStateBuilder(metaclass=NestedStateFactory):
36-
pass
37-
38-
3935
class State:
4036
"""
4137
A State in a :ref:`StateMachine` describes a particular behavior of the machine.
@@ -116,7 +112,29 @@ class State:
116112
"""
117113

118114
class Builder(metaclass=NestedStateFactory):
119-
pass
115+
116+
# Mimic the :ref:`State` public API to help linters discover the result of the Builder
117+
# class.
118+
119+
@classmethod
120+
def to(cls, *args: "State", **kwargs) -> "TransitionList": # pragma: no cover
121+
"""Create transitions to the given target states.
122+
123+
.. note: This method is only a type hint for mypy.
124+
The actual implementation belongs to the :ref:`State` class.
125+
"""
126+
return TransitionList()
127+
128+
@classmethod
129+
def from_(
130+
cls, *args: "State", **kwargs
131+
) -> "TransitionList": # pragma: no cover
132+
"""Create transitions from the given target states (reversed).
133+
134+
.. note: This method is only a type hint for mypy.
135+
The actual implementation belongs to the :ref:`State` class.
136+
"""
137+
return TransitionList()
120138

121139
def __init__(
122140
self,

tests/examples/microwave_inheritance_machine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class on(State.Builder):
5050
cooking.to(idle, cond="open.is_active")
5151
cooking.to.itself(internal=True, on="increment_timer")
5252

53-
assert isinstance(on, State) # so mypy stop complaining
5453
turn_off = on.to(off)
5554
turn_on = off.to(on)
5655
on.to(off, cond="cook_time_is_over") # eventless transition

tests/examples/traffic_light_nested_machine.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class red(State.Builder, enter="reset_elapsed"):
2727

2828
ped_countdown = walk.to(wait) | wait.to(stop)
2929

30-
assert isinstance(red, State)
3130
timer = green.to(yellow) | yellow.to(red) | red.to(green)
3231
power_outage = red.blinking.from_()
3332
power_restored = red.from_()

0 commit comments

Comments
 (0)