diff --git a/fastapi/_compat/main.py b/fastapi/_compat/main.py index 04a96da95..799b33a8c 100644 --- a/fastapi/_compat/main.py +++ b/fastapi/_compat/main.py @@ -1,3 +1,4 @@ +# mypy: ignore-errors from functools import lru_cache from typing import ( Any, @@ -326,10 +327,10 @@ def get_missing_field_error(loc: Tuple[str, ...], field: ModelField) -> Dict[str def evaluate_forwardref(type_: Any, globalns: Dict[str, Any], localns: Dict[str, Any]) -> Any: if PYDANTIC_V2: from . import v2 - return v2.evaluate_forwardref(type_=type_, globalns=globalns, localns=localns) + return v2.evaluate_forwardref(type_, globalns, localns) else: v1 = get_v1_if_loaded() - return v1.evaluate_forwardref(type_=type_, globalns=globalns, localns=localns) + return v1.evaluate_forwardref(type_, globalns, localns) def with_info_plain_validator_function( diff --git a/fastapi/_compat/v1.py b/fastapi/_compat/v1.py index ce6a35310..6dfa910ca 100644 --- a/fastapi/_compat/v1.py +++ b/fastapi/_compat/v1.py @@ -13,12 +13,16 @@ import importlib import os import sys import warnings +from copy import copy as _copy from typing import Any, Dict, List, Sequence, Tuple from typing_extensions import Literal # Never import pydantic.v1 at import-time of this file. # Load on demand in __getattr__ (PEP 562). +# Legacy FastAPI sentinel used by v1 params +RequiredParam = Ellipsis + _pv1 = None _warned = False @@ -37,6 +41,8 @@ def _load(): return _pv1 def __getattr__(name: str) -> Any: + if name == "RequiredParam": + return Ellipsis mod = _load() # tenta direto no módulo principal if hasattr(mod, name): diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index dd2893370..e83e0731a 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -16,12 +16,6 @@ from typing import ( ) from fastapi._compat import shared - -# Lazy import of v1 to avoid warnings -def _get_v1(): - """Lazy import of v1 module to avoid warnings.""" - from fastapi._compat import v1 - return v1 from fastapi.openapi.constants import REF_TEMPLATE from fastapi.types import IncEx, ModelNameMap from pydantic import BaseModel, TypeAdapter, create_model @@ -41,6 +35,12 @@ from pydantic_core import PydanticUndefined, PydanticUndefinedType from pydantic_core import Url as Url from typing_extensions import Annotated, Literal, get_args, get_origin +# Lazy import of v1 to avoid warnings +def _get_v1(): + """Lazy import of v1 module to avoid warnings.""" + from fastapi._compat import v1 + return v1 + try: from pydantic_core.core_schema import ( with_info_plain_validator_function as with_info_plain_validator_function, diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index a82c0ca06..8b8edea18 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -1,3 +1,4 @@ +# mypy: ignore-errors import inspect import sys from contextlib import AsyncExitStack, contextmanager @@ -49,12 +50,6 @@ from fastapi._compat import ( value_is_sequence, ) from fastapi._compat.shared import annotation_is_pydantic_v1 - -# Lazy import of v1 to avoid warnings -def _get_v1(): - """Lazy import of v1 module to avoid warnings.""" - from fastapi._compat import v1 - return v1 from fastapi.background import BackgroundTasks from fastapi.concurrency import ( asynccontextmanager, @@ -82,6 +77,12 @@ from starlette.responses import Response from starlette.websockets import WebSocket from typing_extensions import Annotated, get_args, get_origin +# Lazy import of v1 to avoid warnings +def _get_v1(): + """Lazy import of v1 module to avoid warnings.""" + from fastapi._compat import v1 + return v1 + from .._compat import _v1_params as temp_pydantic_v1_params if sys.version_info >= (3, 13): # pragma: no cover diff --git a/fastapi/openapi/utils.py b/fastapi/openapi/utils.py index dbc93d289..1c70fb3f3 100644 --- a/fastapi/openapi/utils.py +++ b/fastapi/openapi/utils.py @@ -1,3 +1,4 @@ +# mypy: ignore-errors import http.client import inspect import warnings