diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 7bc54285..e4043bcd 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -362,6 +362,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. @@ -375,6 +376,7 @@ def dotenv_values( stream: `StringIO` object with .env content, used if `dotenv_path` is `None`. verbose: Whether to output a warning if the .env file is missing. encoding: Encoding to be used to read the file. + 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. @@ -382,7 +384,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, @@ -390,3 +392,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 fd5e3903..6a294371 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -399,3 +399,9 @@ def test_dotenv_values_file_stream(dotenv_path): 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)