Skip to content

Conversation

@chovanecadam
Copy link

When a field defaults to None, displaying (default: null) provides no additional information to users. The absence of a default value already implies None/null for optional fields, and this matches the behavior of most CLI tools (argparse, click) where optional arguments don't display default: None. Users expect optional CLI arguments to default to None implicitly.

This change aligns pydantic-settings with standard CLI tool conventions (argparse, click) where None defaults are hidden. Also, this reduces visual noise in help messages:

class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        cli_hide_none_type=True,
    )

    foo: Path | None
    bar: str | None
    baz: str | None = None
    timeout: int = 50
    config: Path | None = None

before

usage: main.py [-h] [--foo Path] [--bar str] [--baz str] [--timeout int] [--config Path]

options:
  -h, --help     show this help message and exit
  --foo Path     (required)
  --bar str      (required)
  --baz str      (default: null)
  --timeout int  (default: 50)
  --config Path  (default: null)

after

usage: main.py [-h] [--foo Path] [--bar str] [--baz str] [--timeout int] [--config Path]

options:
  -h, --help     show this help message and exit
  --foo Path     (required)
  --bar str      (required)
  --baz str
  --timeout int  (default: 50)
  --config Path

As this MR alters the behavior of cli_hide_none_type, you might prefer adding another flag to support this, e.g. cli_hide_none_type_defaults?

@hramezani
Copy link
Member

Thanks @chovanecadam for this patch. We need a test for this.

@kschwab Could you please take a look when you have time?

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