-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow builtins and resolved paths as cli types #352
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ | |
import snakemake | ||
from typing_extensions import override | ||
|
||
from snakebids.exceptions import MisspecifiedCliFilterError | ||
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,6 +205,26 @@ def create_parser(include_snakemake: bool = False) -> argparse.ArgumentParser: | |
return parser | ||
|
||
|
||
def _find_type(name: str, *, yamlsafe: bool = True) -> type[Any]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
import importlib | ||
|
||
if name == "Path": | ||
return Path | ||
*module_name, obj_name = name.split(".") if "." in name else ("builtins", name) | ||
try: | ||
type_ = getattr(importlib.import_module(".".join(module_name)), obj_name) | ||
except (ImportError, AttributeError) as err: | ||
msg = f"{name} could not be resolved" | ||
raise ConfigError(msg) from err | ||
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_ | ||
|
||
|
||
def add_dynamic_args( | ||
parser: argparse.ArgumentParser, | ||
parse_args: Mapping[str, Any], | ||
|
@@ -220,16 +241,12 @@ def add_dynamic_args( | |
# a str to allow the edge case where it's already | ||
# been converted | ||
if "type" in arg: | ||
try: | ||
arg_dict = {**arg, "type": globals()[str(arg["type"])]} | ||
except KeyError as err: | ||
msg = f"{arg['type']} is not available as a type for {name}" | ||
raise TypeError(msg) from err | ||
arg_dict = {**arg, "type": _find_type(str(arg["type"]))} | ||
else: | ||
arg_dict = arg | ||
app_group.add_argument( | ||
*_make_underscore_dash_aliases(name), | ||
**arg_dict, | ||
**arg_dict, # type: ignore | ||
) | ||
|
||
# general parser for | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched to ruamel because pyyaml didn't support allowing only specified types to be serialized, it either tried to serialize everything (using weird custom python tags that snakemake would then refuse to read), or only serialize basic types (but we need it to specifically support Path). ruamel did support this.
If we switch to reading yaml with ruamel in the future, we could further take advantage of its round-trip comment preservation