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

CLI with non primitive types + subcommands #430

Open
AlejandroBaron opened this issue Jan 11, 2024 · 3 comments
Open

CLI with non primitive types + subcommands #430

AlejandroBaron opened this issue Jan 11, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@AlejandroBaron
Copy link

Hello!

I'm trying to do combine the CLI + config utility with subcommands and i'm not able to.

The CLI takes an object as an argument and in the subcommand it calls an object submethod.

This is a minimal example with 3 files

python example.py --config config.yaml subcommand

myclass.py

class MyClass:

    def __init__(self, x: int):
        self.x = x

    def method(self):
        print(self.x*2)

example.py (entrypoint)

from jsonargparse import CLI
from myclass import MyClass

class MyCLI:

    def __init__(self, myobj: MyClass):

        self.myobj = myobj

    def subcommand(self):
        self.myobj.method()

if __name__ == "__main__":
    CLI(MyCLI)

config.yaml

myobj:
  class_path: myclass.MyClass
  init_args:
    x: 2

Is this not supported yet?

@mauvilsa
Copy link
Member

In principle it should be supported. But I have never tried having a subclass type in an __init__ parameter a class given to jsonargparse.CLI. Could be that there is a problem with it.

@mauvilsa mauvilsa added the bug Something isn't working label Jan 11, 2024
@mauvilsa
Copy link
Member

mauvilsa commented Jan 22, 2024

The problem is that the definition myobj: MyClass is interpreted as required/positional. When parsing the arguments of python example.py --config config.yaml subcommand after parsing the config (optional), it then tries to parse myobj as a positional, but it finds subcommand (the first given positional), which obviously is not a valid MyClass. I know that inside config.yaml there is already a valid value for myobj, but this is independent of parsing of positionals. I am not sure how this could be fixed or if it is even possible.

@AlejandroBaron changing myobj to be non-positional would make it work. Just change the definition to myobj: Optional[MyClass] = None or add a default like myobj: MyClass = lazy_instance(MyClass, x=1). Also, you need to change the name of the method subcommand since it conflicts with the key used to know which subcommand was selected. It gives non-understandable message, so I need to fix that.

mauvilsa added a commit that referenced this issue Jan 31, 2024
mauvilsa added a commit that referenced this issue Feb 1, 2024
@mauvilsa
Copy link
Member

mauvilsa commented Feb 1, 2024

With pull request #444 a better error message is given when a class has a subcommand method conflicting with the key where the chose subcommand is stored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants