Skip to content

Commit 3fef20a

Browse files
committed
feat: State.Builder mimic the State API to help mypy
1 parent 5307809 commit 3fef20a

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

statemachine/state.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ def __new__( # type: ignore [misc]
2727
return State(name=name, substates=substates, **kwargs)
2828

2929

30-
class NestedStateBuilder(metaclass=NestedStateFactory):
31-
pass
32-
33-
3430
class State:
3531
"""
3632
A State in a state machine describes a particular behavior of the machine.
@@ -111,7 +107,29 @@ class State:
111107
"""
112108

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

116134
def __init__(
117135
self,

tests/examples/microwave_inheritance_machine.py

-1
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

-1
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)