Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cfba8fc

Browse filesBrowse files
committed
feat: add with_env param in dotenv_values
1 parent b4e0c78 commit cfba8fc
Copy full SHA for cfba8fc

File tree

Expand file treeCollapse file tree

2 files changed

+16
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+16
-1
lines changed

‎src/dotenv/main.py

Copy file name to clipboardExpand all lines: src/dotenv/main.py
+10-1Lines 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

Copy file name to clipboardExpand all lines: tests/test_main.py
+6Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.