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

Extra.Forbid is not applied when using BaseSettings with Config.env_file defined #11

Closed
5 of 15 tasks
jberthier-ma opened this issue Dec 7, 2022 · 7 comments · Fixed by #39
Closed
5 of 15 tasks
Labels
bug Something isn't working

Comments

@jberthier-ma
Copy link

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

Reading the documentation and as BaseSettings inherits from BaseModel it'd be expected to have Config.extra parameters working when Config.env_file is defined.

It appears that without Config.env files it works as expected, but when it comes from env_files the extra config is ignored.

If this is expected I can make a PR to make the documentation more explicit on this specific point.

Example Code

from pydantic import BaseSettings, Extra

""""
# .env.test
title="cool stuff"
other=42
"""

class Plop(BaseSettings):
    title: str

    class Config:
        extra: Extra = Extra.forbid
        env_file = ".env.test"

Plop()

Python, Pydantic & OS Version

pydantic version: 1.10.2
            pydantic compiled: True
                 install path: .venv/lib/python3.11/site-packages/pydantic
               python version: 3.11.0 (main, Oct 24 2022, 19:55:51) [GCC 9.4.0]
                     platform: Linux-5.17.0-051700-generic-x86_64-with-glibc2.31
     optional deps. installed: ['dotenv', 'typing-extensions']

Affected Components

@jberthier-ma jberthier-ma added the bug Something isn't working label Dec 7, 2022
@samuelcolvin samuelcolvin transferred this issue from pydantic/pydantic Dec 7, 2022
@samuelcolvin
Copy link
Member

Good catch, thanks for reporting. Realistically this won't be changed in pydantic v1.10, but we should make sure it's fixed in pydantic V2 where settings will become a separate package which uses pydantic.

@hramezani
Copy link
Member

@samuelcolvin Is it really a bug? It seems we loop over settings fields and collect the value for the fields from file or environment variables. So, we won't get the extra value provided in the file. Am I wrong?

@samuelcolvin
Copy link
Member

I haven't actually tried it.

We need to check the current behaviour then decide if it's correct or needs changing.

@hramezani
Copy link
Member

hramezani commented Dec 7, 2022

Currently we loop over fields and collect the values. If we are going to change the behavior, we need to collect the envs and then initialize the model. Then the default behavior(collect envs from environment without prefix and dotenv file) will be tricky because if we define a model with a field named test_field, then the other environment variables are extra. We may need to change it just for dotenv file or prefixed envs

@samuelcolvin
Copy link
Member

Ye, extra behaviour should definitely always be "ignore" for env vars, but for other sources like dotenv, I can see the value in allowing "forbid" or "include".

@hramezani
Copy link
Member

OK, that needs to be changed and documented

@jberthier-ma
Copy link
Author

jberthier-ma commented Dec 8, 2022

It's me who thanks you for all your amazing contributions to the python ecosystem !
Thanks for taking that behavior into account.

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

Successfully merging a pull request may close this issue.

3 participants