Skip to content

Commit

Permalink
feat: add with_env param in dotenv_values
Browse files Browse the repository at this point in the history
  • Loading branch information
ramwin committed Apr 5, 2022
1 parent b4e0c78 commit cfba8fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -362,18 +363,26 @@ 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.
"""
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,
interpolate=interpolate,
override=True,
encoding=encoding,
).dict()
if with_env:
return dict(
**os.environ,
**result,
)
else:
return result
6 changes: 6 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit cfba8fc

Please sign in to comment.