]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Warn instead of exception for missing env file (#2485)
authorMathias Hauser <mathause@users.noreply.github.com>
Tue, 6 Feb 2024 21:58:35 +0000 (22:58 +0100)
committerGitHub <noreply@github.com>
Tue, 6 Feb 2024 21:58:35 +0000 (14:58 -0700)
starlette/config.py
tests/test_config.py

index 75a097724245df2a3d02d949ac4db07f421a5606..d222a0a6278c1f5598d014f4225061409fdbab88 100644 (file)
@@ -2,6 +2,7 @@ from __future__ import annotations
 
 import os
 import typing
+import warnings
 from pathlib import Path
 
 
@@ -62,8 +63,9 @@ class Config:
         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:
index 4974ffdb01569e4f9a452fddca2f99a9ba03bafe..f375910077e4e7eeda8c5cf86a5bc5062fe82c9d 100644 (file)
@@ -108,7 +108,7 @@ def test_config(tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -> None:
 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)