Skip to content

Commit

Permalink
Validate the type of dynamic values
Browse files Browse the repository at this point in the history
  • Loading branch information
plannigan committed Oct 13, 2023
1 parent 8fc0d34 commit befa2dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Python version `3.12` tested during CI
- Python version `3.12` added to package classifiers

### Fixed

- Raise `ValueError` when dynamic value returns the wrong type.

### Removed

- Support for Python version `3.7`.
Expand Down
5 changes: 4 additions & 1 deletion columbo/_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,10 @@ def to_value(
if isinstance(value, value_type):
return value
if callable(value):
return value(answers)
result = value(answers)
if isinstance(result, value_type):
return result
raise ValueError(f"Invalid dynamic value: {result}")
raise ValueError(f"Invalid value: {value}")


Expand Down
5 changes: 5 additions & 0 deletions tests/interaction_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ def test_to_value__invalid_type__exception():
to_value(object(), SOME_ANSWERS, str) # type: ignore[arg-type]


def test_to_value__invalid_dynamic_type__exception():
with pytest.raises(ValueError):
to_value(lambda _: object(), SOME_ANSWERS, str) # type: ignore[arg-type,return-value]


def test_to_labeled_options__invalid_type__exception():
with pytest.raises(ValueError):
to_labeled_options(object(), SOME_ANSWERS) # type: ignore[arg-type]
Expand Down

0 comments on commit befa2dd

Please sign in to comment.