Skip to content

Incorrect metavar generation in CliSettingsSource with Python 3.9 or earlier and Pydantic 2.10 #551

@KanchiShimono

Description

@KanchiShimono

When using Python 3.9 or earlier with Pydantic 2.10, metavar in CliSettingsSource is not generated correctly.

This issue is caused by changes introduced in Pydantic 2.10 related to WithArgsTypes. Since CliSettingsSource relies on WithArgsTypes for metavar generation, its behavior has changed unexpectedly.

elif isinstance(obj, WithArgsTypes):
return self._metavar_format_choices(
list(map(self._metavar_format_recurse, self._get_modified_args(obj))),
obj_qualname=obj.__qualname__ if hasattr(obj, '__qualname__') else str(obj),
)

This issue does not occur in Python 3.10 or later.

Reproduction Code

import sys
from typing import List

from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(cli_parse_args=True)

    val: List[str]


sys.argv = ['main.py', '-h']
Settings()

When running this code, the output looks like this. The type of val is displayed as typing.List[str][str], but it should be List[str].

usage: main.py [-h] [--val typing.List[str][str]]

optional arguments:
  -h, --help            show this help message and exit
  --val typing.List[str][str]
                        (required)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions