← pod_r1_v1e

fastapi_14372

resolved RESOLVED PASS · 11 tool calls · 92 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 (12)

#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,780p' 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 -c \"from dataclasses import dataclass; @dataclass class A: x: int; print(hash(A(1)))\""
}
{"status": "error", "error_type": "CommandError", "error_message": " File \"<string>\", line 1\n from dataclasses import dataclass; @dataclass class A: x: int;
full
{"status": "error", "error_type": "CommandError", "error_message": "  File \"<string>\", line 1\n    from dataclasses import dataclass; @dataclass class A: x: int; print(hash(A(1)))\n                                       ^\nSyntaxError: invalid syntax\n", "details": {"stdout": "", "stderr": "  File \"<string>\", line 1\n    from dataclasses import dataclass; @dataclass class A: x: int; print(hash(A(1)))\n                                       ^\nSyntaxError: invalid syntax\n", "exit_code": 1}}
5run_command
{
 "command": "python3 -c \"from dataclasses import dataclass; \n@dataclass \nclass A: \n    x: int\nprint(hash(A(1)))\""
}
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<string>\", line 5, in <module>\n print(hash(A(1
full
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n  File \"<string>\", line 5, in <module>\n    print(hash(A(1)))\n          ~~~~^^^^^^\nTypeError: unhashable type: 'A'\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n  File \"<string>\", line 5, in <module>\n    print(hash(A(1)))\n          ~~~~^^^^^^\nTypeError: unhashable type: 'A'\n", "exit_code": 1}}
6run_command
{
 "command": "python3 -c \"from dataclasses import dataclass; \n@dataclass(frozen=True) \nclass A: \n    x: int\nprint(hash(A(1)))\""
}
{"status": "ok", "stdout": "-6644214454873602895\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "-6644214454873602895\n", "stderr": "", "exit_code": 0}
7run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/params.py\")\ns = p.read_text()\nold = \"\"\"@dataclass\nclass Depends:\"\"\"\nnew = \"\"\"@dataclass(frozen=True)\nclass Depends:\"\"\"\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}
8run_command
{
 "command": "cd /workspace && python3 - <<'PY'\nfrom pathlib import Path\np = Path(\"fastapi/params.py\")\ns = p.read_text()\nold = \"\"\"@dataclass\nclass Security(Depends):\"\"\"\nnew = \"\"\"@dataclass(frozen=True)\nclass Security(Depends):\"\"\"\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}
9run_command
{
 "command": "python3 -c \"from fastapi.params import Depends, Security; d1 = Depends(int); d2 = Depends(int); s1 = Security(int); s2 = Security(int); print(hash(d1)); print(hash(d2)); print(hash(s1)); print(hash(s2)); assert hash(d1) == hash(d2); assert hash(s1) == hash(s2)\""
}
{"status": "ok", "stdout": "3199140067087704711\n3199140067087704711\n5508599109164697169\n5508599109164697169\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "3199140067087704711\n3199140067087704711\n5508599109164697169\n5508599109164697169\n", "stderr": "", "exit_code": 0}
10run_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}
11run_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}}
12submit_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.33s