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

Yaml config not working #885

Closed
gpongelli opened this issue Mar 6, 2024 · 3 comments
Closed

Yaml config not working #885

gpongelli opened this issue Mar 6, 2024 · 3 comments
Labels
🎲 can't reproduce Root cause not from project ❔ question Further information is requested

Comments

@gpongelli
Copy link

gpongelli commented Mar 6, 2024

Hi,
I've noticed that yaml config file isn't parsed, meanwhile json and toml are.

config.json:

{
  "main": {
    "str_p": "ok"
  }
}

config.yaml:

main:
  str_p: "ko"

config.toml:

[main]
str_p="toml"

code snippet

MAIN_CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'], show_default=True)

@group(invoke_without_command=True, align_option_groups=False, 
       no_args_is_help=False,
       context_settings=MAIN_CONTEXT_SETTINGS) 
@config_option(default=str(Path.cwd() / "*.yaml"), formats=Formats.YAML)  # changed both to json to see differences
@verbosity_option(default="INFO")
@option("--str-p", type=str, default="oooo")
@pass_context
def main(ctx,str_p):
    echo(str_p)

both config.yaml and config.json are into the same folder and are loaded, but the output is different:

  • json prints ok loaded from file
  • toml prints toml loaded from file
  • yaml prints oooo coming from default value

Am I doing something in a wrong way ?

useful versions:
python 3.11
click 8.1.7
click-extra 4.7.4

Thanks.

@gpongelli
Copy link
Author

gpongelli commented Mar 7, 2024

I found the solution: I've other yaml file in the repository, so when using *.yaml click-extra loads a file that it's not the "config" expected one.
I've found which file is loaded using echo(f"Configuration location: {ctx.meta['click_extra.conf_source']}") from docs.

Forcing to load "config.yaml" file in @config_option(default=str(Path.cwd() / "config.yaml"), formats=Formats.YAML) solves the issue.

@kdeldycke kdeldycke added 🚫 wont do/fix This will not be worked on ❔ question Further information is requested labels Apr 15, 2024
@kdeldycke
Copy link
Owner

Thanks @gpongelli for giving Click Extra a try! Happy to see that you ended up finding the root cause of your issue!

And yes, the configuration loader is quite flexible so it's easy to make it work unexpectedly. You did the right thing looking into the ctx.meta. I'm glad I added these direct metadata there.

@kdeldycke kdeldycke added 🎲 can't reproduce Root cause not from project and removed 🚫 wont do/fix This will not be worked on labels Apr 15, 2024
@kdeldycke kdeldycke changed the title yaml config not working Yaml config not working Apr 15, 2024
Copy link
Contributor

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🎲 can't reproduce Root cause not from project ❔ question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants