diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 491634d9..c4f555ce 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -74,7 +74,7 @@ def _get_stream(self) -> Iterator[IO[str]]: def dict(self) -> Dict[str, Optional[str]]: """Return dotenv as dict""" - if self._dict: + if self._dict is not None: return self._dict raw_values = self.parse() diff --git a/tests/test_main.py b/tests/test_main.py index 50703af0..1c33c808 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -10,6 +10,7 @@ import pytest import dotenv +from dotenv.main import DotEnv def test_set_key_no_file(tmp_path): @@ -227,6 +228,18 @@ def test_get_key_none(dotenv_path): mock_warning.assert_not_called() +def test_empty_dotenv_dict_is_cached(tmp_path): + dotenv_path = tmp_path / ".env" + dotenv_path.write_text("") + dotenv_obj = DotEnv(dotenv_path) + + with mock.patch.object(dotenv_obj, "parse", wraps=dotenv_obj.parse) as mock_parse: + assert dotenv_obj.dict() == {} + assert dotenv_obj.dict() == {} + + assert mock_parse.call_count == 1 + + def test_unset_with_value(dotenv_path): logger = logging.getLogger("dotenv.main") dotenv_path.write_text("a=b\nc=d")