Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Release Versions:
- [2.1.1](#211)
- [2.1.0](#210)

## Upcoming changes

- feat(components): verify return value of callbacks (#206)

## 5.2.0

### March 31st, 2025
Expand Down
17 changes: 11 additions & 6 deletions source/modulo_components/modulo_components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, node_name: str, *args, **kwargs):
self.add_predicate("is_finished", False)
self.add_predicate("in_error_state", False)

def _step(self):
def _step(self) -> None:
"""
Step function that is called periodically.
"""
Expand All @@ -38,7 +38,7 @@ def _step(self):
self.get_logger().error(f"Failed to execute step function: {e}", throttle_duration_sec=1.0)
self.raise_error()

def execute(self):
def execute(self) -> None:
"""
Start the execution thread.
"""
Expand All @@ -49,13 +49,18 @@ def execute(self):
self.__execute_thread = Thread(target=self.__on_execute)
self.__execute_thread.start()

def __on_execute(self):
def __on_execute(self) -> None:
"""
Run the execution function in a try catch block and set the predicates according to the outcome of the
execution.
"""
try:
if not self.on_execute_callback():
result = self.on_execute_callback()
if result is None:
self.get_logger().error("Expected a return value from 'on_execute_callback'.")
self.raise_error()
return
if not result:
self.raise_error()
return
except Exception as e:
Expand All @@ -75,7 +80,7 @@ def on_execute_callback(self) -> bool:

def add_output(self, signal_name: str, data: str, message_type: MsgT,
clproto_message_type: Optional[clproto.MessageType] = None, default_topic="", fixed_topic=False,
publish_on_step=True):
publish_on_step=True) -> None:
"""
Add and configure an output signal of the component.

Expand All @@ -97,7 +102,7 @@ def add_output(self, signal_name: str, data: str, message_type: MsgT,
except Exception as e:
self.get_logger().error(f"Failed to add output '{signal_name}': {e}")

def raise_error(self):
def raise_error(self) -> None:
"""
Set the in_error_state predicate to true and cancel the step timer.
"""
Expand Down
Loading