feat: unlink_after_load - unlink .env file after calling load_dotenv #585
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.
Add
unlink_after_load
parameter toload_dotenv()
functionSummary
This PR adds a new optional boolean parameter
unlink_after_load
to theload_dotenv()
function that allows users to automatically remove the .env file after it has been successfully loaded into the environment.Motivation
This feature is useful for scenarios where:
Changes
API Changes
Added a new parameter to
load_dotenv()
:unlink_after_load
(bool, default:False
): Whether to remove the .env file after successfully loading itImplementation Details
dotenv_path
is provided (ignored when usingstream
parameter)Usage Examples
Basic Usage
With Other Parameters
Temporary Configuration Pattern
Error Handling
The function handles various edge cases gracefully:
False
as usualunlink_after_load
parameter is ignored when usingstream
instead ofdotenv_path
Testing
Added comprehensive test suite covering:
unlink_after_load=True
)unlink_after_load=False
)All existing tests continue to pass, ensuring no breaking changes.
Backward Compatibility
This change is fully backward compatible:
False
, maintaining existing behaviorDocumentation
Type: Enhancement
Breaking Change: No
Tests Added: Yes (8 new test cases)
Documentation Updated: Yes