jsonargparse icon indicating copy to clipboard operation
jsonargparse copied to clipboard

Repeated Keys in Nested List

Open efanibi25 opened this issue 1 year ago • 1 comments

This was my first day trying out this module I notice that repeated key names may lead to undesirable results. Even if they are separated by level

I have given out some examples I tested out. It really only causes issues for two of them

efanibi25 avatar Sep 12 '22 22:09 efanibi25

Example 1

#!/usr/bin/env python3.9

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]
p.add_argument("--test")



{
  "test": "value"
}



Example 2: Does not work

#!/usr/bin/env python3.9

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]



p2 = ArgumentParser()
p2.add_argument("--test")

subcommands = p.add_subcommands()
subcommands.add_subcommand('test', p2)

{
  "test": {
    "test":"value"
  }
}




Example 3

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]



p2 = ArgumentParser()
p2.add_argument("--test2")

subcommands = p.add_subcommands()
subcommands.add_subcommand('test', p2)

{
  "test": {
    "test2":"value"
  }
}




Example 4
#!/usr/bin/env python3.9

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]



p2 = ArgumentParser()

p3 = ArgumentParser()
p3.add_argument("--test2")


subcommands = p.add_subcommands()
subcommands.add_subcommand('test', p2)


subcommands = p2.add_subcommands()
subcommands.add_subcommand('test2', p3)


{
  "test": {
    "test2":{
      "test2":"value"
    }
  }
}


Examples 6 : Does not Work
#!/usr/bin/env python3.9

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]



p2 = ArgumentParser()

p3 = ArgumentParser()
p3.add_argument("--test")


subcommands = p.add_subcommands()
subcommands.add_subcommand('test', p2)


subcommands = p2.add_subcommands()
subcommands.add_subcommand('test', p3)



{
  "test": {
    "test":{
      "test":"value"
    }
  }
}

Example 7

#!/usr/bin/env python3.9

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]



p2 = ArgumentParser()

p3 = ArgumentParser()
p3.add_argument("--test2")


subcommands = p.add_subcommands()
subcommands.add_subcommand('test', p2)


subcommands = p2.add_subcommands()
subcommands.add_subcommand('test2', p3)



{
  "test": {
    "test2":{
      "test2":"value"
    }
  }
}

Example 8

#!/usr/bin/env python3.9

from jsonargparse import ArgumentParser, ActionConfigFile
import os


defaultconfigPath = os.path.join(os.path.dirname(
    os.path.realpath(__file__)), "config.json")
p = ArgumentParser()
p.default_config_files = [defaultconfigPath]



p2 = ArgumentParser()

p3 = ArgumentParser()
p3.add_argument("--test3")


subcommands = p.add_subcommands()
subcommands.add_subcommand('test', p2)


subcommands = p2.add_subcommands()
subcommands.add_subcommand('test', p3)



{
  "test": {
    "test":{
      "test3":"value"
    }
  }
}

Example 2 Errors out with

'Key is required if value not a Namespace.'

Example 6 Errors out with

Problem in default config file "...." :: "subcommand" is required but not given or its value is None.

efanibi25 avatar Sep 12 '22 22:09 efanibi25

@efanibi25 thank you for reporting. Just as an update, I have looked at this but unfortunately it is not an easy fix. This will need some refactoring of the code related to subcommands, thus the fix will take a bit more of time.

mauvilsa avatar Sep 26 '22 06:09 mauvilsa