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

Add option to specify config path argument name #334

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

joshlk
Copy link

@joshlk joshlk commented Dec 2, 2024

Add an option so the user can specify the name of the config path argument.

I have extended the add_config_path_arg parameter of ArgumentParser to accept a string. If specified this determines the name of the argument, otherwise, it defaults to the previous value of config_path.

I have also added the config_path parameter to ArgumentParser's docstrings, as it was missing previously.

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.

Project coverage is 87.03%. Comparing base (4388893) to head (83e0e8f).

Files with missing lines Patch % Lines
simple_parsing/parsing.py 66.66% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #334      +/-   ##
==========================================
- Coverage   87.06%   87.03%   -0.03%     
==========================================
  Files          34       34              
  Lines        4538     4543       +5     
==========================================
+ Hits         3951     3954       +3     
- Misses        587      589       +2     

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

Copy link
Owner

@lebrice lebrice left a comment

Choose a reason for hiding this comment

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

Thanks a lot @joshlk , this looks good! There's just a minor comment here about the extra restriction that you place on the values that config_path_name can take.

Comment on lines 169 to 173
if isinstance(add_config_path_arg, str) and not add_config_path_arg.isidentifier():
raise ValueError(
"If `add_config_path_arg` is a string it must be a valid Python identifier (no dashes)."
f" Not: {add_config_path_arg}"
)
Copy link
Owner

Choose a reason for hiding this comment

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

Actually, you can use attribute names with dashes when you getattr and setattr on a Namespace object:

>>> from argparse import Namespace
>>> ns = Namespace()
>>> setattr(ns, "some-attr", 42)
>>> getattr(ns, "some-attr")
42

Copy link
Owner

Choose a reason for hiding this comment

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

Also, if you have the time, would you mind adding a simple test so we can cover this change?

@pytest.mark.parametrize("invalid_value", ["foo.bar.baz?", "-------", "bob bob bob"])  # add other more relevant examples perhaps?
def test_pass_invalid_value_to_add_config_path_arg(invalid_value):
    with pytest.raises(ValueError):
        _ = ArgumentParser(add_config_path_arg=invalid_value)

Copy link
Author

Choose a reason for hiding this comment

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

Actually, you can use attribute names with dashes when you getattr and setattr on a Namespace object:

>>> from argparse import Namespace
>>> ns = Namespace()
>>> setattr(ns, "some-attr", 42)
>>> getattr(ns, "some-attr")
42

Thanks good idea. I have it working now. For reference, you have to replace the dashes with underscores like so:

getattr(args_with_config_path, config_path_name.replace('-', '_'))

Copy link
Author

Choose a reason for hiding this comment

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

Also, if you have the time, would you mind adding a simple test so we can cover this change?

@pytest.mark.parametrize("invalid_value", ["foo.bar.baz?", "-------", "bob bob bob"])  # add other more relevant examples perhaps?
def test_pass_invalid_value_to_add_config_path_arg(invalid_value):
    with pytest.raises(ValueError):
        _ = ArgumentParser(add_config_path_arg=invalid_value)

I've added some tests

@@ -1010,10 +1027,12 @@ def parse(

If `config_path` is passed, loads the values from that file and uses them as defaults.
"""
if dest == add_config_path_arg:
raise ValueError("`add_config_path_arg` cannot use the same name as `dest`.")
Copy link
Owner

@lebrice lebrice Dec 5, 2024

Choose a reason for hiding this comment

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

Suggested change
raise ValueError("`add_config_path_arg` cannot use the same name as `dest`.")
raise ValueError("`add_config_path_arg` cannot be the same as `dest`.")

@joshlk
Copy link
Author

joshlk commented Dec 7, 2024

@lebrice I've addressed the comments. Its ready for a re-review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants