progpy icon indicating copy to clipboard operation
progpy copied to clipboard

Check custom functions return expected type

Open lymichelle21 opened this issue 11 months ago • 1 comments

** Test to Update ** Add tests to check if a custom functions the user passes in return a bool (e.g., event_strategy). If custom function does not return a bool, show a warning noting potential unanticipated behavior.

lymichelle21 avatar Apr 03 '25 22:04 lymichelle21

  1. Add a Helper Function to Validate Return Type

progpy/utils/validators.py

from typing import Callable

def validate_bool_return(func: Callable) -> Callable: """ Decorator to ensure the function returns a boolean value. """ def wrapper(*args, **kwargs): result = func(*args, **kwargs) if not isinstance(result, bool): raise ValueError(f"Function {func.name} must return a boolean value.") return result return wrapper

  1. Apply the Decorator to Custom Functions

Example usage in a model or algorithm

from progpy.utils.validators import validate_bool_return

class CustomModel: @validate_bool_return def event_strategy(self, state): # Custom logic return True # Ensure this returns a boolean

  1. Explanation

What this does: Ensures that custom functions like event_strategy return a boolean value, preventing potential runtime errors.

Why this fixes it: By enforcing a boolean return type, we maintain consistency and prevent unexpected behavior in the system.

Next steps: Apply this decorator to all relevant custom functions within the codebase.