-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Subclassed ArgumentParsers are not respected #171
Comments
In commit 7813142 I fixed the Having said this, I need to give a fair warning. If you do want extend private classes and methods, bear in mind that when your code breaks due to internal changes, it will not be considered a bug. Note that by private I do not mean just names starting with |
Thank you for fixing the issue where it was feasible. I'll certainly accept the risk of having my code break as the API improves (very easy to detect because I setup my CI's to run the minimum and latest versions of dependencies). It's easy to pin versions to keep production smooth, and I feel it's important to contribute back to FOSS projects like this and help fix/improve things when said breakage situations occur. I'm also fine with keeping in this small monkey patch to "make things work" for the foreseeable future. I don't see an immediately easy way to update that staticmethod with the information it would need to know which specific subclass of ArgumentParser it should be associated with. But perhaps cleaned up internals would be able to overcome this. I'm willing to wait for (or potentially help with) that. More details on my use caseFWIW, my immediate goal is to integrate jsonargparse with my yet-another-argparse-alternative: scriptconfig. The way scriptconfig works is that a CLI configuration is defined using a dictionary or data-class-like class. The keys are argument names, and the values are either the default value, or a special import scriptconfig as scfg
class MyClassConf(scfg.DataConfig):
param1 = scfg.Value('a', help='param1')
flag1 = scfg.Value(True, isflag=True, help='a flag')
class MyClass:
__config__ = MyClassConf
def __init__(self, other_arg2, **kwargs):
self.other_arg2 = other_arg2
self.config = self.__config__(**kwargs) So in order to seemlessly transition between scriptconfig and jsonargparse, when I think the more general feature that jsonargparse might consider that would make this entire issue of subclassing moot is if future designs checked classes for some special attribute that could allow the user to be explicit about what arguments could be introspected from them. I love the |
- Fixed parent parser logger not being forwarded to subcommand and internal parsers. - CLI now supports the parser_class parameter. - Parsing steps logging now at debug level. - Discarding init_args warning changed to log at debug level. - Removed replacing list instead of append warning. - Refactored some of the context managers and context variables.
- Fixed parent parser logger not being forwarded to subcommand and internal parsers. - CLI now supports the parser_class parameter. - Parsing steps logging now at debug level. - Discarding init_args warning changed to log at debug level. - Removed replacing list instead of append warning. - Refactored some of the context managers and context variables.
- Fixed parent parser logger not being forwarded to subcommand and internal parsers. - CLI now supports the parser_class parameter. - Parsing steps logging now at debug level. - Discarding init_args warning changed to log at debug level. - Removed replacing list instead of append warning. - Refactored some of the context managers and context variables.
#225 fixed this. However, when using |
🐛 Bug report
If
jsonargparse.ArgumentParser
is subclassed, it will fail to execute subclassed functionality for subclass arguments.This is because (in the case of the help string) in jsonargparse/actions.py in
print_help
. It calls:which creates an empty instance of
jsonargparse.ArgumentParser
to continue argument processing, and not whatever subclass originally invoked the parsing callstack.I'm actually not 100% sure what is happening in the non-help case, but if I monkey patch
jsonargparse.ArgumentParser
andjsonargparse.core.ArgumentParser
that does seem to resolve the issue. However, I'd like to avoid monkey patching if possible, hence the bug report.To reproduce
The following is a MWE where I've disabled the monkey patch to make it work in the way I would expect:
Without the monkey patch if I invoke:
I get:
which does not respect the customized arguments injected by my subclass.
Furthermore, running:
python jsonargparse_subclass_mwe.py --class_a=ClassB --class_a.init_args.b_name=foo
doesn't even work:Expected behavior
If I include the monkey patch then invoking the above help command correctly results in the customized arguments being respected:
And if I invoke:
python jsonargparse_subclass_mwe.py --class_a=ClassB --class_a.init_args.b_name=foo
I getas I would expect.
Environment
jsonargparse.version = 4.14.1
Notes:
Fixing this issue naively would break LightningCLI as noted here: Lightning-AI/pytorch-lightning#15038
The text was updated successfully, but these errors were encountered: