Skip to content

Add on_duplicate parameter to handle duplicate keys#643

Open
akshattalwar001 wants to merge 2 commits intotheskumar:mainfrom
akshattalwar001:feature/on-duplicate-keys
Open

Add on_duplicate parameter to handle duplicate keys#643
akshattalwar001 wants to merge 2 commits intotheskumar:mainfrom
akshattalwar001:feature/on-duplicate-keys

Conversation

@akshattalwar001
Copy link
Copy Markdown

Closes #591

Adds on_duplicate parameter to DotEnv, load_dotenv, and dotenv_values with three modes:

  • warn (default): logs a warning, latter value wins
  • raise: raises ValueError on first duplicate found
  • ignore: silently uses latter value, old behaviour explicit

Closes theskumar#591

Adds on_duplicate parameter to DotEnv, load_dotenv, and dotenv_values
with three modes:
- warn (default): logs a warning, latter value wins
- raise: raises ValueError on first duplicate found
- ignore: silently uses latter value, old behaviour explicit
Copilot AI review requested due to automatic review settings May 5, 2026 16:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new on_duplicate option to the core dotenv parsing APIs to control behavior when duplicate keys are encountered, addressing #591 and making duplicate handling explicit and configurable.

Changes:

  • Introduces on_duplicate parameter on DotEnv, load_dotenv, and dotenv_values with modes warn (default), raise, and ignore.
  • Implements duplicate detection during parsing, including file/stream and line-number context in warnings/errors.
  • Adds a dedicated test suite covering each duplicate-handling mode and invalid option validation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/dotenv/main.py Adds on_duplicate plumbing + duplicate detection/warn/raise behavior in DotEnv.parse(), and wires the parameter through load_dotenv/dotenv_values.
tests/test_on_duplicate.py Adds tests verifying warning emission, exception behavior, ignore behavior, and option validation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_on_duplicate.py Outdated
Comment on lines +45 to +49
with patch.object(logging.getLogger("dotenv.main"), "warning") as mock_warn:
dotenv.load_dotenv(env_file, override=True, on_duplicate="warn")
assert mock_warn.called
assert "Duplicate key" in mock_warn.call_args[0][0]
del os.environ["MYKEY"]
Comment thread src/dotenv/main.py
Comment on lines 414 to 420
override: bool = False,
interpolate: bool = True,
encoding: Optional[str] = "utf-8",
on_duplicate: str = "warn",
) -> bool:
"""Parse a .env file and then load all the variables found as environment variables.

Comment thread src/dotenv/main.py
Comment on lines 465 to 471
verbose: bool = False,
interpolate: bool = True,
encoding: Optional[str] = "utf-8",
on_duplicate: str = "warn",
) -> Dict[str, Optional[str]]:
"""
Parse a .env file and return its content as a dict.
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.

Feature Request: Exception or Warning on Duplicate Configuration Items

2 participants