pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
502 stars 47 forks source link

CliSettingSource fails to handle `AliasChoices` and `AliasPath` #306

Closed kschwab closed 3 weeks ago

kschwab commented 1 month ago

From #303, CliSettingsSource does not handle AliasChoices correctly:

import pytest

from pydantic import AliasChoices, Field
from pydantic_settings import BaseSettings

def test_settings():
    class Check(BaseSettings, cli_parse_args=[]):
        field: str = Field(
            "foo",
            alias=AliasChoices(
                "ALIAS_NAME"
            )
        )

    assert Check().field == 'foo'
    assert Check(ALIAS_NAME="bar").field == 'bar'
kschwab commented 1 month ago

@hramezani I'll try to get to this soon. However, at first glance, I don't think CLI will be able to handle AliasChoices if it has multiple aliases. We will likely be limited to a single alias on the CLI. I will give it some thought.

hramezani commented 1 month ago

Thanks @kschwab for the issue. I am ok to limit it to single alias if it is not possible to handle. But we need to mention this limitation in the documentation .

kschwab commented 3 weeks ago

I was able to sort it out, we can support both AliasChoices and AliasPath.