From cfba8fc7f70ad911315d63634430f98aead75755 Mon Sep 17 00:00:00 2001 From: Xiang Wang Date: Mon, 7 Mar 2022 23:46:16 +0800 Subject: [PATCH] feat: add with_env param in dotenv_values --- src/dotenv/main.py | 11 ++++++++++- tests/test_main.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 76ee5993..7021cd47 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -347,6 +347,7 @@ def dotenv_values( verbose: bool = False, interpolate: bool = True, encoding: Optional[str] = "utf-8", + with_env: bool = False, ) -> Dict[str, Optional[str]]: """ Parse a .env file and return its content as a dict. @@ -362,6 +363,7 @@ def dotenv_values( - `verbose`: whether to output a warning if the .env file is missing. Defaults to `False`. - `encoding`: encoding to be used to read the file. Defaults to `"utf-8"`. + - `with_env`: include the os.environ() to response. If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the .env file. @@ -369,7 +371,7 @@ def dotenv_values( if dotenv_path is None and stream is None: dotenv_path = find_dotenv() - return DotEnv( + result = DotEnv( dotenv_path=dotenv_path, stream=stream, verbose=verbose, @@ -377,3 +379,10 @@ def dotenv_values( override=True, encoding=encoding, ).dict() + if with_env: + return dict( + **os.environ, + **result, + ) + else: + return result diff --git a/tests/test_main.py b/tests/test_main.py index 364fc24d..00b5dccd 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -415,3 +415,9 @@ def test_dotenv_values_file_stream(dotenv_file): result = dotenv.dotenv_values(stream=f) assert result == {"a": "b"} + + +def test_dotenv_values_with_os_environment(): + if os.environ.get("USER"): + assert "USER" not in dotenv.dotenv_values(with_env=False) + assert "USER" in dotenv.dotenv_values(with_env=True)