From 6b56773c34ac5772b8b361c3240d52d6a67993a9 Mon Sep 17 00:00:00 2001 From: Fabian Zills Date: Sun, 16 Jun 2024 00:25:02 +0200 Subject: [PATCH 1/2] do not allow selection to contain duplicates --- tests/test_selection.py | 7 +++++++ zndraw/zndraw.py | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/tests/test_selection.py b/tests/test_selection.py index 021af9d22..4e284618d 100644 --- a/tests/test_selection.py +++ b/tests/test_selection.py @@ -21,3 +21,10 @@ def test_selection(server, s22): vis.selection = [0, 7, 6, 5, 4] assert vis.selection == [0, 7, 6, 5, 4] + + with pytest.raises(ValueError): + vis.selection = [0, 0, 2] + + with pytest.raises(ValueError): + vis.selection = "Hello" + diff --git a/zndraw/zndraw.py b/zndraw/zndraw.py index 0b026b96e..98c670570 100644 --- a/zndraw/zndraw.py +++ b/zndraw/zndraw.py @@ -252,8 +252,12 @@ def selection(self) -> list[int]: @selection.setter def selection(self, value: list[int]): + if not isinstance(value, list): + raise ValueError("Selection must be a list") if not all(isinstance(x, int) for x in value): raise ValueError("Selection must be a list of integers") + if len(value) != len(set(value)): + raise ValueError("Selection must not contain duplicates") max_index = len(self.atoms) if any(x >= max_index for x in value): From 4fbbada4404d96c7f59a5ab9866038c2101eefac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Jun 2024 22:25:19 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_selection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_selection.py b/tests/test_selection.py index 4e284618d..99d87f584 100644 --- a/tests/test_selection.py +++ b/tests/test_selection.py @@ -24,7 +24,6 @@ def test_selection(server, s22): with pytest.raises(ValueError): vis.selection = [0, 0, 2] - + with pytest.raises(ValueError): vis.selection = "Hello" -