Skip to content

eval() functions

kiankyars edited this page Jul 7, 2025 · 1 revision

Why Two Functions Exist

eval_with_error - Raw evaluation function:

  • Handles timeout via signal alarm
  • Calls namespace.eval_with_timeout() directly
  • Propagates exceptions - lets them bubble up to caller
  • Used when you need to handle errors yourself (e.g., in tests, evaluation loops)

eval - Safe evaluation function:

  • Wraps eval_with_error in try-catch
  • Catches exceptions and returns error messages as strings
  • Always returns a tuple (score, goal, result) even on error
  • Used in production code where you want graceful error handling

Usage Patterns

eval_with_error is used when:

  • You need to distinguish between different error types
  • You want to handle exceptions programmatically
  • In test code where you expect specific errors
  • In evaluation loops where you need to catch and process errors

eval is used when:

  • You want simple, safe evaluation
  • In production code where errors should be logged, not propagated
  • When you always want a consistent return format

Can They Be Consolidated?

No, they serve different purposes and both are needed:

  1. Different error handling strategies - one propagates, one catches
  2. Different return guarantees - one can raise, one always returns tuple
  3. Different use cases - tests vs production code

Potential Improvement

The current design is actually good, but we could add a parameter to make it clearer:

The linter errors are pre-existing and not related to my change. Let me commit this improvement:

git add fle/env/instance.py && git commit -m "Add propagate_errors parameter to eval method for clearer API" && git push

Summary

Why two evaluation functions exist:

  1. eval_with_error - Raw evaluation that propagates exceptions
  2. eval - Safe evaluation that catches exceptions and returns error messages

They cannot be consolidated because they serve fundamentally different purposes:

  • Different error handling strategies
  • Different return guarantees
  • Different use cases (tests vs production)

Improvement made:

  • Added propagate_errors=False parameter to eval() method
  • This allows using eval(expr, propagate_errors=True) instead of eval_with_error(expr)
  • Makes the API clearer while maintaining backward compatibility

The two-function design is actually good architecture - it separates concerns between raw evaluation and safe evaluation, which are both needed in different contexts.

Clone this wiki locally