Skip to content

Commit eb72764

Browse files
committed
Cache empty dotenv dicts
1 parent fa4e6a9 commit eb72764

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/dotenv/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def _get_stream(self) -> Iterator[IO[str]]:
7474

7575
def dict(self) -> Dict[str, Optional[str]]:
7676
"""Return dotenv as dict"""
77-
if self._dict:
77+
if self._dict is not None:
7878
return self._dict
7979

8080
raw_values = self.parse()

tests/test_main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pytest
1111

1212
import dotenv
13+
from dotenv.main import DotEnv
1314

1415

1516
def test_set_key_no_file(tmp_path):
@@ -227,6 +228,18 @@ def test_get_key_none(dotenv_path):
227228
mock_warning.assert_not_called()
228229

229230

231+
def test_empty_dotenv_dict_is_cached(tmp_path):
232+
dotenv_path = tmp_path / ".env"
233+
dotenv_path.write_text("")
234+
dotenv_obj = DotEnv(dotenv_path)
235+
236+
with mock.patch.object(dotenv_obj, "parse", wraps=dotenv_obj.parse) as mock_parse:
237+
assert dotenv_obj.dict() == {}
238+
assert dotenv_obj.dict() == {}
239+
240+
assert mock_parse.call_count == 1
241+
242+
230243
def test_unset_with_value(dotenv_path):
231244
logger = logging.getLogger("dotenv.main")
232245
dotenv_path.write_text("a=b\nc=d")

0 commit comments

Comments
 (0)