Skip to content

env_prefix prevents env_file from working #215

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
1 task done
red8888 opened this issue Jan 22, 2024 · 4 comments · Fixed by #218
Closed
1 task done

env_prefix prevents env_file from working #215

red8888 opened this issue Jan 22, 2024 · 4 comments · Fixed by #218
Assignees
Labels
bug Something isn't working

Comments

@red8888
Copy link

red8888 commented Jan 22, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Assuming this is a bug because I found nothing in the docs explaining this behavior.

Spent forever trying to understand why env_file wasn't working. Commenting out env_prefix cause env_file to start working. This can't be by design right? I should be able to define both.

Example Code

model_config = SettingsConfigDict(
        env_file=[
            "my.env",
        ],
        # When uncommented pydantic does not attempt to load any files defined in env_file 
        # env_prefix="my_app_",
    )

Python, Pydantic & OS Version

pydantic version: 2.5.3
        pydantic-core version: 2.14.6
          pydantic-core build: profile=release pgo=true
                 install path: /usr/local/lib/python3.12/site-packages/pydantic
               python version: 3.12.1 (main, Dec 19 2023, 20:23:36) [GCC 10.2.1 20210110]
                     platform: Linux-6.5.11-linuxkit-x86_64-with-glibc2.31
             related packages: pydantic-settings-2.1.0 mypy-1.8.0 typing_extensions-4.9.0

Edit

I found the issue. The issue is that setting env_prefix causes pydantic to ignore non-prefixed env vars defined in you dotenv files.

I was expecting it to fail because I had un-prefixed vars defined in mysettings.env for example:

MY_SETTING_ONE=ksjdlksjd
MY_SETTING_TWO=zzzzzzz

Changing those to this worked:

MY_APP_MY_SETTING_ONE=ksjdlksjd
MY_APP_MY_SETTING_TWO=zzzzzzz

So I think the bug is pydantic should not be ignoring un-prefixed fields in dotenv files.

Because while this correctly throws Extra inputs are not permitted:

MY_APP_SETTING_THAT_DOESNT_EXIST=ksjdlksjd

This throws no errors at all:

SETTING_THAT_DOESNT_EXIST=ksjdlksjd
@hramezani hramezani self-assigned this Jan 23, 2024
@hramezani hramezani transferred this issue from pydantic/pydantic Jan 23, 2024
@hramezani
Copy link
Member

Thanks @red8888 for reporting this.

Agree, that is probably a bug.

@hramezani hramezani added bug Something isn't working and removed unconfirmed labels Jan 23, 2024
@kjithin
Copy link
Contributor

kjithin commented Jan 24, 2024

Hi @hramezani
I'm interested in taking on this task. Given my limited experience in open source, I may require your assistance and guidance to successfully complete it. Would you consider assigning this issue to me?

@hramezani hramezani assigned kjithin and unassigned hramezani Jan 24, 2024
@hramezani
Copy link
Member

Thanks @kjithin, I've assigned it to you!

kjithin added a commit to kjithin/pydantic-settings that referenced this issue Jan 25, 2024
…Source when env_prefix is specified

The current implementation in DotEnvSettingsSource allows non-prefixed variables even when extra='forbid'(default) is set . This commit addresses the issue by raising a SettingsError when extra is set to 'forbid' and variables are encountered without using the specified prefix.
@kjithin
Copy link
Contributor

kjithin commented Jan 25, 2024

Thanks @hramezani. I've submitted a pull request addressing this issue. Kindly review and share your feedback.

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
3 participants