← pod_r5_v1h

fastapi_14372

resolved RESOLVED PASS · 13 tool calls · 89 s · fastapi/fastapi

Task input

♻️ Make the result of `Depends()` and `Security()` hashable, as a workaround for other tools interacting with these internal parts

♻️ Make the result of `Depends()` and `Security()` hashable, as a workaround for other tools interacting with these internal parts

---

This comes from https://github.com/fastapi/fastapi/pull/14320

As a note, FastAPI doesn't really have official support for extending its internals in ways that are not documented in the docs.

So this is not a long-term commitment to keep some specific behavior, but a quick workaround to make things easier for current tools that interact with FastAPI internals in some way.

---

At some point in the future, I'll refactor the internals of FastAPI to make them more explicitly private, and then also figure out the right ways to expose any points of connection with external components that currently need to interact with the internals in some way, and formalize that better.

---

Before that, I need to handle a few issues, bugs, features, refactors, deprecations, etc. And then I'll be able to come back to this idea of figuring out how and what to expose. :nerd_face: 

Tool calls (14)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"class Depends\" . | head -20"
}
{"status": "ok", "stdout": "./fastapi/params.py:766:class Depends:\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "./fastapi/params.py:766:class Depends:\n", "stderr": "", "exit_code": 0}
2run_command
{
 "command": "grep -rn \"class Security\" . | head -20"
}
{"status": "ok", "stdout": "./fastapi/params.py:773:class Security(Depends):\n./fastapi/security/oauth2.py:614:class SecurityScopes:\n./fastapi/security/base.py
full
{"status": "ok", "stdout": "./fastapi/params.py:773:class Security(Depends):\n./fastapi/security/oauth2.py:614:class SecurityScopes:\n./fastapi/security/base.py:4:class SecurityBase:\n./fastapi/dependencies/models.py:19:class SecurityRequirement:\n./fastapi/openapi/models.py:337:class SecuritySchemeType(Enum):\n./fastapi/openapi/models.py:344:class SecurityBase(BaseModelWithConfig):\n", "stderr": "", "exit_code": 0}
3run_command
{
 "command": "sed -n '760,785p' fastapi/params.py"
}
{"status": "ok", "stdout": " json_schema_extra=json_schema_extra,\n **extra,\n )\n\n\n@dataclass\nclass Depends:\n dependency: Optional[Callable[..., Any]] = No
full
{"status": "ok", "stdout": "            json_schema_extra=json_schema_extra,\n            **extra,\n        )\n\n\n@dataclass\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\n\n\n@dataclass\nclass Security(Depends):\n    scopes: Optional[Sequence[str]] = None\n", "stderr": "", "exit_code": 0}
4run_command
{
 "command": "python3 - <<'PY'\nfrom fastapi.params import Depends, Security\nfrom typing import Callable\n\ndef test_hashable():\n    d1 = Depends(Callable)\n    d2 = Depends(Callable)\n    s1 = Security(Callable)\n    s2 = Security(Callable)\n    \n    print(f\"Depends hashable: {hash(d1) == hash(d2)}\")\n    print(f\"Security hashable: {hash(s1) == hash(s2)}\")\n    \n    try:\n        set([d1, d2])\n        print(\"Depends can be in a set\")\n    except TypeError as e:\n        print(f\"Depends cannot be in a set: {e}\")\n\n    try:\n        set([s1, s2])\n        print(\"Security can be in a set\")\n    except TypeError as e:\n        print(f\"Security cannot be in a set: {e}\")\n\ntest_hashable()\nPY\n"
}
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 25, in <module>\n File \"<stdin>
full
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n  File \"<stdin>\", line 25, in <module>\n  File \"<stdin>\", line 10, in test_hashable\nTypeError: unhashable type: 'Depends'\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 25, in <module>\n  File \"<stdin>\", line 10, in test_hashable\nTypeError: unhashable type: 'Depends'\n", "exit_code": 1}}
5run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/params.py\")\ns = p.read_text()\nold = \"\"\"@dataclass\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\"\"\"\nnew = \"\"\"@dataclass(frozen=True)\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\"\"\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\np.write_text(t)\nPY\n"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
6run_command
{
 "command": "python3 - <<'PY'\nfrom fastapi.params import Depends, Security\nfrom typing import Callable\n\ndef test_hashable():\n    d1 = Depends(Callable)\n    d2 = Depends(Callable)\n    s1 = Security(Callable)\n    s2 = Security(Callable)\n    \n    print(f\"Depends hashable: {hash(d1) == hash(d2)}\")\n    print(f\"Security hashable: {hash(s1) == hash(s2)}\")\n    \n    try:\n        set([d1, d2])\n        print(\"Depends can be in a set\")\n    except TypeError as e:\n        print(f\"Depends cannot be in a set: {e}\")\n\n    try:\n        set([s1, s2])\n        print(\"Security can be in a set\")\n    except TypeError as e:\n        print(f\"Security cannot be in a set: {e}\")\n\ntest_hashable()\nPY\n"
}
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 1, in <module>\n File \"/private
full
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"/private/workspace/fastapi/__init__.py\", line 7, in <module>\n    from .applications import FastAPI as FastAPI\n  File \"/private/workspace/fastapi/applications.py\", line 17, in <module>\n    from fastapi import routing\n  File \"/private/workspace/fastapi/routing.py\", line 28, in <module>\n    from fastapi import params, temp_pydantic_v1_params\n  File \"/private/workspace/fastapi/params.py\", line 772, in <module>\n    @dataclass\n     ^^^^^^^^^\n  File \"/Users/jp/.local/share/uv/python/cpython-3.13.15-macos-aarch64-none/lib/python3.13/dataclasses.py\", line 1354, in dataclass\n    return wrap(cls)\n  File \"/Users/jp/.local/share/uv/python/cpython-3.13.15-macos-aarch64-none/lib/python3.13/dataclasses.py\", line 1344, in wrap\n    return _process_class(cls, init, repr, eq, order, unsafe_hash,\n                          frozen, match_args, kw_only, slots,\n                          weakref_slot)\n  File \"/Users/jp/.local/share/uv/python/cpython-3.13.15-macos-aarch64-none/lib/python3.13/dataclasses.py\", line 1038, in _process_class\n    raise TypeError('cannot inherit non-frozen dataclass from a '\n                    'frozen one')\nTypeError: cannot inherit non-frozen dataclass from a frozen one\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"/private/workspace/fastapi/__init__.py\", line 7, in <module>\n    from .applications import FastAPI as FastAPI\n  File \"/private/workspace/fastapi/applications.py\", line 17, in <module>\n    from fastapi import routing\n  File \"/private/workspace/fastapi/routing.py\", line 28, in <module>\n    from fastapi import params, temp_pydantic_v1_params\n  File \"/private/workspace/fastapi/params.py\", line 772, in <module>\n    @dataclass\n     ^^^^^^^^^\n  File \"/Users/jp/.local/share/uv/python
7run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/params.py\")\ns = p.read_text()\nold = \"\"\"@dataclass\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\"\"\"\nnew = \"\"\"@dataclass(frozen=True)\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\"\"\"\n# We need to find where Security is defined and make it frozen too.\n# Let's just replace the whole block.\nPY\n"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
8run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/params.py\")\ns = p.read_text()\nold = \"\"\"@dataclass\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\n\n\n@dataclass\nclass Security(Depends):\n    scopes: Optional[Sequence[str]] = None\"\"\"\nnew = \"\"\"@dataclass(frozen=True)\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\n\n\n@dataclass(frozen=True)\nclass Security(Depends):\n    scopes: Optional[Sequence[str]] = None\"\"\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\np.write_text(t)\nPY\n"
}
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<stdin>\", line 24, in <module>\nAssertionError:
full
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n  File \"<stdin>\", line 24, in <module>\nAssertionError: ('old text found this many times:', 0)\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n  File \"<stdin>\", line 24, in <module>\nAssertionError: ('old text found this many times:', 0)\n", "exit_code": 1}}
9run_command
{
 "command": "sed -n '765,778p' fastapi/params.py"
}
{"status": "ok", "stdout": "@dataclass(frozen=True)\nclass Depends:\n dependency: Optional[Callable[..., Any]] = None\n use_cache: bool = True\n scope: Union[Li
full
{"status": "ok", "stdout": "@dataclass(frozen=True)\nclass Depends:\n    dependency: Optional[Callable[..., Any]] = None\n    use_cache: bool = True\n    scope: Union[Literal[\"function\", \"request\"], None] = None\n\n\n@dataclass\nclass Security(Depends):\n    scopes: Optional[Sequence[str]] = None\n", "stderr": "", "exit_code": 0}
10run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/params.py\")\ns = p.read_text()\nold = \"\"\"@dataclass\nclass Security(Depends):\n    scopes: Optional[Sequence[str]] = None\"\"\"\nnew = \"\"\"@dataclass(frozen=True)\nclass Security(Depends):\n    scopes: Optional[Sequence[str]] = None\"\"\"\nassert s.count(old) == 1, (\"old text found this many times:\", s.count(old))\nt = s.replace(old, new)\np.write_text(t)\nPY\n"
}
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "", "stderr": "", "exit_code": 0}
11run_command
{
 "command": "python3 - <<'PY'\nfrom fastapi.params import Depends, Security\nfrom typing import Callable\n\ndef test_hashable():\n    d1 = Depends(Callable)\n    d2 = Depends(Callable)\n    s1 = Security(Callable)\n    s2 = Security(Callable)\n    \n    print(f\"Depends hashable: {hash(d1) == hash(d2)}\")\n    print(f\"Security hashable: {hash(s1) == hash(s2)}\")\n    \n    try:\n        set([d1, d2])\n        print(\"Depends can be in a set\")\n    except TypeError as e:\n        print(f\"Depends cannot be in a set: {e}\")\n\n    try:\n        set([s1, s2])\n        print(\"Security can be in a set\")\n    except TypeError as e:\n        print(f\"Security cannot be in a set: {e}\")\n\ntest_hashable()\nPY\n"
}
{"status": "ok", "stdout": "Depends hashable: True\nSecurity hashable: True\nDepends can be in a set\nSecurity can be in a set\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "Depends hashable: True\nSecurity hashable: True\nDepends can be in a set\nSecurity can be in a set\n", "stderr": "", "exit_code": 0}
12run_command
{
 "command": "git diff"
}
{"status": "ok", "stdout": "diff --git a/fastapi/params.py b/fastapi/params.py\nindex 6a58d580..6d07df35 100644\n--- a/fastapi/params.py\n+++ b/fastapi/params.p
full
{"status": "ok", "stdout": "diff --git a/fastapi/params.py b/fastapi/params.py\nindex 6a58d580..6d07df35 100644\n--- a/fastapi/params.py\n+++ b/fastapi/params.py\n@@ -762,13 +762,13 @@ class File(Form):  # type: ignore[misc]\n         )\n \n \n-@dataclass\n+@dataclass(frozen=True)\n class Depends:\n     dependency: Optional[Callable[..., Any]] = None\n     use_cache: bool = True\n     scope: Union[Literal[\"function\", \"request\"], None] = None\n \n \n-@dataclass\n+@dataclass(frozen=True)\n class Security(Depends):\n     scopes: Optional[Sequence[str]] = None\n", "stderr": "", "exit_code": 0}
13run_command
{
 "command": "submit_patch"
}
{"status": "error", "error_type": "CommandError", "error_message": "/bin/bash: submit_patch: command not found\n", "details": {"stdout": "", "stderr": "/bin/bas
full
{"status": "error", "error_type": "CommandError", "error_message": "/bin/bash: submit_patch: command not found\n", "details": {"stdout": "", "stderr": "/bin/bash: submit_patch: command not found\n", "exit_code": 127}}
14submit_patch
{}
{"status": "ok", "patch_size": 516, "files_changed": 1}
full
{"status": "ok", "patch_size": 516, "files_changed": 1}

Patch

diff --git a/fastapi/params.py b/fastapi/params.py
index 6a58d580..6d07df35 100644
--- a/fastapi/params.py
+++ b/fastapi/params.py
@@ -762,13 +762,13 @@ class File(Form):  # type: ignore[misc]
         )
 
 
-@dataclass
+@dataclass(frozen=True)
 class Depends:
     dependency: Optional[Callable[..., Any]] = None
     use_cache: bool = True
     scope: Union[Literal["function", "request"], None] = None
 
 
-@dataclass
+@dataclass(frozen=True)
 class Security(Depends):
     scopes: Optional[Sequence[str]] = None

Test output

show
.                                                                        [100%]
1 passed in 0.38s