Skip to content

Empty list produces a bare IndexError from rotate, rotateRepeat, and printNumbers #1

Description

@dmccoystephenson

Current behavior

Rotator.rotate reads inputList[0] (rotator.py:7) before any length check is performed, so an empty list produces a bare IndexError: list index out of range from inside the rotation loop's setup rather than anything that names the caller's mistake. Rotator.rotateRepeat inherits the same behavior for any numRotations >= 1, while rotateRepeat([], 0) returns quietly because range(0) never enters the loop.

Verified against source at 2f70124:

rotate([])            -> RAISES IndexError list index out of range
rotateRepeat([], 0)   -> []
rotateRepeat([], 1)   -> RAISES IndexError list index out of range

main.py's printNumbers is affected by the same class of gap: inputList[numItems - 1] (main.py:9) evaluates to inputList[-1] on an empty list and raises the same bare IndexError.

Why this is worth deciding

The current behavior is now locked in by characterization tests (test_rotate_empty_raises, test_empty_1r_raises, test_printNumbers_empty_raises), so it will not change silently. What has never been decided is whether an empty list should be treated as an error at all — rotating nothing is arguably a well-defined no-op — and, if it is an error, whether the message should say so.

Options

  • Treat an empty list as a no-op, returning early from rotate when len(inputList) == 0.
  • Keep it an error, but raise with an explanatory message instead of letting a raw index access fail.
  • Leave the behavior as-is and consider the characterization tests sufficient documentation.

Whichever option is chosen, the existing characterization tests must be updated in the same change rather than deleted, and the in-place, no-copy constraint stated in README.md must be preserved.

This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions