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

Complicated use case #430

Closed
MartenBE opened this issue Sep 30, 2024 · 8 comments
Closed

Complicated use case #430

MartenBE opened this issue Sep 30, 2024 · 8 comments
Assignees

Comments

@MartenBE
Copy link

Hello, I've read the documentation, but am uncertain how I can implement the following options:

  • --version to give the version.
  • --config-file to point to an other config file. Default should be .test.yml, .test.yaml.
  • --check a CLI only boolean flag (not in yaml)
  • --width can both be set on CLI or in the yml file, is default 50.
  • --accepted-values is a list of codes, can be set on the CLI or in the yaml file.
  • FILENAMES is a postional list of filenames (nargs + so to speak)

I've come up with the following so far, but am unsure how to insert the --version and --config-file options to do as expected:

class Settings(BaseSettings):
    filenames: CliPositionalArg[list[str]]
    check: CliImplicitFlag[bool] = False
    width: int = 50
    accepted_values: list[str] = []

    @classmethod
    def settings_customise_sources(
        cls,
        settings_cls: Type[BaseSettings],
        init_settings: PydanticBaseSettingsSource,
        env_settings: PydanticBaseSettingsSource,
        dotenv_settings: PydanticBaseSettingsSource,
        file_secret_settings: PydanticBaseSettingsSource,
    ) -> Tuple[PydanticBaseSettingsSource, ...]:
        return (
            CliSettingsSource(settings_cls, cli_parse_args=True),
            YamlConfigSettingsSource(settings_cls, yaml_file=[".ski-lint.yml", ".ski-lint.yaml"]),
        )

The docs are rather confusing ... .

@hramezani
Copy link
Member

I've come up with the following so far, but am unsure how to insert the --version and --config-file options to do as expected:

Can't you add fields for these two fields like other fields? what is the difference between these two fields and others?

@MartenBE
Copy link
Author

MartenBE commented Sep 30, 2024

@hramezani

  • If I designate --version as an implicit flag, then I also get an --no-version flag in the help section which is confusing to say the least.
  • How do I get the value of --config-file in the yaml_file option of the YamlConfigSettingsSource? Is this supported?
  • How can I make some options CLI only? Can I make some config file only?

@hramezani
Copy link
Member

If I designate --version as an implicit flag, then I also get an --no-version flag in the help section which is confusing to say the least.

@kschwab is it possible?

How do I get the value of --config-file in the yaml_file option of the YamlConfigSettingsSource? Is this supported?

It is not possible right now. you have to do some hacky workaround. take a look at #259

How can I make some options CLI only? Can I make some config file only?

No, if you define a field in a settings model, then pydantic-settings tries to collect value for it from all the available settings sources. So, probably you can define them in a separate settings model and limit the settings model to only use cli or file.

@kschwab
Copy link
Contributor

kschwab commented Oct 3, 2024

@hramezani, it is possible, we would need to add another annotation to denote creating a boolean flag without --no-. Currently, this is standard behavior for python versions < 3.9 due to argparse limitation. For python >= 3.9, it always generates with the --no-. It is relatively small change, let me know if you would like me to proceed forward with PR.

@hramezani
Copy link
Member

@kschwab Thanks for your response. if it is not possible to remove the --no- in argparse, It is better not to support it.

@MartenBE
Copy link
Author

MartenBE commented Oct 5, 2024

Thank you all for the input. I have decided to go with OmegaConf instead as this gives me more flexibility. I'll close this issue.

@MartenBE MartenBE closed this as completed Oct 5, 2024
@kschwab
Copy link
Contributor

kschwab commented Oct 6, 2024

@hramezani, just for future clarity in case it comes up again, argparse supports both but uses different "actions". For just a constant flag, it uses the "store_true/store_false" actions, and for the "--with,--no-with" it uses the "BooleanOptionalAction" (which was added in python 3.9). Therefore, we just need to expose the constant actions explicitly, it is a small task if needed in the future.

@hramezani
Copy link
Member

Thanks @kschwab for clarification.
Ok, we can consider this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants