← eval4_submission_v1b

fastapi_14186

failed WRONG_FIX wrong_fix · 16 tool calls · 200 s · fastapi/fastapi

Task input

🐛 Fix internal Pydantic v1 compatibility (warnings) for Python 3.14 and Pydantic 2.12.1

The Python 3.14 tests started failing with the Pydantic v.2.12.1 release, because of the combination of https://github.com/fastapi/fastapi/pull/14168 and https://github.com/pydantic/pydantic/pull/12367.

Basically we were importing Pydantic `v1` functionality even when the user had (only) Pydantic v2 installed. This was done in https://github.com/fastapi/fastapi/pull/14168 to support more easy migration from v1 to v2 by allowing mixing versions within the same app.

Here, we prevent importing any `v1` functionality by creating dummy classes for Python 3.14 / Pydantic 2.12.0 onwards, which [drops support](https://github.com/pydantic/pydantic/releases/tag/v2.12.0) for `v1` and emits a [warning](https://github.com/pydantic/pydantic/pull/12367) when importing `v1` functionality.

### TODO

- [x] fix coverage
- [ ] check warning is still shown to users at some point, and not just swallowed by this code.

Tool calls (17)

#ToolArgumentsResult
1run_command
{
 "command": "grep -rn \"pydantic.v1\" fastapi/_compat | head -20"
}
{"status": "ok", "stdout": "fastapi/_compat/__init__.py:36:from .shared import annotation_is_pydantic_v1 as annotation_is_pydantic_v1\nfastapi/_compat/shared.py
full
{"status": "ok", "stdout": "fastapi/_compat/__init__.py:36:from .shared import annotation_is_pydantic_v1 as annotation_is_pydantic_v1\nfastapi/_compat/shared.py:197:def annotation_is_pydantic_v1(annotation: Any) -> bool:\nfastapi/_compat/shared.py:207:            if annotation_is_pydantic_v1(sub_annotation):\nfastapi/_compat/v1.py:72:    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\nfastapi/_compat/v1.py:73:    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\nfastapi/_compat/v1.py:74:    from pydantic.v1 import (  # type: ignore[assignment]\nfastapi/_compat/v1.py:77:    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\nfastapi/_compat/v1.py:78:    from pydantic.v1.class_validators import Validator as Validator\nfastapi/_compat/v1.py:79:    from pydantic.v1.color import Color as Color  # type: ignore[assignment]\nfastapi/_compat/v1.py:80:    from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper\nfastapi/_compat/v1.py:81:    from pydantic.v1.errors import MissingError\nfastapi/_compat/v1.py:82:    from pydantic.v1.fields import (\nfastapi/_compat/v1.py:91:    from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]\nfastapi/_compat/v1.py:92:    from pydantic.v1.fields import ModelField as ModelField\nfastapi/_compat/v1.py:93:    from pydantic.v1.fields import Undefined as Undefined\nfastapi/_compat/v1.py:94:    from pydantic.v1.fields import UndefinedType as UndefinedType\nfastapi/_compat/v1.py:95:    from pydantic.v1.networks import AnyUrl as AnyUrl\nfastapi/_compat/v1.py:96:    from pydantic.v1.networks import (  # type: ignore[assignment]\nfastapi/_compat/v1.py:99:    from pydantic.v1.schema import TypeModelSet as TypeModelSet\nfastapi/_compat/v1.py:100:    from pydantic.v1.schema import (\n", "exit_code": 0}
2read_file
{
 "filepath": "fastapi/_compat/v1.py"
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "from copy import copy\nfrom dataclasses import dataclass, is_dataclass\nfrom enum import Enum\
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "from copy import copy\nfrom dataclasses import dataclass, is_dataclass\nfrom enum import Enum\nfrom typing import (\n    Any,\n    Callable,\n    Dict,\n    List,\n    Sequence,\n    Set,\n    Tuple,\n    Type,\n    Union,\n)\n\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_PREFIX\nfrom fastapi.types import ModelNameMap\nfrom pydantic.version import VERSION as PYDANTIC_VERSION\nfrom typing_extensions import Literal\n\nPYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\nRequiredParam: Any = Ellipsis\n\nif not PYDANTIC_V2:\n    from pydantic import BaseConfig as BaseConfig\n    from pydantic import BaseModel as BaseModel\n    from pydantic import ValidationError as ValidationError\n    from pydantic import create_model as create_model\n    from pydantic.class_validators import Validator as Validator\n    from pydantic.color import Color as Color\n    from pydantic.error_wrappers import ErrorWrapper as ErrorWrapper\n    from pydantic.errors import MissingError\n    from pydantic.fields import (  # type: ignore[attr-defined]\n        SHAPE_FROZENSET,\n        SHAPE_LIST,\n        SHAPE_SEQUENCE,\n        SHAPE_SET,\n        SHAPE_SINGLETON,\n        SHAPE_TUPLE,\n        SHAPE_TUPLE_ELLIPSIS,\n    )\n    from pydantic.fields import FieldInfo as FieldInfo\n    from pydantic.fields import ModelField as ModelField  # type: ignore[attr-defined]\n    from pydantic.fields import Undefined as Undefined  # type: ignore[attr-defined]\n    from pydantic.fields import (  # type: ignore[attr-defined]\n        UndefinedType as UndefinedType,\n    )\n    from pydantic.networks import AnyUrl as AnyUrl\n    from pydantic.networks import NameEmail as NameEmail\n    from pydantic.schema import 
3run_command
{
 "command": "python3 -c \"import pydantic; print(pydantic.VERSION)\""
}
{"status": "ok", "stdout": "2.13.5\n", "exit_code": 0}
full
{"status": "ok", "stdout": "2.13.5\n", "exit_code": 0}
4read_file
{
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 1,
 "end_line": 30
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "from copy import copy\nfrom dataclasses import dataclass, is_dataclass\nfrom enum import Enum\
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "from copy import copy\nfrom dataclasses import dataclass, is_dataclass\nfrom enum import Enum\nfrom typing import (\n    Any,\n    Callable,\n    Dict,\n    List,\n    Sequence,\n    Set,\n    Tuple,\n    Type,\n    Union,\n)\n\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_PREFIX\nfrom fastapi.types import ModelNameMap\nfrom pydantic.version import VERSION as PYDANTIC_VERSION\nfrom typing_extensions import Literal\n\nPYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\nRequiredParam: Any = Ellipsis\n\nif not PYDANTIC_V2:\n    from pydantic import BaseConfig as BaseConfig\n    from pydantic import BaseModel as BaseModel\n", "start_line": 1, "end_line": 30, "total_lines": 334, "is_truncated": false}
5edit_file
{
 "filepath": "fastapi/_compat/v1.py",
 "new_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\\\".\\\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Pydantic v2.12.0+ drops support for v1 and emits a warning when importing v1 functionality.\n# We avoid importing v1 functionality for Pydantic v2.12.0+ to prevent these warnings.\nPYDANTIC_V2_12_PLUS = PYDANTIC_V2 and PYDANTIC_VERSION_MINOR_TUPLE[1] >= 12\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\nRequiredParam: Any = Ellipsis",
 "old_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\\\".\\\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\nRequiredParam: Any = Ellipsis"
}
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
full
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
6read_file
{
 "end_line": 25,
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 15
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_PREFIX\nfrom fastapi.types import ModelNameMap\nfrom pydantic.version import VERSION as PYDANTIC_VERSION\nfrom typing_extensions import Literal\n\nPYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\n", "start_line": 15, "end_line": 25, "total_lines": 334, "is_truncated": false}
7edit_file
{
 "filepath": "fastapi/_compat/v1.py",
 "new_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\\\".\\\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Pydantic v2.12.0+ drops support for v1 and emits a warning when importing v1 functionality.\n# We avoid importing v1 functionality for Pydantic v2.12.0+ to prevent these warnings.\nPYDANTIC_V2_12_PLUS = PYDANTIC_V2 and PYDANTIC_VERSION_MINOR_TUPLE[1] >= 12\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.",
 "old_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\\\".\\\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required."
}
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
full
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
8read_file
{
 "end_line": 30,
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 15
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_PREFIX\nfrom fastapi.types import ModelNameMap\nfrom pydantic.version import VERSION as PYDANTIC_VERSION\nfrom typing_extensions import Literal\n\nPYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\nRequiredParam: Any = Ellipsis\n\nif not PYDANTIC_V2:\n    from pydantic import BaseConfig as BaseConfig\n    from pydantic import BaseModel as BaseModel\n", "start_line": 15, "end_line": 30, "total_lines": 334, "is_truncated": false}
9edit_file
{
 "filepath": "fastapi/_compat/v1.py",
 "new_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\\\".\\\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Pydantic v2.12.0+ drops support for v1 and emits a warning when importing v1 functionality.\n# We avoid importing v1 functionality for Pydantic v2.12.0+ to prevent these warnings.\nPYDANTIC_V2_12_PLUS = PYDANTIC_V2 and PYDANTIC_VERSION_MINOR_TUPLE[1] >= 12\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.",
 "old_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\\\".\\\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required."
}
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
full
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
10read_file
{
 "end_line": 25,
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 15
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "\nfrom fastapi._compat import shared\nfrom fastapi.openapi.constants import REF_PREFIX as REF_PREFIX\nfrom fastapi.types import ModelNameMap\nfrom pydantic.version import VERSION as PYDANTIC_VERSION\nfrom typing_extensions import Literal\n\nPYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.\n", "start_line": 15, "end_line": 25, "total_lines": 334, "is_truncated": false}
11edit_file
{
 "filepath": "fastapi/_compat/v1.py",
 "new_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Pydantic v2.12.0+ drops support for v1 and emits a warning when importing v1 functionality.\n# We avoid importing v1 functionality for Pydantic v2.12.0+ to prevent these warnings.\nPYDANTIC_V2_12_PLUS = PYDANTIC_V2 and PYDANTIC_VERSION_MINOR_TUPLE[1] >= 12\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required.",
 "old_string": "PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\nPYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n# Keeping old \"Required\" functionality from Pydantic V1, without\n# shadowing typing.Required."
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/_compat/v1.py\n+++ b/fastapi/_compat/v1.py\n
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/_compat/v1.py\n+++ b/fastapi/_compat/v1.py\n@@ -21,6 +21,9 @@\n \n PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(\".\")[:2])\n PYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2\n+# Pydantic v2.12.0+ drops support for v1 and emits a warning when importing v1 functionality.\n+# We avoid importing v1 functionality for Pydantic v2.12.0+ to prevent these warnings.\n+PYDANTIC_V2_12_PLUS = PYDANTIC_V2 and PYDANTIC_VERSION_MINOR_TUPLE[1] >= 12\n # Keeping old \"Required\" functionality from Pydantic V1, without\n # shadowing typing.Required.\n RequiredParam: Any = Ellipsis\n", "is_truncated": false}
12read_file
{
 "end_line": 110,
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 60
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": " get_flat_models_from_fields,\n model_process_schema,\n )\n from pydantic.schema import (\n ge
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "        get_flat_models_from_fields,\n        model_process_schema,\n    )\n    from pydantic.schema import (\n        get_annotation_from_field_info as get_annotation_from_field_info,\n    )\n    from pydantic.schema import get_flat_models_from_field as get_flat_models_from_field\n    from pydantic.schema import get_model_name_map as get_model_name_map\n    from pydantic.types import SecretBytes as SecretBytes\n    from pydantic.types import SecretStr as SecretStr\n    from pydantic.typing import evaluate_forwardref as evaluate_forwardref\n    from pydantic.utils import lenient_issubclass as lenient_issubclass\n\n\nelse:\n    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\n    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\n    from pydantic.v1 import (  # type: ignore[assignment]\n        ValidationError as ValidationError,\n    )\n    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\n    from pydantic.v1.class_validators import Validator as Validator\n    from pydantic.v1.color import Color as Color  # type: ignore[assignment]\n    from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper\n    from pydantic.v1.errors import MissingError\n    from pydantic.v1.fields import (\n        SHAPE_FROZENSET,\n        SHAPE_LIST,\n        SHAPE_SEQUENCE,\n        SHAPE_SET,\n        SHAPE_SINGLETON,\n        SHAPE_TUPLE,\n        SHAPE_TUPLE_ELLIPSIS,\n    )\n    from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]\n    from pydantic.v1.fields import ModelField as ModelField\n    from pydantic.v1.fields import Undefined as Undefined\n    from pydantic.v1.fields import UndefinedType as UndefinedType\n    from pydantic.v1.networks import AnyUrl as AnyUrl\n    from pydantic.v1.networks import (  # type: ignore[assignment]\n        NameEmail as NameEmail,\n    )\n    from pydantic.v1.sch
13read_file
{
 "end_line": 150,
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 111
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": " from pydantic.v1.schema import (\n get_flat_models_from_field as get_flat_models_from_field,\
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "    from pydantic.v1.schema import (\n        get_flat_models_from_field as get_flat_models_from_field,\n    )\n    from pydantic.v1.schema import get_model_name_map as get_model_name_map\n    from pydantic.v1.types import (  # type: ignore[assignment]\n        SecretBytes as SecretBytes,\n    )\n    from pydantic.v1.types import (  # type: ignore[assignment]\n        SecretStr as SecretStr,\n    )\n    from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref\n    from pydantic.v1.utils import lenient_issubclass as lenient_issubclass\n\n\nGetJsonSchemaHandler = Any\nJsonSchemaValue = Dict[str, Any]\nCoreSchema = Any\nUrl = AnyUrl\n\nsequence_shapes = {\n    SHAPE_LIST,\n    SHAPE_SET,\n    SHAPE_FROZENSET,\n    SHAPE_TUPLE,\n    SHAPE_SEQUENCE,\n    SHAPE_TUPLE_ELLIPSIS,\n}\nsequence_shape_to_type = {\n    SHAPE_LIST: list,\n    SHAPE_SET: set,\n    SHAPE_TUPLE: tuple,\n    SHAPE_SEQUENCE: list,\n    SHAPE_TUPLE_ELLIPSIS: list,\n}\n\n\n@dataclass\nclass GenerateJsonSchema:\n    ref_template: str\n\n", "start_line": 111, "end_line": 150, "total_lines": 337, "is_truncated": false}
14edit_file
{
 "filepath": "fastapi/_compat/v1.py",
 "new_string": "else:\\n    if PYDANTIC_V2_12_PLUS:\\n        # Dummy classes for Pydantic v2.12.0+ to avoid importing v1 functionality\\n        # and triggering warnings.\\n        BaseConfig = Any\\n        BaseModel = Any\\n        ValidationError = Any\\n        create_model = Any\\n        Validator = Any\\n        Color = Any\\n        ErrorWrapper = Any\\n        MissingError = Any\\n        SHAPE_FROZENSET = Any\\n        SHAPE_LIST = Any\\n        SHAPE_SEQUENCE = Any\\n        SHAPE_SET = Any\\n        SHAPE_SINGLETON = Any\\n        SHAPE_TUPLE = Any\\n        SHAPE_TUPLE_ELLIPSIS = Any\\n        FieldInfo = Any\\n        ModelField = Any\\n        Undefined = Any\\n        UndefinedType = Any\\n        AnyUrl = Any\\n        NameEmail = Any\\n        TypeModelSet = Any\\n        field_schema = Any\\n        get_flat_models_from_fields = Any\\n        model_process_schema = Any\\n        get_annotation_from_field_info = Any\\n        get_flat_models_from_field = Any\\n        get_model_name_map = Any\\n        SecretBytes = Any\\n        SecretStr = Any\\n        evaluate_forwardref = Any\\n        lenient_issubclass = Any\\n    else:\\n        from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\\n        from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\\n        from pydantic.v1 import (  # type: ignore[assignment]\\n            ValidationError as ValidationError,\\n        )\\n        from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\\n        from pydantic.v1.class_validators import Validator as Validator\\n        from pydantic.v1.color import Color as Color  # type: ignore[assignment]\\n        from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper\\n        from pydantic.v1.errors import MissingError\\n        from pydantic.v1.fields import (\\n            SHAPE_FROZENSET,\\n            SHAPE_LIST,\\n            SHAPE_SEQUENCE,\\n            SHAPE_SET,\\n            SHAPE_SINGLETON,\\n            SHAPE_TUPLE,\\n            SHAPE_TUPLE_ELLIPSIS,\\n        )\\n        from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]\\n        from pydantic.v1.fields import ModelField as ModelField\\n        from pydantic.v1.fields import Undefined as Undefined\\n        from pydantic.v1.fields import UndefinedType as UndefinedType\\n        from pydantic.v1.networks import AnyUrl as AnyUrl\\n        from pydantic.v1.networks import (  # type: ignore[assignment]\\n            NameEmail as NameEmail,\\n        )\\n        from pydantic.v1.schema import TypeModelSet as TypeModelSet\\n        from pydantic.v1.schema import (\\n            field_schema,\\n            get_flat_models_from_fields,\\n            model_process_schema,\\n        )\\n        from pydantic.v1.schema import (\\n            get_annotation_from_field_info as get_annotation_from_field_info,\\n        )\\n        from pydantic.v1.schema import (\\n            get_flat_models_from_field as get_flat_models_from_field,\\n        )\\n        from pydantic.v1.schema import get_model_name_map as get_model_name_map\\n        from pydantic.v1.types import (  # type: ignore[assignment]\\n            SecretBytes as SecretBytes,\\n        )\\n        from pydantic.v1.types import (  # type: ignore[assignment]\\n            SecretStr as SecretStr,\\n        )\\n        from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref\\n        from pydantic.v1.utils import lenient_issubclass as lenient_issubclass",
 "old_string": "else:\\n    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\\n    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\\n    from pydantic.v1 import (  # type: ignore[assignment]\\n        ValidationError as ValidationError,\\n    )\\n    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\\n    fro
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
full
{"status": "error", "error_type": "FileEditError", "error_message": "old_string not found in file (tried exact, flexible and regex matching)"}
15read_file
{
 "end_line": 140,
 "filepath": "fastapi/_compat/v1.py",
 "start_line": 70
}
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": " from pydantic.typing import evaluate_forwardref as evaluate_forwardref\n from pydantic.utils
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "content": "    from pydantic.typing import evaluate_forwardref as evaluate_forwardref\n    from pydantic.utils import lenient_issubclass as lenient_issubclass\n\n\nelse:\n    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\n    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\n    from pydantic.v1 import (  # type: ignore[assignment]\n        ValidationError as ValidationError,\n    )\n    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\n    from pydantic.v1.class_validators import Validator as Validator\n    from pydantic.v1.color import Color as Color  # type: ignore[assignment]\n    from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper\n    from pydantic.v1.errors import MissingError\n    from pydantic.v1.fields import (\n        SHAPE_FROZENSET,\n        SHAPE_LIST,\n        SHAPE_SEQUENCE,\n        SHAPE_SET,\n        SHAPE_SINGLETON,\n        SHAPE_TUPLE,\n        SHAPE_TUPLE_ELLIPSIS,\n    )\n    from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]\n    from pydantic.v1.fields import ModelField as ModelField\n    from pydantic.v1.fields import Undefined as Undefined\n    from pydantic.v1.fields import UndefinedType as UndefinedType\n    from pydantic.v1.networks import AnyUrl as AnyUrl\n    from pydantic.v1.networks import (  # type: ignore[assignment]\n        NameEmail as NameEmail,\n    )\n    from pydantic.v1.schema import TypeModelSet as TypeModelSet\n    from pydantic.v1.schema import (\n        field_schema,\n        get_flat_models_from_fields,\n        model_process_schema,\n    )\n    from pydantic.v1.schema import (\n        get_annotation_from_field_info as get_annotation_from_field_info,\n    )\n    from pydantic.v1.schema import (\n        get_flat_models_from_field as get_flat_models_from_field,\n    )\n    from pydantic.v1.schema import get_model_name_map as get_
16edit_file
{
 "filepath": "fastapi/_compat/v1.py",
 "new_string": "else:\n    if PYDANTIC_V2_12_PLUS:\n        # Dummy classes for Pydantic v2.12.0+ to avoid importing v1 functionality\n        # and triggering warnings.\n        BaseConfig = Any\n        BaseModel = Any\n        ValidationError = Any\n        create_model = Any\n        Validator = Any\n        Color = Any\n        ErrorWrapper = Any\n        MissingError = Any\n        SHAPE_FROZENSET = Any\n        SHAPE_LIST = Any\n        SHAPE_SEQUENCE = Any\n        SHAPE_SET = Any\n        SHAPE_SINGLETON = Any\n        SHAPE_TUPLE = Any\n        SHAPE_TUPLE_ELLIPSIS = Any\n        FieldInfo = Any\n        ModelField = Any\n        Undefined = Any\n        UndefinedType = Any\n        AnyUrl = Any\n        NameEmail = Any\n        TypeModelSet = Any\n        field_schema = Any\n        get_flat_models_from_fields = Any\n        model_process_schema = Any\n        get_annotation_from_field_info = Any\n        get_flat_models_from_field = Any\n        get_model_name_map = Any\n        SecretBytes = Any\n        SecretStr = Any\n        evaluate_forwardref = Any\n        lenient_issubclass = Any\n    else:\n        from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\n        from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\n        from pydantic.v1 import (  # type: ignore[assignment]\n            ValidationError as ValidationError,\n        )\n        from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\n        from pydantic.v1.class_validators import Validator as Validator\n        from pydantic.v1.color import Color as Color  # type: ignore[assignment]\n        from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper\n        from pydantic.v1.errors import MissingError\n        from pydantic.v1.fields import (\n            SHAPE_FROZENSET,\n            SHAPE_LIST,\n            SHAPE_SEQUENCE,\n            SHAPE_SET,\n            SHAPE_SINGLETON,\n            SHAPE_TUPLE,\n            SHAPE_TUPLE_ELLIPSIS,\n        )\n        from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]\n        from pydantic.v1.fields import ModelField as ModelField\n        from pydantic.v1.fields import Undefined as Undefined\n        from pydantic.v1.fields import UndefinedType as UndefinedType\n        from pydantic.v1.networks import AnyUrl as AnyUrl\n        from pydantic.v1.networks import (  # type: ignore[assignment]\n            NameEmail as NameEmail,\n        )\n        from pydantic.v1.schema import TypeModelSet as TypeModelSet\n        from pydantic.v1.schema import (\n            field_schema,\n            get_flat_models_from_fields,\n            model_process_schema,\n        )\n        from pydantic.v1.schema import (\n            get_annotation_from_field_info as get_annotation_from_field_info,\n        )\n        from pydantic.v1.schema import (\n            get_flat_models_from_field as get_flat_models_from_field,\n        )\n        from pydantic.v1.schema import get_model_name_map as get_model_name_map\n        from pydantic.v1.types import (  # type: ignore[assignment]\n            SecretBytes as SecretBytes,\n        )\n        from pydantic.v1.types import (  # type: ignore[assignment]\n            SecretStr as SecretStr,\n        )\n        from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref\n        from pydantic.v1.utils import lenient_issubclass as lenient_issubclass",
 "old_string": "else:\n    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\n    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\n    from pydantic.v1 import (  # type: ignore[assignment]\n        ValidationError as ValidationError,\n    )\n    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\n    from pydantic.v1.class_validators import Validator as Validator\n    from pydantic.v1.color im
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/_compat/v1.py\n+++ b/fastapi/_compat/v1.py\n
full
{"status": "ok", "filepath": "fastapi/_compat/v1.py", "occurrences": 1, "strategy": "exact", "diff": "--- a/fastapi/_compat/v1.py\n+++ b/fastapi/_compat/v1.py\n@@ -72,54 +72,90 @@\n \n \n else:\n-    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]\n-    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]\n-    from pydantic.v1 import (  # type: ignore[assignment]\n-        ValidationError as ValidationError,\n-    )\n-    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]\n-    from pydantic.v1.class_validators import Validator as Validator\n-    from pydantic.v1.color import Color as Color  # type: ignore[assignment]\n-    from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper\n-    from pydantic.v1.errors import MissingError\n-    from pydantic.v1.fields import (\n-        SHAPE_FROZENSET,\n-        SHAPE_LIST,\n-        SHAPE_SEQUENCE,\n-        SHAPE_SET,\n-        SHAPE_SINGLETON,\n-        SHAPE_TUPLE,\n-        SHAPE_TUPLE_ELLIPSIS,\n-    )\n-    from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]\n-    from pydantic.v1.fields import ModelField as ModelField\n-    from pydantic.v1.fields import Undefined as Undefined\n-    from pydantic.v1.fields import UndefinedType as UndefinedType\n-    from pydantic.v1.networks import AnyUrl as AnyUrl\n-    from pydantic.v1.networks import (  # type: ignore[assignment]\n-        NameEmail as NameEmail,\n-    )\n-    from pydantic.v1.schema import TypeModelSet as TypeModelSet\n-    from pydantic.v1.schema import (\n-        field_schema,\n-        get_flat_models_from_fields,\n-        model_process_schema,\n-    )\n-    from pydantic.v1.schema import (\n-        get_annotation_from_field_info as get_annotation_from_field_info,\n-    )\n-    from pydantic.v1.schema import (\n-        get_flat_models_from_field as get_flat_models_from_field,\n-    )\n-    from pydantic.v1.schema import get_model_name_ma
17submit_patch
[]
{"status": "ok", "patch_size": 6431, "files_changed": 1}
full
{"status": "ok", "patch_size": 6431, "files_changed": 1}

Patch

diff --git a/fastapi/_compat/v1.py b/fastapi/_compat/v1.py
index f0ac5163..2142fe1c 100644
--- a/fastapi/_compat/v1.py
+++ b/fastapi/_compat/v1.py
@@ -21,6 +21,9 @@ from typing_extensions import Literal
 
 PYDANTIC_VERSION_MINOR_TUPLE = tuple(int(x) for x in PYDANTIC_VERSION.split(".")[:2])
 PYDANTIC_V2 = PYDANTIC_VERSION_MINOR_TUPLE[0] == 2
+# Pydantic v2.12.0+ drops support for v1 and emits a warning when importing v1 functionality.
+# We avoid importing v1 functionality for Pydantic v2.12.0+ to prevent these warnings.
+PYDANTIC_V2_12_PLUS = PYDANTIC_V2 and PYDANTIC_VERSION_MINOR_TUPLE[1] >= 12
 # Keeping old "Required" functionality from Pydantic V1, without
 # shadowing typing.Required.
 RequiredParam: Any = Ellipsis
@@ -69,54 +72,90 @@ if not PYDANTIC_V2:
 
 
 else:
-    from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]
-    from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]
-    from pydantic.v1 import (  # type: ignore[assignment]
-        ValidationError as ValidationError,
-    )
-    from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]
-    from pydantic.v1.class_validators import Validator as Validator
-    from pydantic.v1.color import Color as Color  # type: ignore[assignment]
-    from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper
-    from pydantic.v1.errors import MissingError
-    from pydantic.v1.fields import (
-        SHAPE_FROZENSET,
-        SHAPE_LIST,
-        SHAPE_SEQUENCE,
-        SHAPE_SET,
-        SHAPE_SINGLETON,
-        SHAPE_TUPLE,
-        SHAPE_TUPLE_ELLIPSIS,
-    )
-    from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]
-    from pydantic.v1.fields import ModelField as ModelField
-    from pydantic.v1.fields import Undefined as Undefined
-    from pydantic.v1.fields import UndefinedType as UndefinedType
-    from pydantic.v1.networks import AnyUrl as AnyUrl
-    from pydantic.v1.networks import (  # type: ignore[assignment]
-        NameEmail as NameEmail,
-    )
-    from pydantic.v1.schema import TypeModelSet as TypeModelSet
-    from pydantic.v1.schema import (
-        field_schema,
-        get_flat_models_from_fields,
-        model_process_schema,
-    )
-    from pydantic.v1.schema import (
-        get_annotation_from_field_info as get_annotation_from_field_info,
-    )
-    from pydantic.v1.schema import (
-        get_flat_models_from_field as get_flat_models_from_field,
-    )
-    from pydantic.v1.schema import get_model_name_map as get_model_name_map
-    from pydantic.v1.types import (  # type: ignore[assignment]
-        SecretBytes as SecretBytes,
-    )
-    from pydantic.v1.types import (  # type: ignore[assignment]
-        SecretStr as SecretStr,
-    )
-    from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref
-    from pydantic.v1.utils import lenient_issubclass as lenient_issubclass
+    if PYDANTIC_V2_12_PLUS:
+        # Dummy classes for Pydantic v2.12.0+ to avoid importing v1 functionality
+        # and triggering warnings.
+        BaseConfig = Any
+        BaseModel = Any
+        ValidationError = Any
+        create_model = Any
+        Validator = Any
+        Color = Any
+        ErrorWrapper = Any
+        MissingError = Any
+        SHAPE_FROZENSET = Any
+        SHAPE_LIST = Any
+        SHAPE_SEQUENCE = Any
+        SHAPE_SET = Any
+        SHAPE_SINGLETON = Any
+        SHAPE_TUPLE = Any
+        SHAPE_TUPLE_ELLIPSIS = Any
+        FieldInfo = Any
+        ModelField = Any
+        Undefined = Any
+        UndefinedType = Any
+        AnyUrl = Any
+        NameEmail = Any
+        TypeModelSet = Any
+        field_schema = Any
+        get_flat_models_from_fields = Any
+        model_process_schema = Any
+        get_annotation_from_field_info = Any
+        get_flat_models_from_field = Any
+        get_model_name_map = Any
+        SecretBytes = Any
+        SecretStr = Any
+        evaluate_forwardref = Any
+        lenient_issubclass = Any
+    else:
+        from pydantic.v1 import BaseConfig as BaseConfig  # type: ignore[assignment]
+        from pydantic.v1 import BaseModel as BaseModel  # type: ignore[assignment]
+        from pydantic.v1 import (  # type: ignore[assignment]
+            ValidationError as ValidationError,
+        )
+        from pydantic.v1 import create_model as create_model  # type: ignore[no-redef]
+        from pydantic.v1.class_validators import Validator as Validator
+        from pydantic.v1.color import Color as Color  # type: ignore[assignment]
+        from pydantic.v1.error_wrappers import ErrorWrapper as ErrorWrapper
+        from pydantic.v1.errors import MissingError
+        from pydantic.v1.fields import (
+            SHAPE_FROZENSET,
+            SHAPE_LIST,
+            SHAPE_SEQUENCE,
+            SHAPE_SET,
+            SHAPE_SINGLETON,
+            SHAPE_TUPLE,
+            SHAPE_TUPLE_ELLIPSIS,
+        )
+        from pydantic.v1.fields import FieldInfo as FieldInfo  # type: ignore[assignment]
+        from pydantic.v1.fields import ModelField as ModelField
+        from pydantic.v1.fields import Undefined as Undefined
+        from pydantic.v1.fields import UndefinedType as UndefinedType
+        from pydantic.v1.networks import AnyUrl as AnyUrl
+        from pydantic.v1.networks import (  # type: ignore[assignment]
+            NameEmail as NameEmail,
+        )
+        from pydantic.v1.schema import TypeModelSet as TypeModelSet
+        from pydantic.v1.schema import (
+            field_schema,
+            get_flat_models_from_fields,
+            model_process_schema,
+        )
+        from pydantic.v1.schema import (
+            get_annotation_from_field_info as get_annotation_from_field_info,
+        )
+        from pydantic.v1.schema import (
+            get_flat_models_from_field as get_flat_models_from_field,
+        )
+        from pydantic.v1.schema import get_model_name_map as get_model_name_map
+        from pydantic.v1.types import (  # type: ignore[assignment]
+            SecretBytes as SecretBytes,
+        )
+        from pydantic.v1.types import (  # type: ignore[assignment]
+            SecretStr as SecretStr,
+        )
+        from pydantic.v1.typing import evaluate_forwardref as evaluate_forwardref
+        from pydantic.v1.utils import lenient_issubclass as lenient_issubclass
 
 
 GetJsonSchemaHandler = Any

Test output

show
==================================== ERRORS ====================================
____________________ ERROR collecting tests/test_compat.py _____________________
tests/test_compat.py:3: in <module>
    from fastapi import FastAPI, UploadFile
fastapi/__init__.py:7: in <module>
    from .applications import FastAPI as FastAPI
fastapi/applications.py:16: in <module>
    from fastapi import routing
fastapi/routing.py:27: in <module>
    from fastapi import params, temp_pydantic_v1_params
fastapi/params.py:5: in <module>
    from fastapi.openapi.models import Example
fastapi/openapi/models.py:4: in <module>
    from fastapi._compat import (
fastapi/_compat/__init__.py:1: in <module>
    from .main import BaseConfig as BaseConfig
fastapi/_compat/main.py:11: in <module>
    from fastapi._compat import v1
fastapi/_compat/v1.py:192: in <module>
    RequestErrorModel: Type[BaseModel] = create_model("Request")
                                         ^^^^^^^^^^^^^^^^^^^^^^^
/root/.local/share/uv/python/cpython-3.13.14-linux-x86_64-gnu/lib/python3.13/typing.py:612: in __new__
    raise TypeError("Any cannot be instantiated")
E   TypeError: Any cannot be instantiated
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 error in 0.32s