Skip to content

Commit

Permalink
Ensure cli types are yaml serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
pvandyken committed Dec 20, 2023
1 parent 1363dcd commit 90b0845
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
6 changes: 5 additions & 1 deletion snakebids/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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":
Expand All @@ -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_


Expand Down
21 changes: 5 additions & 16 deletions snakebids/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 90b0845

Please sign in to comment.