From 86676b77cd469baddf1ec6d3c7ca0d6b567a1ed2 Mon Sep 17 00:00:00 2001 From: miskatul-anwar Date: Mon, 24 Aug 2026 00:51:10 +0600 Subject: [PATCH 1/2] feat(picker): support negative default_index --- src/pick/__init__.py | 7 +++++-- tests/test_pick.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pick/__init__.py b/src/pick/__init__.py index d9535a8..b0e7ff5 100644 --- a/src/pick/__init__.py +++ b/src/pick/__init__.py @@ -66,8 +66,11 @@ def __post_init__(self) -> None: if len(self.options) == 0: raise ValueError("options should not be an empty list") - if self.default_index >= len(self.options): - raise ValueError("default_index should be less than the length of options") + if self.default_index < -len(self.options) or self.default_index >= len(self.options): + raise ValueError("default_index is out of range of options") + + if self.default_index < 0: + self.default_index += len(self.options) if self.multiselect and self.min_selection_count > len(self.options): raise ValueError( diff --git a/tests/test_pick.py b/tests/test_pick.py index acec4c3..d8f023f 100644 --- a/tests/test_pick.py +++ b/tests/test_pick.py @@ -77,3 +77,21 @@ def test_disabled_option(): assert picker.get_selected() == (Option("option1"), 0) picker.move_down() assert picker.get_selected() == (Option("option3"), 2) + + +def test_negative_default_index(): + import pytest + options = ["option1", "option2", "option3"] + picker = Picker(options, default_index=-1) + assert picker.get_selected() == ("option3", 2) + lines, _ = picker.get_lines() + assert "* option3" in lines + + picker = Picker(options, default_index=-3) + assert picker.get_selected() == ("option1", 0) + + with pytest.raises(ValueError): + Picker(options, default_index=-4) + with pytest.raises(ValueError): + Picker(options, default_index=3) + From 1222ed19f2a860437f0392d52ecae9636e948efe Mon Sep 17 00:00:00 2001 From: miskatul-anwar Date: Sat, 29 Aug 2026 17:37:05 +0600 Subject: [PATCH 2/2] refactor(tests): move pytest import to top of test_pick.py --- tests/test_pick.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_pick.py b/tests/test_pick.py index d8f023f..c7e3e88 100644 --- a/tests/test_pick.py +++ b/tests/test_pick.py @@ -1,4 +1,6 @@ -from pick import Picker, Option +import pytest + +from pick import Option, Picker def test_move_up_down(): @@ -80,7 +82,6 @@ def test_disabled_option(): def test_negative_default_index(): - import pytest options = ["option1", "option2", "option3"] picker = Picker(options, default_index=-1) assert picker.get_selected() == ("option3", 2) @@ -94,4 +95,3 @@ def test_negative_default_index(): Picker(options, default_index=-4) with pytest.raises(ValueError): Picker(options, default_index=3) -