← pod_step15_v1

fastapi_14372

resolved RESOLVED PASS · 17 tool calls · 60 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 (18)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"class Depends\" ."
}
{"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\" ."
}
{"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}
3-4 ×2read_file
{
 "filepath": "fastapi/params.py",
 "start_line": "760",
 "end_line": "790"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
5read_file
{
 "filepath": "fastapi/params.py",
 "start_line": "760"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
6run_command
{
 "command": "grep -n \"class Depends\" fastapi/params.py"
}
{"status": "ok", "stdout": "766:class Depends:\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "766:class Depends:\n", "stderr": "", "exit_code": 0}
7read_file
{
 "end_line": "785",
 "filepath": "fastapi/params.py",
 "start_line": "760"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
8run_command
{
 "command": "head -n 800 fastapi/params.py | tail -n 50"
}
{"status": "ok", "stdout": " deprecated: Union[deprecated, str, bool, None] = None,\n include_in_schema: bool = True,\n json_schema_extra: Union[Dict[str, Any],
full
{"status": "ok", "stdout": "        deprecated: Union[deprecated, str, bool, None] = None,\n        include_in_schema: bool = True,\n        json_schema_extra: Union[Dict[str, Any], None] = None,\n        **extra: Any,\n    ):\n        super().__init__(\n            default=default,\n            default_factory=default_factory,\n            annotation=annotation,\n            media_type=media_type,\n            alias=alias,\n            alias_priority=alias_priority,\n            validation_alias=validation_alias,\n            serialization_alias=serialization_alias,\n            title=title,\n            description=description,\n            gt=gt,\n            ge=ge,\n            lt=lt,\n            le=le,\n            min_length=min_length,\n            max_length=max_length,\n            pattern=pattern,\n            regex=regex,\n            discriminator=discriminator,\n            strict=strict,\n            multiple_of=multiple_of,\n            allow_inf_nan=allow_inf_nan,\n            max_digits=max_digits,\n            decimal_places=decimal_places,\n            deprecated=deprecated,\n            example=example,\n            examples=examples,\n            openapi_examples=openapi_examples,\n            include_in_schema=include_in_schema,\n            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}
9run_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}}
10run_command
{
 "command": "python3 -c \"from dataclasses import dataclass; @dataclass class A: x: int; a = A(1); print(hash(a))\""
}
{"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; a = A(1); print(hash(a))\n                                       ^\nSyntaxError: invalid syntax\n", "details": {"stdout": "", "stderr": "  File \"<string>\", line 1\n    from dataclasses import dataclass; @dataclass class A: x: int; a = A(1); print(hash(a))\n                                       ^\nSyntaxError: invalid syntax\n", "exit_code": 1}}
11run_command
{
 "command": "python3 -c \"\nfrom dataclasses import dataclass\n@dataclass\nclass A:\n    x: int\na = A(1)\nprint(hash(a))\n\""
}
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n File \"<string>\", line 7, in <module>\n print(hash(a))
full
{"status": "error", "error_type": "CommandError", "error_message": "Traceback (most recent call last):\n  File \"<string>\", line 7, in <module>\n    print(hash(a))\n          ~~~~^^^\nTypeError: unhashable type: 'A'\n", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n  File \"<string>\", line 7, in <module>\n    print(hash(a))\n          ~~~~^^^\nTypeError: unhashable type: 'A'\n", "exit_code": 1}}
12run_command
{
 "command": "python3 -c \"\nfrom dataclasses import dataclass\n@dataclass(frozen=True)\nclass A:\n    x: int\na = A(1)\nprint(hash(a))\n\""
}
{"status": "ok", "stdout": "-6644214454873602895\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "-6644214454873602895\n", "stderr": "", "exit_code": 0}
13run_command
{
 "command": "grep -n \"class Depends\" fastapi/params.py"
}
{"status": "ok", "stdout": "766:class Depends:\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "766:class Depends:\n", "stderr": "", "exit_code": 0}
14read_file
{
 "end_line": "780",
 "filepath": "fastapi/params.py",
 "start_line": "760"
}
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
full
{"status": "error", "error_type": "FileReadError", "error_message": "'>' not supported between instances of 'int' and 'str'"}
15run_command
{
 "command": "grep -C 10 \"class Depends\" fastapi/params.py"
}
{"status": "ok", "stdout": " example=example,\n examples=examples,\n openapi_examples=openapi_examples,\n include_in_schema=include_in_schema,\n json_schema_ext
full
{"status": "ok", "stdout": "            example=example,\n            examples=examples,\n            openapi_examples=openapi_examples,\n            include_in_schema=include_in_schema,\n            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}
16edit_file
{
 "filepath": "fastapi/params.py",
 "new_string": "@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\n",
 "old_string": "@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"
}
{"status": "ok", "filepath": "fastapi/params.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/params.py\n+++ b/fastapi/params.py\n@@ -762,13 +
full
{"status": "ok", "filepath": "fastapi/params.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/params.py\n+++ b/fastapi/params.py\n@@ -762,13 +762,13 @@\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", "is_truncated": false}
17run_command
{
 "command": "python3 -c \"from fastapi.params import Depends, Security; d = Depends(); s = Security(); print(hash(d)); print(hash(s))\""
}
{"status": "ok", "stdout": "2526156517640742742\n-5969495213416321178\n", "stderr": "", "exit_code": 0}
full
{"status": "ok", "stdout": "2526156517640742742\n-5969495213416321178\n", "stderr": "", "exit_code": 0}
18submit_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.29s