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

Feature/scope #2884

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

ryan-minato
Copy link

_scope_ keyword be added to specify a common root module for a dictionary and its sub-dictionaries.

Motivation

This feature can reduce repetition in the configuration files and codes. On the other hand, it could also isolate users and developers by treating target as a configuration item.

Currently, I have to write a data class configuration for every class I use. If this feature is added, I could implement a similar function simply through a decorator that checks input types based on the init function signature.

Have you read the [Contributing Guidelines on pull requests]

Yes

Test Plan

I have not yet added tests for this code because these changes are just for the demonstration implementation I envisioned.
However, I did create a small, simple demo that runs correctly, as shown below:

main.py

import hydra
from omegaconf import DictConfig, OmegaConf
from hydra.utils import instantiate


class A:
    def __init__(self, a1=1, a2="a2"):
        self.a1 = a1
        self.a2 = a2

    def __str__(self):
        return f"A: {self.a1}, {self.a2}"


class B:
    def __init__(self, b1=1, b2="b2"):
        self.b1 = b1
        self.b2 = b2

    def __str__(self):
        return f"B: {self.b1}, {self.b2}"
    
class C:
    def __init__(self, a: A, b: B):
        self.a = a
        self.b = b

    def __str__(self):
        return f"C: {self.a}, {self.b}"
    

@hydra.main(config_path="conf", config_name="config")
def main(cfg):
    c = instantiate(cfg.c)
    print(c)
    print(OmegaConf.to_yaml(cfg))


if __name__ == "__main__":
    main()

config.yaml

c:
  _scope_: "__main__"
  _target_: ".C"
  a:
    _target_: ".A"
    a1: 10
  b:
    _target_: ".B"
    b1: 100

Output

C: A: 10, a2, B: 100, b2
c:
  _scope_: __main__
  _target_: .C
  a:
    _target_: .A
    a1: 10
  b:
    _target_: .B
    b1: 100

Related Issues and PRs

Issue: #2883
Discussion: #2882

`_scope_` keyword be added to specify a common root module for a dictionary and its sub-dictionaries.
@facebook-github-bot
Copy link
Contributor

Hi @minato-ellie!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 4, 2024
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants