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

Add CLI App Support #389

Merged
merged 20 commits into from
Sep 23, 2024
Merged

Add CLI App Support #389

merged 20 commits into from
Sep 23, 2024

Conversation

kschwab
Copy link
Contributor

@kschwab kschwab commented Sep 10, 2024

Adds utility functions for running CLI applications.

See documentation for details.

docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
this_foo: str


print(Settings(_cli_parse_args=['--this_foo', 'is such a foo']).model_dump())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed suggestion of using private param _cli_parse_args. The "formal" equivalent is now:

CliApp.run(Settings, cli_args=['--this_foo', 'is such a foo']).model_dump()

@@ -1211,13 +1287,15 @@ cli_settings = CliSettingsSource(Settings, root_parser=parser)

# Parse and load CLI settings from the command line into the settings source.
sys.argv = ['example.py', '--food', 'kiwi', '--name', 'waldo']
print(Settings(_cli_settings_source=cli_settings(args=True)).model_dump())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed suggestions of using private param _cli_settings_source for its formal equivalent.

@@ -33,7 +40,6 @@ class SettingsConfigDict(ConfigDict, total=False):
env_parse_enums: bool | None
cli_prog_name: str | None
cli_parse_args: bool | list[str] | tuple[str, ...] | None
cli_settings_source: CliSettingsSource[Any] | None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug. It's not possible to provide cli_settings_source as a configuration setting as the settings_cls cannot be defined (circular dependency).

@kschwab
Copy link
Contributor Author

kschwab commented Sep 10, 2024

@hramezani this is ready for review.

I removed most the scope originally discussed in #335 to avoid breaking changes and keep maintenance light. i.e., CliApp is similar in nature to get_subcommand, no structural changes etc. This is the last of the feature adds I had planned 👍🏾

I think any remaining items can depend on demand from the community.

@hramezani
Copy link
Member

Thanks @kschwab for this PR.

I have some concerns about the cli settings source, it has a lot of code and I think maintaining this amount of code would be hard in the future.

Specially about CLI App, Is it requested by somebody?

@kschwab
Copy link
Contributor Author

kschwab commented Sep 12, 2024

@hramezani, yes, this was requested and discussed in #335 and #336. It is related to discussion we had on get_subcomand, where this functionality for CLI is somewhat standard. I think drawing the line here for feature support makes sense.

Also, I understand the LOC concern for CliSettingsSource, however the root of this is in sources.py. With everything now in and understood, it can be simplified. I was planning on addressing this separately.

@hramezani
Copy link
Member

@kschwab Don't we need more tests for this patch? I can't see any test for CliApp.run in particular. did I miss something?

@kschwab
Copy link
Contributor Author

kschwab commented Sep 17, 2024

@hramezani it's mostly a superficial commit. The doc tests cover all happy paths, and I added tests in test_settings.py for all exception paths. Would you like me to copy the doc tests into test_settings.py?

Copy link
Contributor

@hyperlint-ai hyperlint-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 files reviewed, 5 outstanding issue(s) found.

docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
@kschwab kschwab marked this pull request as draft September 21, 2024 15:36
@kschwab kschwab marked this pull request as ready for review September 21, 2024 18:45
@kschwab
Copy link
Contributor Author

kschwab commented Sep 21, 2024

@hramezani, I added more tests specific to CliApp.run and updated all other test references using Settings(_cli_parse_args=...) to instead use CliApp.run(Settings, cli_args=...) where relevant.

@hramezani
Copy link
Member

Thanks @kschwab.

I will check it next week

@hramezani
Copy link
Member

Thanks @kschwab

@hramezani hramezani merged commit 84cab2b into pydantic:main Sep 23, 2024
20 checks passed
@kschwab kschwab deleted the cli-app-support branch September 26, 2024 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants