Skip to content

Commit

Permalink
Warn instead of exception for missing env file (#2485)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause authored Feb 6, 2024
1 parent 0e4da0a commit 7ec763c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions starlette/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import typing
import warnings
from pathlib import Path


Expand Down Expand Up @@ -62,8 +63,9 @@ def __init__(
self.file_values: typing.Dict[str, str] = {}
if env_file is not None:
if not os.path.isfile(env_file):
raise FileNotFoundError(f"Config file '{env_file}' not found.")
self.file_values = self._read_file(env_file)
warnings.warn(f"Config file '{env_file}' not found.")
else:
self.file_values = self._read_file(env_file)

@typing.overload
def __call__(self, key: str, *, default: None) -> str | None:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def cast_to_int(v: typing.Any) -> int:
def test_missing_env_file_raises(tmpdir: Path) -> None:
path = os.path.join(tmpdir, ".env")

with pytest.raises(FileNotFoundError, match=f"Config file '{path}' not found."):
with pytest.warns(UserWarning, match=f"Config file '{path}' not found."):
Config(path)


Expand Down

0 comments on commit 7ec763c

Please sign in to comment.