Skip to content
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

Regression - .env nested optional model support #420

Closed
slingshotvfx opened this issue Sep 23, 2024 · 4 comments
Closed

Regression - .env nested optional model support #420

slingshotvfx opened this issue Sep 23, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@slingshotvfx
Copy link

slingshotvfx commented Sep 23, 2024

As of v2.5.0, the following code fails:

# .env
not_nested=works
NESTED__A=fails
NESTED__b=2
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

    model_config = SettingsConfigDict(
        env_file=".env",
        env_nested_delimiter="__",
        extra="forbid",
    )

print(Settings())

result:

pydantic_core._pydantic_core.ValidationError: 2 validation errors for Settings
nested__a
  Extra inputs are not permitted [type=extra_forbidden, input_value='fails', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden
nested__b
  Extra inputs are not permitted [type=extra_forbidden, input_value='2', input_type=str]
    For further information visit https://errors.pydantic.dev/2.9/v/extra_forbidden

setting extra="allow" ends up with duplicate NESTED and nested__* fields on the model:

not_nested='works' NESTED=NestedSettings(A='fails', b=2) nested__a='fails' nested__b='2'

removing | None from NESTED: NestedSettings works as expected when forbid is both True or False:

not_nested='works' NESTED=NestedSettings(A='fails', b=2)

Using Python 3.12 on Windows 11. This worked as expected in 2.4.0

@hramezani
Copy link
Member

Thanks @slingshotvfx for reporting this bug.

I will prepare a fix for it.

@hramezani hramezani added bug Something isn't working and removed unconfirmed labels Sep 25, 2024
@hramezani
Copy link
Member

I created #423 as a fix.

Could you please confirm the fix?

@slingshotvfx
Copy link
Author

Confirmed #423 fixes the issue for me.

Thanks!

@hramezani
Copy link
Member

Thanks @slingshotvfx for checking!

Fixed in f3a25f2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants