-
-
Notifications
You must be signed in to change notification settings - Fork 75
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 Request: Add CLI argument parsing for model initialization #209
Comments
Thanks, @kschwab for this feature request. I think if we want to support CLI parsing, it has to be added as a new setting source class like other source classes. So, I think |
There was a similar issue before that was closed. at that point, they suggested using typer. @samuelcolvin @dmontagu what do you think? |
I think this would be great, as I said on the original issue, it can start small - e.g. fields can only be set via named arguments like |
I agree with @hramezani that this should be implemented as a separate Source. PR welcome. |
Shameless plug: cyto contains a CLI-based settings Source for pydantic. It uses click under the hood to parse the CLI arguments. There is an extensive test suite. Feel free to copy or take inspiration from cyto (it's under an MIT license). |
Yep, I agree with this as well. Once I started integration this was the direction I took. The only point here was backwards compatibility. As it currently sits, I introduced print(Settings(_cli_parse_args=True))
"""
{
'v0': '0',
'sub_model': {'v1': 'json-1', 'v2': b'nested-2', 'v3': 3, 'deep': {'v4': 'v4'}},
}
"""
Thanks for highlighting this thread, I had not seen it. @dmontagu opening statement "I would really like a lightweight CLI-argument parsing class, similar to BaseSettings" captures our interest and use case as well.
Perfect, this is where I started. The main points I intend to cover are:
Points I have not covered:
To summarize, it's basically a shim layer over the existing {
'v0': '0',
'sub_model.v1': 'json-1',
'sub_model.v2': b'nested-2',
'sub_model.v3': '3',
'sub_mode.deep.v4': 'v4'
} I still have some local cleanup to do but will push a draft PR once complete. |
The source should be off by default, that avoids any backwards compatibility issues. I'm not sure if you need sub-commands or shortened names initially, just populating a model from named arguments is a great start. |
It was just with respect to the @classmethod
def settings_customise_sources(
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
cli_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]: If we kick that out, then no conflicts. From user perspective it would just mean enabling CLI parsing would be done through override of @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), env_settings, init_settings |
@samuelcolvin I punted on the short opts but kept the subcommands. Dictionaries are also included 👍🏾 |
Does this mean if a user has a model |
@mpkocher that's a good point. I think there are two user groups for this feature. Those that want to use pydantic to create CLIs and those that want to use a CLI to interact with pydantic models. If you’re in the latter group, the answer is yes. However, if you’re in the former group, you probably do care that |
Hi @hramezani,
I wanted to propose adding CLI argument parsing into Pydantic settings. Under the hood, it is essentially the same as environment variable parsing, so not very difficult to add (relatively speaking). This would be nice to have as it would complete all sources of ingestion at the application level (FILES, ENV, CLI). I am in the process of completing a rough draft and wanted to see if there was interest in bringing something like this into main. Would love to contribute and take feedback on what would be desired if interested.
As with parsing environment variables example, nested settings would take precedence, etc. Essentially, the below:
would be equivalent to:
Then, within the application it would simply be:
Thoughts?
The text was updated successfully, but these errors were encountered: