pydantic / pydantic-settings

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

environment variables case sensitivity on windows #295

Open slingshotvfx opened 1 month ago

slingshotvfx commented 1 month ago

According to the docs :

On Windows, Python's os module always treats environment variables as case-insensitive, so the case_sensitive config setting will have no effect - settings will always be updated ignoring case.

In my tests on windows 11:

The example given in the docs fails for me:

class RedisSettings(BaseModel):
    host: str
    port: int

class Settings(BaseSettings, case_sensitive=True):
    redis: RedisSettings

os.environ["redis"] = '{"host": "localhost", "port": 6379}'
print(Settings().model_dump())
pydantic_core._pydantic_core.ValidationError: 1 validation error for Settings
redis
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.7/v/missing

But it works if I set case_sensitive=False.

hramezani commented 1 month ago

Thanks @slingshotvfx for your investigation. I am not a Windows user. probably, we need to update the doc. Would you like to create a PR?

slingshotvfx commented 1 month ago

I'm actually not sure what the expected behavior is to update the docs.

When case_sensitive=True I get a validation error even when my cases do match, which seems more like a bug?

hramezani commented 1 month ago

Ok, yes, it seems to be a bug in Windows.