failed WRONG_FIX wrong_fix · 25 tool calls · 130 s · fastapi/fastapi
🐛 Fix evaluating stringified annotations in Python 3.10 I've bumped into the exact same issue as https://github.com/tiangolo/typer/discussions/598 so I solved it like https://github.com/tiangolo/typer/pull/721 :smile: I'll try to add tests soon but if you can do it faster, please feel free !!
| # | Tool | Arguments | Result |
|---|---|---|---|
| 1 | run_command | {
"command": "grep -rn \"get_type_hints\" fastapi | head -20"
} | {"status": "ok", "stdout": "", "exit_code": 0}full{"status": "ok", "stdout": "", "exit_code": 0} |
| 2 | run_command | {
"command": "grep -rn \"get_type_hints\" . | head -20"
} | {"status": "ok", "stdout": "", "exit_code": 0}full{"status": "ok", "stdout": "", "exit_code": 0} |
| 3 | run_command | {
"command": "grep -rn \"eval\" fastapi | head -20"
} | {"status": "ok", "stdout": "fastapi/dependencies/utils.py:33: evaluate_forwardref,\nfastapi/dependencies/utils.py:213: annotation = evaluate_forwardref(annotatifull{"status": "ok", "stdout": "fastapi/dependencies/utils.py:33: evaluate_forwardref,\nfastapi/dependencies/utils.py:213: annotation = evaluate_forwardref(annotation, globalns, globalns)\nfastapi/_compat/__init__.py:17:from .main import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/v1.py:69: from pydantic.typing import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/v1.py:122: from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/main.py:29: from .v2 import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/main.py:45: from .v1 import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/v2.py:28:from pydantic._internal._typing_extra import eval_type_lenient\nfastapi/_compat/v2.py:50:evaluate_forwardref = eval_type_lenient\nfastapi/openapi/models.py:162: unevaluatedItems: Optional[\"SchemaOrBool\"] = None\nfastapi/openapi/models.py:163: unevaluatedProperties: Optional[\"SchemaOrBool\"] = None\n", "exit_code": 0} |
| 4 | read_file | {
"filepath": "fastapi/_compat/v2.py"
} | {"status": "ok", "filepath": "fastapi/_compat/v2.py", "content": "import re\nimport warnings\nfrom copy import copy, deepcopy\nfrom dataclasses import dataclassfull{"status": "ok", "filepath": "fastapi/_compat/v2.py", "content": "import re\nimport warnings\nfrom copy import copy, deepcopy\nfrom dataclasses import dataclass\nfrom enum import Enum\nfrom typing import (\n Any,\n Dict,\n List,\n Sequence,\n Set,\n Tuple,\n Type,\n Union,\n cast,\n)\n\nfrom fastapi._compat import may_v1, shared\nfrom fastapi.openapi.constants import REF_TEMPLATE\nfrom fastapi.types import IncEx, ModelNameMap, UnionType\nfrom pydantic import BaseModel, TypeAdapter, create_model\nfrom pydantic import PydanticSchemaGenerationError as PydanticSchemaGenerationError\nfrom pydantic import PydanticUndefinedAnnotation as PydanticUndefinedAnnotation\nfrom pydantic import ValidationError as ValidationError\nfrom pydantic._internal._schema_generation_shared import ( # type: ignore[attr-defined]\n GetJsonSchemaHandler as GetJsonSchemaHandler,\n)\nfrom pydantic._internal._typing_extra import eval_type_lenient\nfrom pydantic._internal._utils import lenient_issubclass as lenient_issubclass\nfrom pydantic.fields import FieldInfo as FieldInfo\nfrom pydantic.json_schema import GenerateJsonSchema as GenerateJsonSchema\nfrom pydantic.json_schema import JsonSchemaValue as JsonSchemaValue\nfrom pydantic_core import CoreSchema as CoreSchema\nfrom pydantic_core import PydanticUndefined, PydanticUndefinedType\nfrom pydantic_core import Url as Url\nfrom typing_extensions import Annotated, Literal, get_args, get_origin\n\ntry:\n from pydantic_core.core_schema import (\n with_info_plain_validator_function as with_info_plain_validator_function,\n )\nexcept ImportError: # pragma: no cover\n from pydantic_core.core_schema import (\n general_plain_validator_function as with_info_plain_validator_function, # noqa: F401\n )\n\nRequiredParam = PydanticUndefined\nUndefined = PydanticUndefined\nUndefinedType = PydanticUndefinedType\nevaluate_forwardref = eval_type_lenient\nValidator = Any\n\n\nclass BaseConfig:\n pass\n\n\ncl |
| 5 | run_command | {
"command": "grep -rn \"evaluate_forwardref\" fastapi/dependencies/utils.py"
} | {"status": "ok", "stdout": "33: evaluate_forwardref,\n213: annotation = evaluate_forwardref(annotation, globalns, globalns)\n", "exit_code": 0}full{"status": "ok", "stdout": "33: evaluate_forwardref,\n213: annotation = evaluate_forwardref(annotation, globalns, globalns)\n", "exit_code": 0} |
| 6 | read_file | {
"filepath": "fastapi/dependencies/utils.py",
"start_line": 200,
"end_line": 230
} | {"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": " kind=param.kind,\n default=param.default,\n annotation=get_typed_annotation(param.annfull{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": " kind=param.kind,\n default=param.default,\n annotation=get_typed_annotation(param.annotation, globalns),\n )\n for param in signature.parameters.values()\n ]\n typed_signature = inspect.Signature(typed_params)\n return typed_signature\n\n\ndef get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:\n if isinstance(annotation, str):\n annotation = ForwardRef(annotation)\n annotation = evaluate_forwardref(annotation, globalns, globalns)\n if annotation is type(None):\n return None\n return annotation\n\n\ndef get_typed_return_annotation(call: Callable[..., Any]) -> Any:\n signature = inspect.signature(call)\n unwrapped = inspect.unwrap(call)\n annotation = signature.return_annotation\n\n if annotation is inspect.Signature.empty:\n return None\n\n globalns = getattr(unwrapped, \"__globals__\", {})\n return get_typed_annotation(annotation, globalns)\n\n\n", "start_line": 200, "end_line": 230, "total_lines": 1037, "is_truncated": false} |
| 7 | run_command | {
"command": "python3 -c \"from typing import ForwardRef; from pydantic._internal._typing_extra import eval_type_lenient; fr = ForwardRef('int'); print(eval_type_lenient(fr, {}, {}))\""
} | {"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0}full{"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0} |
| 8 | run_command | {
"command": "python3 -c \"from typing import ForwardRef; from pydantic._internal._typing_extra import eval_type_lenient; fr = ForwardRef('int'); print(eval_type_lenient('int', {}, {}))\""
} | {"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0}full{"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0} |
| 9 | run_command | {
"command": "python3 -c \"import typing; print(typing.get_type_hints('def foo(a: \\\"int\\\"): pass'))\""
} | {"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": "Traceback (most recenfull{"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": "Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\n import typing; print(typing.get_type_hints('def foo(a: \"int\"): pass'))\n ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/root/.local/share/uv/python/cpython-3.13.14-linux-x86_64-gnu/lib/python3.13/typing.py\", line 2508, in get_type_hints\n raise TypeError('{!r} is not a module, class, method, '\n 'or function.'.format(obj))\nTypeError: 'def foo(a: \"int\"): pass' is not a module, class, method, or function.\n", "exit_code": 1}} |
| 10 | run_command | {
"command": "python3 -c \"import typing; def foo(a: 'int'): pass; print(typing.get_type_hints(foo))\""
} | {"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": " File \"<string>\", lfull{"status": "error", "error_type": "CommandError", "error_message": "Command failed with exit code 1", "details": {"stdout": "", "stderr": " File \"<string>\", line 1\n import typing; def foo(a: 'int'): pass; print(typing.get_type_hints(foo))\n ^^^\nSyntaxError: invalid syntax\n", "exit_code": 1}} |
| 11 | run_command | {
"command": "python3 -c \"import typing; \ndef foo(a: 'int'): pass\nprint(typing.get_type_hints(foo))\""
} | {"status": "ok", "stdout": "{'a': <class 'int'>}\n", "exit_code": 0}full{"status": "ok", "stdout": "{'a': <class 'int'>}\n", "exit_code": 0} |
| 12 | run_command | {
"command": "python3 -c \"import typing; \ndef foo(a: 'int'): pass\nprint(typing.get_type_hints(foo, include_extras=False))\""
} | {"status": "ok", "stdout": "{'a': <class 'int'>}\n", "exit_code": 0}full{"status": "ok", "stdout": "{'a': <class 'int'>}\n", "exit_code": 0} |
| 13 | run_command | {
"command": "grep -rn \"get_type_hints\" fastapi | head -20"
} | {"status": "ok", "stdout": "", "exit_code": 0}full{"status": "ok", "stdout": "", "exit_code": 0} |
| 14 | run_command | {
"command": "grep -rn \"get_type_hints\" . | head -20"
} | {"status": "ok", "stdout": "", "exit_code": 0}full{"status": "ok", "stdout": "", "exit_code": 0} |
| 15 | run_command | {
"command": "grep -rn \"get_type_hints\" tests | head -20"
} | {"status": "ok", "stdout": "", "exit_code": 0}full{"status": "ok", "stdout": "", "exit_code": 0} |
| 16 | run_command | {
"command": "grep -rn \"ForwardRef\" fastapi | head -20"
} | {"status": "ok", "stdout": "fastapi/dependencies/utils.py:11: ForwardRef,\nfastapi/dependencies/utils.py:212: annotation = ForwardRef(annotation)\n", "exit_codefull{"status": "ok", "stdout": "fastapi/dependencies/utils.py:11: ForwardRef,\nfastapi/dependencies/utils.py:212: annotation = ForwardRef(annotation)\n", "exit_code": 0} |
| 17 | read_file | {
"filepath": "fastapi/dependencies/utils.py",
"start_line": 1,
"end_line": 50
} | {"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "import dataclasses\nimport inspect\nfrom contextlib import AsyncExitStack, contextmanafull{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": "import dataclasses\nimport inspect\nfrom contextlib import AsyncExitStack, contextmanager\nfrom copy import copy, deepcopy\nfrom dataclasses import dataclass\nfrom typing import (\n Any,\n Callable,\n Coroutine,\n Dict,\n ForwardRef,\n List,\n Mapping,\n Optional,\n Sequence,\n Tuple,\n Type,\n Union,\n cast,\n)\n\nimport anyio\nfrom fastapi import params\nfrom fastapi._compat import (\n PYDANTIC_V2,\n ModelField,\n RequiredParam,\n Undefined,\n _is_error_wrapper,\n _is_model_class,\n copy_field_info,\n create_body_model,\n evaluate_forwardref,\n field_annotation_is_scalar,\n get_annotation_from_field_info,\n get_cached_model_fields,\n get_missing_field_error,\n is_bytes_field,\n is_bytes_sequence_field,\n is_scalar_field,\n is_scalar_sequence_field,\n is_sequence_field,\n is_uploadfile_or_nonable_uploadfile_annotation,\n is_uploadfile_sequence_annotation,\n lenient_issubclass,\n may_v1,\n sequence_types,\n serialize_sequence_value,\n value_is_sequence,\n)\n", "start_line": 1, "end_line": 50, "total_lines": 1037, "is_truncated": false} |
| 18 | run_command | {
"command": "python3 -c \"from typing import ForwardRef; from pydantic._internal._typing_extra import eval_type_lenient; print(eval_type_lenient(ForwardRef('int'), {}, {}))\""
} | {"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0}full{"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0} |
| 19 | run_command | {
"command": "python3 -c \"from typing import ForwardRef; from pydantic._internal._typing_extra import eval_type_lenient; print(eval_type_lenient('int', {}, {}))\""
} | {"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0}full{"status": "ok", "stdout": "<class 'int'>\n", "exit_code": 0} |
| 20 | run_command | {
"command": "grep -rn \"evaluate_forwardref\" fastapi | head -20"
} | {"status": "ok", "stdout": "fastapi/dependencies/utils.py:33: evaluate_forwardref,\nfastapi/dependencies/utils.py:213: annotation = evaluate_forwardref(annotatifull{"status": "ok", "stdout": "fastapi/dependencies/utils.py:33: evaluate_forwardref,\nfastapi/dependencies/utils.py:213: annotation = evaluate_forwardref(annotation, globalns, globalns)\nfastapi/_compat/__init__.py:17:from .main import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/v1.py:69: from pydantic.typing import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/v1.py:122: from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/main.py:29: from .v2 import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/main.py:45: from .v1 import evaluate_forwardref as evaluate_forwardref\nfastapi/_compat/v2.py:50:evaluate_forwardref = eval_type_lenient\n", "exit_code": 0} |
| 21 | read_file | {
"end_line": 220,
"filepath": "fastapi/dependencies/utils.py",
"start_line": 200
} | {"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": " kind=param.kind,\n default=param.default,\n annotation=get_typed_annotation(param.annfull{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "content": " kind=param.kind,\n default=param.default,\n annotation=get_typed_annotation(param.annotation, globalns),\n )\n for param in signature.parameters.values()\n ]\n typed_signature = inspect.Signature(typed_params)\n return typed_signature\n\n\ndef get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:\n if isinstance(annotation, str):\n annotation = ForwardRef(annotation)\n annotation = evaluate_forwardref(annotation, globalns, globalns)\n if annotation is type(None):\n return None\n return annotation\n\n\ndef get_typed_return_annotation(call: Callable[..., Any]) -> Any:\n signature = inspect.signature(call)\n", "start_line": 200, "end_line": 220, "total_lines": 1037, "is_truncated": false} |
| 22 | run_command | {
"command": "python3 -c \"import typing; from typing import ForwardRef; print(f'ForwardRef is instance of str: {isinstance(ForwardRef(\\\"int\\\"), str)}'); print(f'str is instance of str: {isinstance(\\\"int\\\", str)}')\""
} | {"status": "ok", "stdout": "ForwardRef is instance of str: False\nstr is instance of str: True\n", "exit_code": 0}full{"status": "ok", "stdout": "ForwardRef is instance of str: False\nstr is instance of str: True\n", "exit_code": 0} |
| 23 | run_command | {
"command": "python3 -c \"import typing; from typing import ForwardRef; from pydantic._internal._typing_extra import eval_type_lenient; \ndef test_eval(ann, ns):\n if isinstance(ann, str):\n ann = ForwardRef(ann)\n ann = eval_type_lenient(ann, ns, ns)\n return ann\n\nprint(test_eval('int', {}))\nprint(test_eval(ForwardRef('int'), {}))\""
} | {"status": "ok", "stdout": "<class 'int'>\nForwardRef('int')\n", "exit_code": 0}full{"status": "ok", "stdout": "<class 'int'>\nForwardRef('int')\n", "exit_code": 0} |
| 24 | run_command | {
"command": "python3 -c \"import typing; from typing import ForwardRef; from pydantic._internal._typing_extra import eval_type_lenient; \ndef test_eval(ann, ns):\n if isinstance(ann, (str, ForwardRef)):\n if isinstance(ann, str):\n ann = ForwardRef(ann)\n ann = eval_type_lenient(ann, ns, ns)\n return ann\n\nprint(test_eval('int', {}))\nprint(test_eval(ForwardRef('int'), {}))\""
} | {"status": "ok", "stdout": "<class 'int'>\n<class 'int'>\n", "exit_code": 0}full{"status": "ok", "stdout": "<class 'int'>\n<class 'int'>\n", "exit_code": 0} |
| 25 | edit_file | {
"filepath": "fastapi/dependencies/utils.py",
"new_string": "def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:\n if isinstance(annotation, (str, ForwardRef)):\n if isinstance(annotation, str):\n annotation = ForwardRef(annotation)\n annotation = evaluate_forwardref(annotation, globalns, globalns)\n if annotation is type(None):\n return None\n return annotation",
"old_string": "def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:\n if isinstance(annotation, str):\n annotation = ForwardRef(annotation)\n annotation = evaluate_forwardref(annotation, globalns, globalns)\n if annotation is type(None):\n return None\n return annotation"
} | {"status": "ok", "filepath": "fastapi/dependencies/utils.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/dependencies/utils.py\n+++ b/fastapifull{"status": "ok", "filepath": "fastapi/dependencies/utils.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/dependencies/utils.py\n+++ b/fastapi/dependencies/utils.py\n@@ -208,8 +208,9 @@\n \n \n def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:\n- if isinstance(annotation, str):\n- annotation = ForwardRef(annotation)\n+ if isinstance(annotation, (str, ForwardRef)):\n+ if isinstance(annotation, str):\n+ annotation = ForwardRef(annotation)\n annotation = evaluate_forwardref(annotation, globalns, globalns)\n if annotation is type(None):\n return None\n", "is_truncated": false} |
| 26 | submit_patch | [] | {"status": "ok", "patch_size": 712, "files_changed": 1}full{"status": "ok", "patch_size": 712, "files_changed": 1} |
diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py
index 91348c8e..db48ad33 100644
--- a/fastapi/dependencies/utils.py
+++ b/fastapi/dependencies/utils.py
@@ -208,8 +208,9 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
def get_typed_annotation(annotation: Any, globalns: Dict[str, Any]) -> Any:
- if isinstance(annotation, str):
- annotation = ForwardRef(annotation)
+ if isinstance(annotation, (str, ForwardRef)):
+ if isinstance(annotation, str):
+ annotation = ForwardRef(annotation)
annotation = evaluate_forwardref(annotation, globalns, globalns)
if annotation is type(None):
return None
F
=================================== FAILURES ===================================
_________________________ test_stringified_annotations _________________________
@needs_py310
def test_stringified_annotations():
app = FastAPI()
client = TestClient(app)
@app.get("/test/")
def call(test: Annotated[str, Depends(Dep())]):
return {"test": test}
response = client.get("/test")
> assert response.status_code == 200
E assert 422 == 200
E + where 422 = <Response [422 Unprocessable Entity]>.status_code
tests/test_stringified_annotations_simple.py:26: AssertionError
=============================== warnings summary ===============================
../../../../../../kaggle/tmp/envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45
/kaggle/tmp/envs/overlays/starlette-0.50.0-py3-none-any/starlette/testclient.py:45: 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
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed, 1 warning in 0.77s