Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add with_env param in dotenv_values #385

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/dotenv/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -375,18 +376,26 @@ 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.
"""
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 @@ -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)
Loading