Skip to content

Add descriptive ValueError when no operators are added to ComponentGenerator#91

Merged
gbomarito merged 2 commits into
nasa:expressionsfrom
karlhillx:fix/descriptive-no-operators-error
Jun 23, 2026
Merged

Add descriptive ValueError when no operators are added to ComponentGenerator#91
gbomarito merged 2 commits into
nasa:expressionsfrom
karlhillx:fix/descriptive-no-operators-error

Conversation

@karlhillx

Copy link
Copy Markdown

Fixes #90.

Problem

When ComponentGenerator is used without calling add_operator(), random_operator() raises a cryptic IndexError: list index out of range from deep in ProbabilityMassFunction.draw_sample(). The error gives users no indication of what went wrong or how to fix it.

Fix

Add a guard in random_operator() that checks if _operator_pmf.items is empty and raises a descriptive ValueError instead:

ValueError: No operators have been added to this ComponentGenerator. Call add_operator() before generating graphs.

Since random_operator() is the single choke point for all operator-drawing paths (direct calls, random_operator_command(), and random_command() → PMF → random_operator_command()), the guard catches all entry paths with one check.

Tests

Added TestNoOperatorsError class with three tests:

  • test_random_operator_raises_without_operators — direct call to random_operator()
  • test_random_operator_command_raises_without_operators — via random_operator_command()
  • test_generator_call_raises_without_operators — end-to-end replication via AGraphGenerator.__call__

All 31 tests in test_component_generator.py pass. The 2 pre-existing failures in test_expression.py (sklearn __sklearn_tags__ compatibility) are unrelated to this change.

…nerator

Fixes nasa#90.

When ComponentGenerator is used without calling add_operator(),
random_operator() raises a cryptic IndexError from deep in
ProbabilityMassFunction.draw_sample(). This adds a guard in
random_operator() that raises a descriptive ValueError instead,
pointing the user to call add_operator() before generating graphs.

Since random_operator() is the single choke point for all operator-
drawing paths (direct calls, random_operator_command(), and
random_command() via PMF), the guard catches all entry paths with
one check.
Copilot AI review requested due to automatic review settings June 22, 2026 23:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves the usability of AGraph expression generation by replacing a cryptic IndexError (triggered when no operators are configured) with a clear, actionable ValueError from ComponentGenerator.random_operator(), and adds regression tests for the affected call paths.

Changes:

  • Add an explicit guard in ComponentGenerator.random_operator() that raises a descriptive ValueError when no operators have been added.
  • Extend the random_operator() docstring with a Raises section documenting the new behavior.
  • Add unit tests covering direct operator selection, operator-command generation, and end-to-end generator invocation when no operators are configured.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
bingo/expressions/agraph/component_generator.py Adds the empty-operator guard and documents the new exception behavior.
tests/unit/expressions/agraph/test_component_generator.py Adds regression tests ensuring all entry paths raise the descriptive ValueError when no operators are configured.

Comment thread tests/unit/expressions/agraph/test_component_generator.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@gbomarito gbomarito merged commit 4161d62 into nasa:expressions Jun 23, 2026
3 of 5 checks passed
@gbomarito

Copy link
Copy Markdown
Collaborator

Thanks for the work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants