feat: add strict parameter to load_dotenv() and dotenv_values()#632
Open
AhsanSheraz wants to merge 2 commits intotheskumar:mainfrom
Open
feat: add strict parameter to load_dotenv() and dotenv_values()#632AhsanSheraz wants to merge 2 commits intotheskumar:mainfrom
strict parameter to load_dotenv() and dotenv_values()#632AhsanSheraz wants to merge 2 commits intotheskumar:mainfrom
Conversation
3fab2a8 to
1ced9d9
Compare
Add opt-in strict mode that raises exceptions instead of silently ignoring errors. When `strict=True`: - `FileNotFoundError` is raised if the .env file is not found - `ValueError` is raised if any line cannot be parsed, with line number Defaults to `False` for full backwards compatibility. Closes theskumar#631 Related: theskumar#467, theskumar#297, theskumar#321, theskumar#520, theskumar#591
1ced9d9 to
bdee098
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
strictparameter (defaultFalse) toload_dotenv()anddotenv_values()that enables fail-fast behaviorstrict=True, raisesFileNotFoundErrorif the .env file is missingstrict=True, raisesValueErrorwith line number if any line cannot be parsedstrict=False(default)Motivation
Silent failures are the #1 complaint about python-dotenv across GitHub issues, blog posts, and community discussions:
load_dotenv()used to returnTrueeven when file not found (fixed by feat: return False when we do not discover any environment variables #388, but no exception option)The DEV Community article "Why load_dotenv() Is an Anti-Pattern" specifically cites silent failures as the primary reason developers migrate to alternatives.
Real-world impact from PR #520: "I dealt with an .env file that accidentally contained an unparsable line. The software then set a default value and I almost wrote to a wrong database."
Design Philosophy — Parser Correctness, Not Config Validation
This PR intentionally stays within python-dotenv's existing philosophy of "populate what is available, let consuming code validate requirements."
strictmode does not validate whether specific keys exist, whether values are the correct type, or whether the configuration is "complete" — that is the domain of tools like pydantic-settings.What
strictdoes is make python-dotenv honest about its own job: "did the file I was asked to read exist?" and "could I parse every line in it?" These are parser-level guarantees, not application-level config validation.Usage
What changes
strict=False(default)strict=TrueFalseFileNotFoundErrorlogger.warning()ValueErrorwith line numberTrueTrueInteraction with
verbosestricttakes precedence oververbose. When both areTrue, the exception is raised without emitting a warning first — logging the same message before raising would be redundant since the exception already carries the information. Whenstrict=False,verbosecontinues to work exactly as before.strictverboseFalseFalseFalse)FalseTruelogger.info()warningTrueFalseFileNotFoundErrorTrueTrueFileNotFoundError(no warning logged)Changes
src/dotenv/main.py: Addedstrictparameter toDotEnv.__init__(),load_dotenv(),dotenv_values(), andwith_warn_for_invalid_lines(). Docstrings explicitly documentstrictvsverboseprecedence.tests/test_main.py: Added 14 tests covering all strict mode scenarios + backwards compatibility verificationTest plan
ruff checkpassesruff format --checkpassesstrictdefaults toFalse, no behavior change for existing usersCloses #631
Related: #467, #297, #321, #520, #591, #164