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

adding yaml config interpolation #294

Open
stas00 opened this issue Nov 18, 2023 · 5 comments
Open

adding yaml config interpolation #294

stas00 opened this issue Nov 18, 2023 · 5 comments
Labels
help wanted Extra attention is needed

Comments

@stas00
Copy link
Contributor

stas00 commented Nov 18, 2023

Is your feature request related to a problem? Please describe.

Could SimpleParsing add support for interpolation? here is an example of how omegaconf does it:
https://omegaconf.readthedocs.io/en/2.3_branch/usage.html#config-node-interpolation

Describe the solution you'd like

So that we could do something like this:

Input YAML file:

server:
  host: localhost
  port: 80

client:
  url: http://${server.host}:${server.port}/
  server_port: ${server.port}
  # relative interpolation
  description: Client of ${.url}

Thank you!

@lebrice
Copy link
Owner

lebrice commented Nov 23, 2023

Hello there @stas00 , thanks for posting! :)

I'm familiar with Hydra/OmegaConf, and this feature is very powerful. I agree that it would be great to also have this in Simple-Parsing. One way to do this could be to add omegaconf as an optional extra dependency, and reuse as much of their interpolation code as possible, with some custom logic as needed.

I'm assuming we'd want to use this only when parsing config files, correct?
Otherwise I could also imagine a nice Python api with something like an interpolated_field variant of the dataclasses.field function:

from dataclasses import dataclass
from simple_parsing import interpolated_field, parse

@dataclass
class Server:
    host: str
    port: int

@dataclass
class Client
    url: str = interpolated_field("https://${server.host}:${server.port}/")
    server_port: int = interpolated_field("${server.port}")
    #relative interpolation
    description: str = interpolated_field("Client of ${.url}")

@dataclass
class Config:
    server: Server
    client: Client

config = parse(Config)

Note: For the last kind of (relative) interpolation, I made this little gist for what I call conditional_field, which you might find interesting: https://gist.github.com/lebrice/728f0e0218dcef6f74cbebd70af5857e#file-conditional_fields-py-L145-L160 (You can also directly pip-install that gist if you want to try it out.)

I think that adding interpolation in just yaml files first would be a (challenging) good start. It shouldn't be much more work to later add a helper function to also make this possible in the dataclasses, if we think this is useful.

Thanks again for posting, let me know what you think! :)

@stas00
Copy link
Contributor Author

stas00 commented Nov 23, 2023

Hi Fabrice,

I agree that starting with interpolation support in just the yaml file would be great!

For the dataclasses python code one could already use the __post_init__ to interpolate the data at loading time. But, of course, the solution you proposed looks better.

@lebrice
Copy link
Owner

lebrice commented Nov 23, 2023

__post_init__ doesn't easily let you interpolate with fields outside your own dataclass instance though, except if you're using some kind of global variable or contextvar, I suppose.

@stas00
Copy link
Contributor Author

stas00 commented Nov 23, 2023

Indeed, the top dataclass class will receive access to all parsed configs, so that's where we currently manipulate things.

@lebrice lebrice added the help wanted Extra attention is needed label Nov 27, 2023
@jerpint
Copy link

jerpint commented Feb 5, 2024

+1 to adding suport for Omega-conf style interpolation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants