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

Allow variadic CliPositionalArg to accept an empty list, or have a default value #524

Closed
jploskey opened this issue Jan 15, 2025 · 2 comments
Assignees

Comments

@jploskey
Copy link

Thank you so much for implementing #519!

However, i've pulled down the changes for testing, and did notice that there doesn't seem to be a way to have a command accept an arbitrary number of positional arguments that can be empty.

# File: example.py

from pydantic_settings import BaseSettings, CliPositionalArg, SettingsConfigDict


class Main(BaseSettings):
    model_config = SettingsConfigDict(
        cli_parse_args=True,
        cli_enforce_required=True,
    )

    values: CliPositionalArg[list[str]]


parsed = Main()
print(parsed)

Output when running as python example.py:

usage: that.py [-h] VALUES
example.py: error: the following arguments are required: VALUES

So, to work around this I've tried using both values: CliPositionalArg[list[str]] = [] and values: CliPositionalArg[list[str] | None] = None instead, but both result in pydantic_settings.sources.SettingsError: positional argument Main.values has a default value.

My use case is similar to a command like docker compose up, where when no positional arguments are passed there's different behavior.

docker compose up # Run all services.
docker compose up my-app postgres # Run specific services.
@jploskey jploskey changed the title Allow variadic positional arguments to accept a list of no values, or have a default value Allow variadic positional arguments to accept an empty list, or have a default value Jan 15, 2025
@hramezani
Copy link
Member

@kschwab do you have time to take a look?

@jploskey jploskey changed the title Allow variadic positional arguments to accept an empty list, or have a default value Allow variadic CliPositionalArg to accept an empty list, or have a default value Jan 15, 2025
@jploskey
Copy link
Author

Sorry, this was my mistake. The version of pydantic-settings my virtualenv was using was incorrect. values: CliPositionalArg[list[str]] = [] does in fact work. I'm going to go ahead and close 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