Add on_duplicate parameter to handle duplicate keys#643
Open
akshattalwar001 wants to merge 2 commits intotheskumar:mainfrom
Open
Add on_duplicate parameter to handle duplicate keys#643akshattalwar001 wants to merge 2 commits intotheskumar:mainfrom
akshattalwar001 wants to merge 2 commits intotheskumar:mainfrom
Conversation
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
There was a problem hiding this comment.
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_duplicateparameter onDotEnv,load_dotenv, anddotenv_valueswith modeswarn(default),raise, andignore. - 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 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 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 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. |
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.
Closes #591
Adds on_duplicate parameter to DotEnv, load_dotenv, and dotenv_values with three modes: