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

Support YAML anchors in configs for CLI #536

Closed
rusmux opened this issue Jun 24, 2024 · 4 comments
Closed

Support YAML anchors in configs for CLI #536

rusmux opened this issue Jun 24, 2024 · 4 comments
Labels
question Further information is requested

Comments

@rusmux
Copy link

rusmux commented Jun 24, 2024

🚀 Feature request

Support YAML anchors in configs for CLI.

Motivation

Anchors reduce duplication of configuration values. For example (not necessarily like this):

x-common-args: &common_args
  a: 1
  b: 2
  c: 3

instances:
  - class_path: __main__.MyClass
    init_args:
      name: instance_1
      <<: *common_arg
  - class_path: __main__.MyClass
    init_args:
      name: instance_2
      <<: *common_arg
  ...

Pitch

YAML anchors are supported by CLI.

Alternatives

@rusmux rusmux added the enhancement New feature or request label Jun 24, 2024
@mauvilsa
Copy link
Member

mauvilsa commented Jun 24, 2024

Than you for proposing! Note that yaml anchors are supported. What isn't possible is to add keys with arbitrary names, like x-common-args, which are not part of the signature. For example the following should work fine:

instances:
  - class_path: __main__.MyClass
    init_args: &init_args
      name: instance_1
      a: 1
      b: 2
      c: 3
  - class_path: __main__.MyClass
    init_args:
      <<: *init_args
      name: instance_2

@rusmux
Copy link
Author

rusmux commented Jun 24, 2024

Oh, cool, thank you! Would it be possible to make CLI ignore extra values in configs?

@mauvilsa
Copy link
Member

Ignoring extra content in configs is dangerous because other mistakes wouldn't be noticed. The same reason why parse_known_args is not implemented. So I am not really inclined to do this.

But you could handle this yourself by implementing a custom loader. That is:

import yaml
from jsonargparse import set_loader

def custom_yaml_load(stream):
    data = yaml.safe_load(stream)
    if isinstance(data, dict):
        data.pop("x-common-args")
    return data

set_loader("yaml", custom_yaml_load)

Some notes about this:

  • The loader is used to parse everything (command line values, subconfigs, env vars), so the pop-ed key must be truly unique, otherwise valid data could be removed.
  • jsonargparse's default yaml loader handles a few cases that the default yaml doesn't, e.g. floats in scientific notation. Instead of yaml.safe_load the default jsonargparse._loaders_dumpers.yaml_load could be used. Currently not public but could be made public.

@mauvilsa mauvilsa added question Further information is requested and removed enhancement New feature or request labels Jul 1, 2024
@mauvilsa
Copy link
Member

mauvilsa commented Jul 3, 2024

With #543 now it is possible to get the default yaml loader.

@mauvilsa mauvilsa closed this as completed Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants