X Tutup
Skip to content

#417 -- update JavaList pop method#564

Open
b0uks wants to merge 1 commit intopy4j:masterfrom
b0uks:417-update-pop-method
Open

#417 -- update JavaList pop method#564
b0uks wants to merge 1 commit intopy4j:masterfrom
b0uks:417-update-pop-method

Conversation

@b0uks
Copy link

@b0uks b0uks commented May 19, 2025

Simplify JavaList.pop and Ensure IndexError on Empty Lists


Summary
This patch refactors the JavaList.pop implementation to default the key parameter to -1 instead of None, removes the special-case branch, and delegates all index handling to __compute_index. As a result, calling pop() on an empty Java list now correctly raises an IndexError in accordance with the MutableSequence contract.


Related Issues


Changes

- Removes the `key is None` branch entirely.
- Relies on `__compute_index` to translate both positive and negative indices (and to raise `IndexError` if out of bounds).
  • Test coverage:
    Added comprehensive unit tests in java_list_test.py:

    • testPopDefault: Verifies that pop() removes and returns the last element.

    • testPopPositiveIndex: Covers popping at explicit positive indices (including 0).

    • testPopNegativeIndex: Covers various negative indices (-1, -2, etc.).

    • testPopEdgeCases: Confirms that popping from an empty list or with out-of-bounds indices raises IndexError.


Testing
All existing tests pass, and the new testPop* methods validate correct behavior across default, positive, negative, and edge-case scenarios.


Future Considerations

  • We could remove JavaList.pop entirely and inherit MutableSequence.pop, which appears to handle all cases correctly. That approach may reduce maintenance overhead, but risks altering backward-compatibility with any custom behavior in java_remove.

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.

1 participant

X Tutup