Skip to content

Commit ee15221

Browse files
larskstheskumar
andauthored
feat: return False when we do not discover any environment variables (#388)
* Fix docstring for load_dotenv The docstring for load_dotenv was missing a word, rendering it confusing. This commits modifies it for clarity. * Return False when we do not discover any environment variables This modifies Dotenv.set_as_environment_variables to return False if we have not discovered any environment variables via either `dotenv_path` or `stream`. The return value gets passed through to `load_dotenv`, so this can be used to determine if `dotenv.load_dotenv` was able to set anything. Closes #321 Co-authored-by: Saurabh Kumar <theskumar@users.noreply.github.com>
1 parent 29bceb8 commit ee15221

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/dotenv/main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ def set_as_environment_variables(self) -> bool:
8787
"""
8888
Load the current dotenv as system environment variable.
8989
"""
90+
if not self.dict():
91+
return False
92+
9093
for k, v in self.dict().items():
9194
if k in os.environ and not self.override:
9295
continue
@@ -324,6 +327,8 @@ def load_dotenv(
324327
override: Whether to override the system environment variables with the variables
325328
from the `.env` file.
326329
encoding: Encoding to be used to read the file.
330+
Returns:
331+
Bool: True if atleast one environment variable is set elese False
327332
328333
If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
329334
.env file.

tests/test_main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ def test_load_dotenv_no_file_verbose():
259259
logger = logging.getLogger("dotenv.main")
260260

261261
with mock.patch.object(logger, "info") as mock_info:
262-
dotenv.load_dotenv('.does_not_exist', verbose=True)
262+
result = dotenv.load_dotenv('.does_not_exist', verbose=True)
263263

264+
assert result is False
264265
mock_info.assert_called_once_with("Python-dotenv could not find configuration file %s.", ".does_not_exist")
265266

266267

0 commit comments

Comments
 (0)