Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/pyrecest/filters/sequence_association.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,10 @@ def _validate_cost(value: object, name: str) -> float:

def _validate_positive_integer(value: object, name: str) -> int:
message = f"{name} must be a positive integer"
value_array = np.asarray(value)
try:
value_array = np.asarray(value)
except (TypeError, ValueError) as exc:
raise ValueError(message) from exc
if value_array.ndim != 0 or value_array.dtype == np.bool_:
raise ValueError(message)

Expand Down
20 changes: 20 additions & 0 deletions tests/filters/test_sequence_association_top_k_bad_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest

from pyrecest.filters import SequenceAssociationNode, solve_top_k_viterbi_sequence_associations


class UncoercibleScalar:
def __array__(self, dtype=None):
del dtype
raise TypeError("cannot convert")


def test_top_k_terminal_paths_reports_value_error_for_uncoercible_scalar():
frames = [[SequenceAssociationNode(0, 0)]]

with pytest.raises(ValueError, match="top_k_terminal_paths must be a positive integer"):
solve_top_k_viterbi_sequence_associations(
frames,
lambda _previous, _current, _context: 0.0,
top_k_terminal_paths=UncoercibleScalar(),
)
Loading