Skip to content

Commit cfba8fc

Browse files
committed
feat: add with_env param in dotenv_values
1 parent b4e0c78 commit cfba8fc

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
@@ -347,6 +347,7 @@ def dotenv_values(
347347
verbose: bool = False,
348348
interpolate: bool = True,
349349
encoding: Optional[str] = "utf-8",
350+
with_env: bool = False,
350351
) -> Dict[str, Optional[str]]:
351352
"""
352353
Parse a .env file and return its content as a dict.
@@ -362,18 +363,26 @@ def dotenv_values(
362363
- `verbose`: whether to output a warning if the .env file is missing. Defaults to
363364
`False`.
364365
- `encoding`: encoding to be used to read the file. Defaults to `"utf-8"`.
366+
- `with_env`: include the os.environ() to response.
365367
366368
If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
367369
.env file.
368370
"""
369371
if dotenv_path is None and stream is None:
370372
dotenv_path = find_dotenv()
371373

372-
return DotEnv(
374+
result = DotEnv(
373375
dotenv_path=dotenv_path,
374376
stream=stream,
375377
verbose=verbose,
376378
interpolate=interpolate,
377379
override=True,
378380
encoding=encoding,
379381
).dict()
382+
if with_env:
383+
return dict(
384+
**os.environ,
385+
**result,
386+
)
387+
else:
388+
return result

tests/test_main.py

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

417417
assert result == {"a": "b"}
418+
419+
420+
def test_dotenv_values_with_os_environment():
421+
if os.environ.get("USER"):
422+
assert "USER" not in dotenv.dotenv_values(with_env=False)
423+
assert "USER" in dotenv.dotenv_values(with_env=True)

0 commit comments

Comments
 (0)