Skip to content
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

Merged
merged 2 commits into from
Jan 16, 2024

Conversation

pvandyken
Copy link
Contributor

The architecture for type conversion in the argparse integration was intended for using pathlib.Path, but had no flexibility even for builtin types like int

Here, remove the old globals() lookup, which exposed too much internal api. Instead, attempt to lookup simple strings as builtins, and strings with dots as resolved module imports (e.g. module.submodule.type). Path is kept as an exception, so using type: Path will still convert the argument into a path

Resolves #294

@github-actions github-actions bot requested review from akhanf and kaitj December 13, 2023 19:10
@pvandyken pvandyken added the enhancement New feature or request label Dec 13, 2023
Copy link
Contributor

@kaitj kaitj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost looks good, just had one clarification question and potentially a misspelling in the tests.

snakebids/tests/test_cli.py Outdated Show resolved Hide resolved
docs/bids_app/config.md Outdated Show resolved Hide resolved
@pvandyken pvandyken marked this pull request as draft December 18, 2023 21:02
@pvandyken
Copy link
Contributor Author

An essential problem with the current fix: with snakemake, every CLI arg will have to be serialized into YAML. Even with paths, we don't actually keep them as paths, they get turned into strings. So I'm going to add checks to ensure that all types selected can be simply serializable into primitive yaml types. I'll keep the module path logic though, as I already have the tests, and it may be useful in a more generic future if snakebids works with tools beyond snakemake

@pvandyken pvandyken force-pushed the feat/builtins-as-types branch from ba3f546 to 90b0845 Compare December 20, 2023 02:13
@@ -68,6 +67,7 @@ copier = ">=8.2.0"
jinja2-time = ">=0.2.0"
# minimum 2.31.0 because of security vulnerability
requests = ">=2.31.0"
ruamel-yaml = ">=0.17.2"
Copy link
Contributor Author

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

@@ -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]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yamlsafe is unused, but leaving this more as a hint to the future that we may want to support more flexible parsing of types for different applications

@pvandyken pvandyken force-pushed the feat/builtins-as-types branch from 38626c3 to 48e3bfe Compare December 20, 2023 02:28
@codecov-commenter
Copy link

codecov-commenter commented Dec 20, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c3bba2f) 89.14% compared to head (762d37c) 90.26%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #352      +/-   ##
==========================================
+ Coverage   89.14%   90.26%   +1.11%     
==========================================
  Files          30       32       +2     
  Lines        1465     1489      +24     
==========================================
+ Hits         1306     1344      +38     
+ Misses        159      145      -14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@pvandyken pvandyken marked this pull request as ready for review December 20, 2023 02:40
@pvandyken
Copy link
Contributor Author

I left a few specific comments above.

I'll write a test for the config writing function (although this functionality wasn't actually tested before), but should be good for review nonetheless

Copy link
Contributor

@kaitj kaitj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one lgtm pending resolving that test failure.

@pvandyken pvandyken force-pushed the feat/builtins-as-types branch 3 times, most recently from f98f97d to 363bbd3 Compare January 12, 2024 16:01
The architecture for type conversion in the argparse integration was
intended for using pathlib.Path, but had no flexibility even for
builtin types like int

Here, remove the old `globals()` lookup, which exposed too much
internal api. Instead, attempt to lookup simple strings as builtins, and
strings with dots as resolved module imports (e.g.
`module.submodule.type`). `Path` is kept as an exception, so using
`type: Path` will still convert the argument into a path

ruamel is now used for serialization, as pyyaml didn't support
serialization of specific python classes (e.g.  Path) without
restricting other classes: it was all or nothing. ruamel is safe by
default, and allows specifically opting in to representing different
classes

Clarify in docs how Path should be used for all paths to avoid relative
path issues

Resolves khanlab#294
ruamel fails to roundtrip the \x85 unicode character
@pvandyken pvandyken force-pushed the feat/builtins-as-types branch from 363bbd3 to 762d37c Compare January 15, 2024 19:18
@pvandyken pvandyken merged commit 19215f8 into khanlab:main Jan 16, 2024
34 checks passed
@pvandyken pvandyken deleted the feat/builtins-as-types branch January 16, 2024 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow builtins as types
3 participants