]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add overload for cast as Callable in Config.__call__ (#1378)
authorPetter Friberg <petter@5monkeys.se>
Tue, 8 Feb 2022 10:51:52 +0000 (11:51 +0100)
committerGitHub <noreply@github.com>
Tue, 8 Feb 2022 10:51:52 +0000 (10:51 +0000)
Co-authored-by: Tom Christie <tom@tomchristie.com>
starlette/config.py
tests/test_config.py

index 7444ae06d2fe45735766039e4eeefd443cdaf10c..bd809afb4ce5981c90e893dcc4d032ad02a38a37 100644 (file)
@@ -72,6 +72,15 @@ class Config:
     ) -> str:  # pragma: no cover
         ...
 
+    @typing.overload
+    def __call__(
+        self,
+        key: str,
+        cast: typing.Callable[[typing.Any], T] = ...,
+        default: typing.Any = ...,
+    ) -> T:  # pragma: no cover
+        ...
+
     @typing.overload
     def __call__(
         self, key: str, cast: typing.Type[str] = ..., default: T = ...
index ae91f9695928e66a92f2ce8b53edda7cbd05aca8..cfe908bc0dfb4829e4059202c4714679aecf7fc3 100644 (file)
@@ -20,12 +20,17 @@ def test_config(tmpdir, monkeypatch):
 
     config = Config(path, environ={"DEBUG": "true"})
 
+    def cast_to_int(v) -> int:
+        return int(v)
+
     DEBUG = config("DEBUG", cast=bool)
     DATABASE_URL = config("DATABASE_URL", cast=URL)
     REQUEST_TIMEOUT = config("REQUEST_TIMEOUT", cast=int, default=10)
     REQUEST_HOSTNAME = config("REQUEST_HOSTNAME")
     SECRET_KEY = config("SECRET_KEY", cast=Secret)
     assert config("BOOL_AS_INT", cast=bool) is False
+    assert config("BOOL_AS_INT", cast=cast_to_int) == 0
+    assert config("DEFAULTED_BOOL", cast=cast_to_int, default=True) == 1
 
     assert DEBUG is True
     assert DATABASE_URL.path == "/dbname"