diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 84dfa4d03..1943d0a07 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -228,9 +228,25 @@ def get_flat_params(dependant: Dependant) -> List[ModelField]: return path_params + query_params + header_params + cookie_params +def get_globalns(call: Callable[..., Any]) -> Dict[str, Any]: + source_func: Any + if hasattr(call, "__globals__"): + # If the callable has __globals__ set, use that + source_func = call + elif isinstance(call, type): + # If the callable is a class, its dependencies are declared on __init__ + source_func = call.__init__ + else: + # For callable instances of classes, the dependencies are declared on __call__ + source_func = call.__call__ + + return cast(Dict[str, Any], getattr(source_func, "__globals__", {})) + + def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: signature = inspect.signature(call) - globalns = getattr(call, "__globals__", {}) + globalns = get_globalns(call) + typed_params = [ inspect.Parameter( name=param.name,