Skip to content

Commit 834fb90

Browse files
committed
feat: add with_env param in dotenv_values
1 parent ba9408c commit 834fb90

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/dotenv/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ def dotenv_values(
338338
verbose: bool = False,
339339
interpolate: bool = True,
340340
encoding: Optional[str] = "utf-8",
341+
with_env: bool = False,
341342
) -> Dict[str, Optional[str]]:
342343
"""
343344
Parse a .env file and return its content as a dict.
@@ -348,17 +349,25 @@ def dotenv_values(
348349
`False`.
349350
in `.env` file. Defaults to `False`.
350351
- *encoding*: encoding to be used to read the file.
352+
- *with_env*: include the os.environ() to response.
351353
352354
If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
353355
"""
354356
if dotenv_path is None and stream is None:
355357
dotenv_path = find_dotenv()
356358

357-
return DotEnv(
359+
result = DotEnv(
358360
dotenv_path=dotenv_path,
359361
stream=stream,
360362
verbose=verbose,
361363
interpolate=interpolate,
362364
override=True,
363365
encoding=encoding,
364366
).dict()
367+
if with_env:
368+
return dict(
369+
**os.environ,
370+
**result,
371+
)
372+
else:
373+
return result

tests/test_main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,9 @@ def test_dotenv_values_file_stream(dotenv_file):
384384
result = dotenv.dotenv_values(stream=f)
385385

386386
assert result == {"a": "b"}
387+
388+
389+
def test_dotenv_values_with_os_environment():
390+
if os.environ.get("USER"):
391+
assert "USER" not in dotenv.dotenv_values(with_env=False)
392+
assert "USER" in dotenv.dotenv_values(with_env=True)

0 commit comments

Comments
 (0)