feat: accept None to 'set_key' to write value-less environment variable #454
+21
−14
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.
Changes the signature of
set_key
in the following ways:value_to_set
:str
toOptional[str]
Tuple[Optional[bool], str, str]
toTuple[Optional[bool], str, Optional[str]]
The purpose of this change is to allow a program to pass in
None
to thevalue_to_set
parameter, and therefore write the key to the dotenv file without a value. For example:The resulting
.env
file:The use-case is pretty specific, but I think valuable: I'm writing a Python script to parse a file to find environment variables being used, and then writing those to a gitignored
.env
file, so that running the script creates a.env
file with stubbed values. By allowingNone
, any defaults defined in the config file will be used. In contrast, if the script wrote empty strings or mock values, the defaults would not be used.Here's an example for illustration:
After running the Python script to parse this file, the resulting
.env
file should contain something like:Note: This is technically a breaking change
(the change to the return value of
set_key
is not backward-compatible). If this is a problem, we can instead return empty string in the case whereNone
is passed forvalue_to_set
.