Skip to content

cli_kebab_case="all" doesnt work with CliImplicitFlag[bool] #701

@Digity101

Description

@Digity101

Related to to #686 and #685

from pydantic import Field
from pydantic_settings import BaseSettings, CliImplicitFlag, SettingsConfigDict


class Settings(BaseSettings):
    model_config = SettingsConfigDict(
        cli_parse_args=True,
        cli_kebab_case="all",
    )

    test_bool_flag: CliImplicitFlag[bool] = Field(
        False,
    )


if __name__ == "__main__":
    settings = Settings()
    print(settings.test_bool_flag)

Calling the above code with main.py --test-bool-flag gives the following error:

Traceback (most recent call last):
  File "C:\Users\user\Documents\pydantic-settings-test\main.py", line 17, in <module>
    settings = Settings()
               ^^^^^^^^^^
  File "C:\Users\user\Documents\pydantic-settings-test\.venv\Lib\site-packages\pydantic_settings\main.py", line 194, in __init__
    **__pydantic_self__._settings_build_values(
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\pydantic-settings-test\.venv\Lib\site-packages\pydantic_settings\main.py", line 402, in _settings_build_values
    cli_settings = CliSettingsSource[Any](
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\typing.py", line 1157, in __call__
    result = self.__origin__(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\pydantic-settings-test\.venv\Lib\site-packages\pydantic_settings\sources\providers\cli.py", line 420, in __init__
    self._load_env_vars(parsed_args=self._parse_args(self.root_parser, cli_parse_args))
  File "C:\Users\user\Documents\pydantic-settings-test\.venv\Lib\site-packages\pydantic_settings\sources\providers\cli.py", line 506, in _load_env_vars
    selected_subcommands = self._resolve_parsed_args(parsed_args)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Documents\pydantic-settings-test\.venv\Lib\site-packages\pydantic_settings\sources\providers\cli.py", line 552, in _resolve_parsed_args
    snake_val = val.replace('-', '_')
                ^^^^^^^^^^^
AttributeError: 'bool' object has no attribute 'replace'

Using cli_kebab_case=True, gives the correct behaviour.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions