Skip to content

Commit 2635eb1

Browse files
committed
Validate trigger
1 parent e19f30e commit 2635eb1

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

Diff for: commands2/button/trigger.py

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# validated: 2024-01-23 DS 70b60e3a7465 button/Trigger.java
12
from types import SimpleNamespace
23
from typing import Callable, overload
34

@@ -44,6 +45,7 @@ def __init__(self, loop: EventLoop, condition: Callable[[], bool]):
4445

4546
def __init__(self, *args, **kwargs):
4647
def init_loop_condition(loop: EventLoop, condition: Callable[[], bool]):
48+
assert callable(condition)
4749
self._loop = loop
4850
self._condition = condition
4951

@@ -77,7 +79,7 @@ def init_condition(condition: Callable[[], bool]):
7779
1. (self: Trigger)
7880
2. (self: Trigger, condition: () -> bool)
7981
3. (self: Trigger, loop: EventLoop, condition: () -> bool)
80-
82+
8183
Invoked with: {format_args_kwargs(self, *args, **kwargs)}
8284
"""
8385
)
@@ -90,8 +92,10 @@ def onTrue(self, command: Command) -> Self:
9092
:returns: this trigger, so calls can be chained
9193
"""
9294

95+
state = SimpleNamespace(pressed_last=self._condition())
96+
9397
@self._loop.bind
94-
def _(state=SimpleNamespace(pressed_last=self._condition())):
98+
def _():
9599
pressed = self._condition()
96100
if not state.pressed_last and pressed:
97101
command.schedule()
@@ -107,8 +111,10 @@ def onFalse(self, command: Command) -> Self:
107111
:returns: this trigger, so calls can be chained
108112
"""
109113

114+
state = SimpleNamespace(pressed_last=self._condition())
115+
110116
@self._loop.bind
111-
def _(state=SimpleNamespace(pressed_last=self._condition())):
117+
def _():
112118
pressed = self._condition()
113119
if state.pressed_last and not pressed:
114120
command.schedule()
@@ -122,14 +128,16 @@ def whileTrue(self, command: Command) -> Self:
122128
changes to `False`.
123129
124130
Doesn't re-start the command if it ends while the condition is still `True`. If the command
125-
should restart, see RepeatCommand.
131+
should restart, see :class:`commands2.RepeatCommand`.
126132
127133
:param command: the command to start
128134
:returns: this trigger, so calls can be chained
129135
"""
130136

137+
state = SimpleNamespace(pressed_last=self._condition())
138+
131139
@self._loop.bind
132-
def _(state=SimpleNamespace(pressed_last=self._condition())):
140+
def _():
133141
pressed = self._condition()
134142
if not state.pressed_last and pressed:
135143
command.schedule()
@@ -145,14 +153,16 @@ def whileFalse(self, command: Command) -> Self:
145153
condition changes to `True`.
146154
147155
Doesn't re-start the command if it ends while the condition is still `False`. If the command
148-
should restart, see RepeatCommand.
156+
should restart, see :class:`commands2.RepeatCommand`.
149157
150158
:param command: the command to start
151159
:returns: this trigger, so calls can be chained
152160
"""
153161

162+
state = SimpleNamespace(pressed_last=self._condition())
163+
154164
@self._loop.bind
155-
def _(state=SimpleNamespace(pressed_last=self._condition())):
165+
def _():
156166
pressed = self._condition()
157167
if state.pressed_last and not pressed:
158168
command.schedule()
@@ -170,8 +180,10 @@ def toggleOnTrue(self, command: Command) -> Self:
170180
:returns: this trigger, so calls can be chained
171181
"""
172182

183+
state = SimpleNamespace(pressed_last=self._condition())
184+
173185
@self._loop.bind
174-
def _(state=SimpleNamespace(pressed_last=self._condition())):
186+
def _():
175187
pressed = self._condition()
176188
if not state.pressed_last and pressed:
177189
if command.isScheduled():
@@ -190,8 +202,10 @@ def toggleOnFalse(self, command: Command) -> Self:
190202
:returns: this trigger, so calls can be chained
191203
"""
192204

205+
state = SimpleNamespace(pressed_last=self._condition())
206+
193207
@self._loop.bind
194-
def _(state=SimpleNamespace(pressed_last=self._condition())):
208+
def _():
195209
pressed = self._condition()
196210
if state.pressed_last and not pressed:
197211
if command.isScheduled():
@@ -212,6 +226,7 @@ def __bool__(self) -> bool:
212226
return self._condition()
213227

214228
def __and__(self, other: Callable[[], bool]) -> "Trigger":
229+
assert callable(other)
215230
return Trigger(lambda: self() and other())
216231

217232
def and_(self, other: Callable[[], bool]) -> "Trigger":
@@ -224,6 +239,7 @@ def and_(self, other: Callable[[], bool]) -> "Trigger":
224239
return self & other
225240

226241
def __or__(self, other: Callable[[], bool]) -> "Trigger":
242+
assert callable(other)
227243
return Trigger(lambda: self() or other())
228244

229245
def or_(self, other: Callable[[], bool]) -> "Trigger":

0 commit comments

Comments
 (0)