Skip to content

Add unload_dotenv() to reverse load_dotenv for tests - #703

Open
iamsaketh70 wants to merge 1 commit into
theskumar:mainfrom
iamsaketh70:feat/unload-dotenv
Open

iamsaketh70 wants to merge 1 commit into
theskumar:mainfrom
iamsaketh70:feat/unload-dotenv

Conversation

@iamsaketh70

Copy link
Copy Markdown

Summary

Adds unload_dotenv() so tests can undo load_dotenv() without leaking env vars into later cases.

Fixes #594

Behavior

  • Parses the same .env file as load_dotenv / dotenv_values.
  • Removes a key from os.environ only when the current value still matches the file, so a later explicit assignment is left alone.
  • Returns True if at least one variable was removed.

Tests

  • test_unload_dotenv_removes_loaded_values.
  • test_unload_dotenv_keeps_changed_values.
  • test_unload_dotenv_missing_file.

pytest tests/test_main.py::test_unload_dotenv_removes_loaded_values tests/test_main.py::test_unload_dotenv_keeps_changed_values tests/test_main.py::test_unload_dotenv_missing_file — 3 passed.

Keys are removed from os.environ only when the current value still matches the .env file, so later assignments are left alone.

Closes theskumar#594

Co-authored-by: Cursor <cursoragent@cursor.com>
Copilot AI lite review requested due to automatic review settings September 15, 2026 02:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical and moderate issues remain in the implementation and formatting.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds public unload_dotenv() functionality to clean environment variables loaded from .env files, with tests and documentation.

Changes:

  • Implements conditional environment cleanup.
  • Exports and documents the new helper.
  • Adds removal, preservation, and missing-file tests.
File summaries
File Changes Final findings
tests/test_main.py Adds unload behavior tests. None
src/dotenv/main.py Implements unload_dotenv(). Moderate (3 votes): Interpolation settings do not match default load_dotenv() behavior. Critical (3 votes): Matching values can cause caller-owned variables to be deleted.
src/dotenv/__init__.py Exports the new API. Critical (3 votes): Import formatting will fail the formatter check.
README.md Documents usage. None
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/dotenv/__init__.py
from typing import Any, Optional

from .main import dotenv_values, find_dotenv, get_key, load_dotenv, set_key, unset_key
from .main import dotenv_values, find_dotenv, get_key, load_dotenv, set_key, unload_dotenv, unset_key
Comment thread src/dotenv/main.py
Comment on lines +461 to +462
if os.environ.get(key) == value:
del os.environ[key]
Comment thread src/dotenv/main.py
Comment on lines +452 to +456
values = dotenv_values(
dotenv_path=dotenv_path,
stream=stream,
encoding=encoding,
)
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: Unloading function unload_dotenv()

2 participants