diff --git a/snakebids/cli.py b/snakebids/cli.py index c9a2d717..9aef32a5 100644 --- a/snakebids/cli.py +++ b/snakebids/cli.py @@ -12,6 +12,7 @@ from typing_extensions import override from snakebids.exceptions import ConfigError, MisspecifiedCliFilterError +from snakebids.io.yaml import get_yaml_io from snakebids.types import InputsConfig, OptionalFilter from snakebids.utils.utils import to_resolved_path @@ -204,7 +205,7 @@ def create_parser(include_snakemake: bool = False) -> argparse.ArgumentParser: return parser -def _find_type(name: str) -> type[Any]: +def _find_type(name: str, *, yamlsafe: bool = True) -> type[Any]: import importlib if name == "Path": @@ -218,6 +219,9 @@ def _find_type(name: str) -> type[Any]: if not callable(type_): msg = f"{name} cannot be used as a type" raise ConfigError(msg) + if yamlsafe and type_ not in get_yaml_io().representer.yaml_representers: + msg = f"{name} cannot be serialized into yaml" + raise ConfigError(msg) return type_ diff --git a/snakebids/tests/test_cli.py b/snakebids/tests/test_cli.py index 15e5dac7..6edc446b 100644 --- a/snakebids/tests/test_cli.py +++ b/snakebids/tests/test_cli.py @@ -24,7 +24,6 @@ from snakebids.tests import strategies as sb_st from snakebids.tests.helpers import allow_function_scoped from snakebids.types import InputsConfig, OptionalFilter -from snakebids.utils.utils import BidsEntity from .mock.config import parse_args, pybids_inputs @@ -224,25 +223,17 @@ def test_convert_arg_to_builtin( args = parser.parse_args() assert isinstance(args.new_param, int) - def test_convert_arg_to_resolved_object( - self, parser: ArgumentParser, mocker: MockerFixture - ): + def test_non_serialiable_type_raises_error(self, parser: ArgumentParser): new_args = { "--new-param": { "help": "Generic Help Message", "type": "snakebids.utils.utils.BidsEntity", } } - mocker.patch.object( - sys, "argv", [*self.mock_all_args, "--new-param", "subject"] - ) - add_dynamic_args(parser, new_args, {}) - args = parser.parse_args() - assert isinstance(args.new_param, BidsEntity) + with pytest.raises(ConfigError, match="cannot be serialized into yaml"): + add_dynamic_args(parser, new_args, {}) - def test_using_module_as_type_gives_error( - self, parser: ArgumentParser, mocker: MockerFixture - ): + def test_using_module_as_type_gives_error(self, parser: ArgumentParser): new_args = { "--new-param": { "help": "Generic Help Message", @@ -252,9 +243,7 @@ def test_using_module_as_type_gives_error( with pytest.raises(ConfigError, match="cannot be used as a type"): add_dynamic_args(parser, new_args, {}) - def test_using_class_method_as_type_gives_error( - self, parser: ArgumentParser, mocker: MockerFixture - ): + def test_using_class_method_as_type_gives_error(self, parser: ArgumentParser): new_args = { "--new-param": { "help": "Generic Help Message",