From 5acbdeba34fe3531273a9f29c12cdc418fae655c Mon Sep 17 00:00:00 2001 From: SthPhoenix <17834919+SthPhoenix@users.noreply.github.com> Date: Fri, 14 Jul 2023 00:24:00 +0300 Subject: [PATCH] Add documentation about compatibility with pydantic 1.x BaseSettings when using dotenv (#127) Co-authored-by: Hasan Ramezani --- docs/index.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index e200f9c4..1ddf61ec 100644 --- a/docs/index.md +++ b/docs/index.md @@ -383,10 +383,20 @@ Because python-dotenv is used to parse the file, bash-like semantics such as `ex (depending on your OS and environment) may allow your dotenv file to also be used with `source`, see [python-dotenv's documentation](https://saurabh-kumar.com/python-dotenv/#usages) for more details. -Pydantic settings consider `extra` config in case of dotenv file. It means if you set the `extra=forbid` +Pydantic settings consider `extra` config in case of dotenv file. It means if you set the `extra=forbid` (*default*) on `model_config` and your dotenv file contains an entry for a field that is not defined in settings model, it will raise `ValidationError` in settings construction. +For compatibility with pydantic 1.x BaseSettings you should use `extra=ignore`: +```py +from pydantic_settings import BaseSettings, SettingsConfigDict + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_file='.env', extra='ignore') +``` + + ## Secrets Placing secret values in files is a common pattern to provide sensitive configuration to an application.