Closed slingshotvfx closed 1 month ago
@hramezani,
Could you please take a look at this? Thanks!
Thanks @slingshotvfx for reporting this bug. I will investigate and try to prepare a fix
@slingshotvfx I created https://github.com/pydantic/pydantic-settings/pull/294 to fix the problem. Could you please confirm it?
Hey @hramezani, confirmed that #294 fixes the issues I was having with .env files, thanks!
This might be a separate issue, but I'm still a bit confused by 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:
works
works
does NOT work
works
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
.
@slingshotvfx Thanks for checking. I am going to merge the PR and it will close the issue. It would be great to create a new issue for the things that you mentioned in your last comment.
Initial Checks
Description
When using nested
pydantic-settings
models, I am seeing unexpected behavior with the case_sensitive flag when reading from a .env file.When
case_sensitive=True
, things work as expected and are case sensitive, even though the docs say that on windows things should always be case insensitive.However, when
case_sensitive=False
, I getValidationError
s forOptional
nested models regardless of what case I use.Example Code
Changing
case_sensitive=True
and re-casing the .env file (e.g.nested__A=works
) solves the issue. Removing the| None = None
solves the issue too, although then of course the nested setting are required.Python, Pydantic & OS Version