Skip to content

Is EnvSettingsSource's behavior intentional or bug? #258

Open
@qkrwoghk15

Description

@qkrwoghk15

Hi, I was using the dotenv related settings in pydantic BaseSettings and found something weird.

Nested 'BaseModel' is used for 'BaseSettings', and the alias of the inner model operate dependently on each other as it is set with 'case_sensitive' in the outer model.

Below is an example code for the above situation.
when outer Model's case_sensitive is False,

import os

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

class MyModel(BaseModel):
    a: int = Field(alias="A")


class MySettings(BaseSettings):
    uid: str
    name: str
    inner: MyModel

    model_config = SettingsConfigDict(env_nested_delimiter="__", case_sensitive=False)

def test(environ_name:str):
    os.environ[environ_name] = "3"
    
    try:
        settings = MySettings(uid="uid", name="name")
    except Exception as e:
        print(e)
    else:
        print(settings)
    finally:
        os.environ.pop(environ_name)

then

>>> test("inner__A")
1 validation error for MySettings
inner.A
  Field required [type=missing, input_value={'a': '3'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing

but when outer Model's case_sensitive is True,

...

class MySettings(BaseSettings):
    uid: str
    name: str
    inner: MyModel

    model_config = SettingsConfigDict(env_nested_delimiter="__", case_sensitive=True)

...

then the result is below.

>>> test("inner__A")
uid='uid' name='name' inner=MyModel(a=3)

I think it's a movement that comes from this line
Is this an intended action or is it a bug?

Thanks for reading my long issue

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions