← eval3_submission_v2t

fastapi_14301

resolved RESOLVED PASS · 7 tool calls · 68 s · fastapi/fastapi

Task input

🐛 Fix `Depends(func, scope='function')` for top level (parameterless) dependencies

**Summary**

This PR enables proper support for `scope="function"` on `APIRoute` dependencies by passing the dependency’s scope through to `get_dependant`. Previously, parameter-less dependencies created via `get_parameterless_sub_dependant` ignored `depends.scope`, which led to unexpected behavior for function-scoped dependencies.

**Motivation**

Users expect `scope="function"` to work as defined, even when registered at the route level. Because `depends.scope` wasn’t forwarded, function scope behaved like the default(request). This change aligns behavior with user expectations and documentation around dependency scopes.
(Related: #14296)

Tool calls (8)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"get_parameterless_sub_dependant\" ."
}
{"status": "ok", "stdout": "./fastapi/dependencies/utils.py:124:def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependant:\n./fast
full
{"status": "ok", "stdout": "./fastapi/dependencies/utils.py:124:def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependant:\n./fastapi/routing.py:44:    get_parameterless_sub_dependant,\n./fastapi/routing.py:489:                get_parameterless_sub_dependant(depends=depends, path=self.path_format),\n./fastapi/routing.py:642:                get_parameterless_sub_dependant(depends=depends, path=self.path_format),\n", "exit_code": 0}
2read_file
{
 "filepath": "fastapi/dependencies/utils.py",
 "start_line": 110,
 "end_line": 140
}
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": " # parse_options_header is only available in the right multipart\n from multipart.mult
full
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "                # parse_options_header is only available in the right multipart\n                from multipart.multipart import (  # type: ignore[import-untyped]\n                    parse_options_header,\n                )\n\n                assert parse_options_header\n            except ImportError:\n                logger.error(multipart_incorrect_install_error)\n                raise RuntimeError(multipart_incorrect_install_error) from None\n        except ImportError:\n            logger.error(multipart_not_installed_error)\n            raise RuntimeError(multipart_not_installed_error) from None\n\n\ndef get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependant:\n    assert callable(depends.dependency), (\n        \"A parameter-less dependency must have a callable dependency\"\n    )\n    use_security_scopes: List[str] = []\n    if isinstance(depends, params.Security) and depends.scopes:\n        use_security_scopes.extend(depends.scopes)\n    return get_dependant(\n        path=path, call=depends.dependency, security_scopes=use_security_scopes\n    )\n\n\ndef get_flat_dependant(\n    dependant: Dependant,\n    *,\n    skip_repeats: bool = False,\n    visited: Optional[List[DependencyCacheKey]] = None,\n", "start_line": 110, "end_line": 140, "total_lines": 1020, "is_truncated": false}
3read_file
{
 "end_line": 200,
 "filepath": "fastapi/dependencies/utils.py",
 "start_line": 150
}
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": " cookie_params=dependant.cookie_params.copy(),\n body_params=dependant.body_params.cop
full
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "        cookie_params=dependant.cookie_params.copy(),\n        body_params=dependant.body_params.copy(),\n        security_requirements=dependant.security_requirements.copy(),\n        use_cache=dependant.use_cache,\n        path=dependant.path,\n    )\n    for sub_dependant in dependant.dependencies:\n        if skip_repeats and sub_dependant.cache_key in visited:\n            continue\n        flat_sub = get_flat_dependant(\n            sub_dependant, skip_repeats=skip_repeats, visited=visited\n        )\n        flat_dependant.path_params.extend(flat_sub.path_params)\n        flat_dependant.query_params.extend(flat_sub.query_params)\n        flat_dependant.header_params.extend(flat_sub.header_params)\n        flat_dependant.cookie_params.extend(flat_sub.cookie_params)\n        flat_dependant.body_params.extend(flat_sub.body_params)\n        flat_dependant.security_requirements.extend(flat_sub.security_requirements)\n    return flat_dependant\n\n\ndef _get_flat_fields_from_params(fields: List[ModelField]) -> List[ModelField]:\n    if not fields:\n        return fields\n    first_field = fields[0]\n    if len(fields) == 1 and _is_model_class(first_field.type_):\n        fields_to_extract = get_cached_model_fields(first_field.type_)\n        return fields_to_extract\n    return fields\n\n\ndef get_flat_params(dependant: Dependant) -> List[ModelField]:\n    flat_dependant = get_flat_dependant(dependant, skip_repeats=True)\n    path_params = _get_flat_fields_from_params(flat_dependant.path_params)\n    query_params = _get_flat_fields_from_params(flat_dependant.query_params)\n    header_params = _get_flat_fields_from_params(flat_dependant.header_params)\n    cookie_params = _get_flat_fields_from_params(flat_dependant.cookie_params)\n    return path_params + query_params + header_params + cookie_params\n\n\ndef get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:\n    signature = in
4run_command
{
 "command": "grep -n \"def get_dependant\" fastapi/dependencies/utils.py"
}
{"status": "ok", "stdout": "226:def get_dependant(\n", "exit_code": 0}
full
{"status": "ok", "stdout": "226:def get_dependant(\n", "exit_code": 0}
5read_file
{
 "end_line": 250,
 "filepath": "fastapi/dependencies/utils.py",
 "start_line": 226
}
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "def get_dependant(\n *,\n path: str,\n call: Callable[..., Any],\n name: Optional[str]
full
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "def get_dependant(\n    *,\n    path: str,\n    call: Callable[..., Any],\n    name: Optional[str] = None,\n    security_scopes: Optional[List[str]] = None,\n    use_cache: bool = True,\n    scope: Union[Literal[\"function\", \"request\"], None] = None,\n) -> Dependant:\n    dependant = Dependant(\n        call=call,\n        name=name,\n        path=path,\n        security_scopes=security_scopes,\n        use_cache=use_cache,\n        scope=scope,\n    )\n    path_param_names = get_path_param_names(path)\n    endpoint_signature = get_typed_signature(call)\n    signature_params = endpoint_signature.parameters\n    if isinstance(call, SecurityBase):\n        use_scopes: List[str] = []\n        if isinstance(call, (OAuth2, OpenIdConnect)):\n            use_scopes = security_scopes or use_scopes\n        security_requirement = SecurityRequirement(\n", "start_line": 226, "end_line": 250, "total_lines": 1020, "is_truncated": false}
6read_file
{
 "end_line": 132,
 "filepath": "fastapi/dependencies/utils.py",
 "start_line": 124
}
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependan
full
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependant:\n    assert callable(depends.dependency), (\n        \"A parameter-less dependency must have a callable dependency\"\n    )\n    use_security_scopes: List[str] = []\n    if isinstance(depends, params.Security) and depends.scopes:\n        use_security_scopes.extend(depends.scopes)\n    return get_dependant(\n        path=path, call=depends.dependency, security_scopes=use_security_scopes\n", "start_line": 124, "end_line": 132, "total_lines": 1020, "is_truncated": false}
7edit_file
{
 "filepath": "fastapi/dependencies/utils.py",
 "new_string": "    return get_dependant(\n        path=path, call=depends.dependency, security_scopes=use_security_scopes, scope=depends.scope\n    )",
 "old_string": "    return get_dependant(\n        path=path, call=depends.dependency, security_scopes=use_security_scopes\n    )"
}
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/dependencies/utils.py\n+++ b/fastapi
full
{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/dependencies/utils.py\n+++ b/fastapi/dependencies/utils.py\n@@ -129,7 +129,7 @@\n     if isinstance(depends, params.Security) and depends.scopes:\n         use_security_scopes.extend(depends.scopes)\n     return get_dependant(\n-        path=path, call=depends.dependency, security_scopes=use_security_scopes\n+        path=path, call=depends.dependency, security_scopes=use_security_scopes, scope=depends.scope\n     )\n \n \n", "is_truncated": false}
8submit_patch
[]
{"status": "ok", "patch_size": 618, "files_changed": 1}
full
{"status": "ok", "patch_size": 618, "files_changed": 1}

Patch

diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py
index c5c6b69b..f71e84ee 100644
--- a/fastapi/dependencies/utils.py
+++ b/fastapi/dependencies/utils.py
@@ -129,7 +129,7 @@ def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> De
     if isinstance(depends, params.Security) and depends.scopes:
         use_security_scopes.extend(depends.scopes)
     return get_dependant(
-        path=path, call=depends.dependency, security_scopes=use_security_scopes
+        path=path, call=depends.dependency, security_scopes=use_security_scopes, scope=depends.scope
     )
 
 

Test output

show
...........                                                              [100%]
=============================== warnings summary ===============================
../../../../../../kaggle/tmp/envs/overlays/starlette-0.49.3-py3-none-any/starlette/testclient.py:51
  /kaggle/tmp/envs/overlays/starlette-0.49.3-py3-none-any/starlette/testclient.py:51: DeprecationWarning: The anyio.abc.BlockingPortal alias is deprecated, use anyio.from_thread.BlockingPortal instead.
    _PortalFactoryType = Callable[[], AbstractContextManager[anyio.abc.BlockingPortal]]

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
11 passed, 1 warning in 0.75s