-
-
Notifications
You must be signed in to change notification settings - Fork 115
Open
Labels
Description
In the following example i am adding two external cli arguments with action append in addition to a CliSettings source. Notice that after pydantic-settings parses the arguments the type and value of those external arguments change from list to str.
Depending on the contents of the list the change is different even; a list[str] changes to '["abc"]' but a list[int] changes to the empty string.
from pydantic_settings import (
CliApp,
CliSettingsSource,
SettingsConfigDict,
BaseSettings,
)
import pydantic_settings
import argparse
class CLISettings(BaseSettings):
model_config = SettingsConfigDict(cli_parse_args=True)
def parse_args():
print(f"{pydantic_settings.VERSION=}")
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter, description=__doc__
)
cli_settings = CliSettingsSource(CLISettings, root_parser=parser)
parser.add_argument(
"--test-cli",
action="append",
default=[1],
)
parser.add_argument(
"--test-cli2",
action="append",
default=["abc"],
)
parsed_args = parser.parse_args()
print(f"{parsed_args=}")
settings = CliApp.run(
CLISettings, cli_args=parsed_args, cli_settings_source=cli_settings
)
print(f"{parsed_args=}")
return settings
def main() -> None:
parse_args()
if __name__ == "__main__":
main()$ uv run test-settings-nargs ❮❮
pydantic_settings.VERSION='2.12.0'
parsed_args=Namespace(test_cli=[1], test_cli2=['abc'])
parsed_args=Namespace(test_cli='', test_cli2='["abc"]')The culprit seems to be CliSettingsSource._merge_parsed_list(...) which always converts the result to str after merging. Note that this error also existed on version 2.10, so #669 did not introduce it.