Skip to content

Regression of case sensitive environment variable support #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
slingshotsys opened this issue Sep 23, 2024 · 7 comments
Closed

Regression of case sensitive environment variable support #419

slingshotsys opened this issue Sep 23, 2024 · 7 comments
Assignees
Labels
duplicate This issue or pull request already exists

Comments

@slingshotsys
Copy link

slingshotsys commented Sep 23, 2024

As of v2.5.0 case sensitive environment variables no longer function properly:

import os

from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict

class NestedSettings(BaseModel):
    A: str
    b: int

class Settings(BaseSettings):
    not_nested: str
    NESTED: NestedSettings | None
    NestedTwo: NestedSettings | None

    model_config = SettingsConfigDict(
        case_sensitive=True,
        extra="forbid",
    )

os.environ["not_nested"] = "test"
os.environ["NESTED"] = '{"A": "10", "b": "10"}'
os.environ["NestedTwo"] = '{"a": "10", "b": "10"}'

print(Settings())

result:

pydantic_core._pydantic_core.ValidationError: 2 validation errors for Settings
not_nested
  Field required [type=missing, input_value={'NESTED': {'a': '10', 'b': '10'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/missing
NestedTwo
  Field required [type=missing, input_value={'NESTED': {'a': '10', 'b': '10'}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.9/v/missing

changing case_sensitive to False works as expected.

see: #295

@hramezani
Copy link
Member

Thanks @slingshotvfx for reporting.

Is it happening on Windows?

@slingshotsys
Copy link
Author

On Windows 11 in Python 3.12.1

I misspoke actually, this is a regression maybe all the way back to v2.2.0

@hramezani
Copy link
Member

Thanks for your quick response @slingshotvfx .

my question is why do you enable the case_sensitive in your model. as you said you are using windows and this config doesn't have any effect on windows:

from the doc:
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.

@slingshotsys
Copy link
Author

Our code is cross-platform and we utilize case_sensitive for our Linux deployments. I admit having case_sensitive=True in windows code is misleading if it really does nothing, but ideally it would still work and not throw a validation error.

@hramezani
Copy link
Member

One solution would be to set case_sensitive=False when pydantic-settings in on Windows.

But you can do it on your side as well. like

case_sensitive=False if sys.platform.startswith('win') else True,

@slingshotsys
Copy link
Author

I suppose that would work. The docs are a bit misleading though, since the case_sensitive config setting will have no effect implies that it doesn't matter if it's True or False and yet the behavior does change on windows when it's set to True.

I actually bumped into this while troubleshooting #420, but I'm realizing now that that issue is unrelated and I think this issue is just a duplicate of #295 rather than a regression. Apologies for the confusion.

@hramezani
Copy link
Member

No worries, let's mark this one as duplicate and close.

@hramezani hramezani added duplicate This issue or pull request already exists and removed unconfirmed labels Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants