Skip to content

Commit

Permalink
Add check that type is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Sep 7, 2023
1 parent 66e663d commit 9a34f82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/chris_plugin/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
StoreFalseAction,
VersionAction,
)
from typing import Optional, Any, Sequence, List, Tuple, Union
from typing import Optional, Any, Sequence, List, Tuple, Union, get_args

_ALLOWED_PARAM_TYPES = get_args(ParameterType)


# noinspection PyProtectedMember
Expand Down Expand Up @@ -138,6 +140,10 @@ def get_param_type(a: StoreAction) -> ParameterType:
t = str
else:
t = a.type
if t.__name__ not in _ALLOWED_PARAM_TYPES:
raise ValueError(
f"Unsupported parameter type {t.__name__}. Supported types are {_ALLOWED_PARAM_TYPES}"
)
# noinspection PyTypeChecker
return t.__name__

Expand Down
8 changes: 8 additions & 0 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ def test_version_option_is_allowed(parser: ArgumentParser):
assert serialize(parser) == []


def test_add_list_arg(parser: ArgumentParser):
parser.add_argument(
"-l", "--list", default=[1, 2, 3], type=list, help="accepts a list of things"
)
with pytest.raises(ValueError, match="Unsupported parameter type list."):
serialize(parser)


def test_special_serializer(parser: ArgumentParser):
pass # TODO

Expand Down

0 comments on commit 9a34f82

Please sign in to comment.