Skip to content

feat: add strict parameter to load_dotenv() and dotenv_values()#632

Open
AhsanSheraz wants to merge 2 commits intotheskumar:mainfrom
AhsanSheraz:feature/strict-mode
Open

feat: add strict parameter to load_dotenv() and dotenv_values()#632
AhsanSheraz wants to merge 2 commits intotheskumar:mainfrom
AhsanSheraz:feature/strict-mode

Conversation

@AhsanSheraz
Copy link

@AhsanSheraz AhsanSheraz commented Mar 15, 2026

Summary

  • Adds an opt-in strict parameter (default False) to load_dotenv() and dotenv_values() that enables fail-fast behavior
  • When strict=True, raises FileNotFoundError if the .env file is missing
  • When strict=True, raises ValueError with line number if any line cannot be parsed
  • 100% backwards compatible — existing behavior unchanged when strict=False (default)

Motivation

Silent failures are the #1 complaint about python-dotenv across GitHub issues, blog posts, and community discussions:

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." strict mode 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 strict does 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

from dotenv import load_dotenv

# Existing behavior preserved (default)
load_dotenv()

# Opt-in strict mode — fail fast on missing file or parse errors
load_dotenv(strict=True)

# Works with dotenv_values too
from dotenv import dotenv_values
values = dotenv_values(".env", strict=True)

What changes

Scenario strict=False (default) strict=True
File not found Returns False Raises FileNotFoundError
Unparseable line logger.warning() Raises ValueError with line number
Valid file Returns True Returns True

Interaction with verbose

strict takes precedence over verbose. When both are True, 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. When strict=False, verbose continues to work exactly as before.

strict verbose Missing file behavior
False False Silent (returns False)
False True logger.info() warning
True False Raises FileNotFoundError
True True Raises FileNotFoundError (no warning logged)

Changes

  • src/dotenv/main.py: Added strict parameter to DotEnv.__init__(), load_dotenv(), dotenv_values(), and with_warn_for_invalid_lines(). Docstrings explicitly document strict vs verbose precedence.
  • tests/test_main.py: Added 14 tests covering all strict mode scenarios + backwards compatibility verification

Test plan

  • All 127 tests pass (113 existing + 14 new)
  • ruff check passes
  • ruff format --check passes
  • Backwards compatibility verified: strict defaults to False, no behavior change for existing users

Closes #631
Related: #467, #297, #321, #520, #591, #164

@AhsanSheraz AhsanSheraz force-pushed the feature/strict-mode branch 2 times, most recently from 3fab2a8 to 1ced9d9 Compare March 15, 2026 12:21
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add strict parameter to load_dotenv() and dotenv_values() for fail-fast behavior

2 participants