Repository: tiangolo/fastapi
File: fastapi/_compat.py (Python)

Code before:
        def default(self) -> Any:
            return self.get_default()

        @property
        def type_(self) -> Any:
            return self.field_info.annotation

        def __post_init__(self) -> None:
            with warnings.catch_warnings():
                # Pydantic >= 2.12.0a1 warns about this when building
                # TypeAdapters from field information that uses aliases.
                # The Pydantic team recommends ignoring this in this case:
                # https://github.com/fastapi/fastapi/pull/14036#issuecomment-3316045587
                try:
                    from pydantic.warnings import (  # type: ignore[attr-defined]
                        UnsupportedFieldAttributeWarning,
                    )
                except ImportError:  # pragma: no cover
                    pass
                else:  # pragma: no cover
                    warnings.simplefilter(
                        "ignore", category=UnsupportedFieldAttributeWarning
                    )
                self._type_adapter: TypeAdapter[Any] = TypeAdapter(
                    Annotated[self.field_info.annotation, self.field_info]
                )

        def get_default(self) -> Any:
            if self.field_info.is_required():
                return Undefined
            return self.field_info.get_default(call_default_factory=True)

        def validate(
            self,
            value: Any,
            values: Dict[str, Any] = {},  # noqa: B006
            *,
            loc: Tuple[Union[int, str], ...] = (),
        ) -> Tuple[Any, Union[List[Dict[str, Any]], None]]:
            try:
                return (
                    self._type_adapter.validate_python(value, from_attributes=True),
                    None,
                )
            except ValidationError as exc:
                return None, _regenerate_error_with_loc(
                    errors=exc.errors(include_url=False), loc_prefix=loc
                )

        def serialize(
            self,

Review comment (suggestion):
- I believe you need to use the `catch_warnings` context manager, because as it stands the warning will always be ignored from now on.
- I would suggest using an explicit version check on the `pydantic` version instead of a `try..except` statement, so that it can easily be unconditionally imported once FastAPI drops support for Pydantic < 2.12.

Code after:
        def default(self) -> Any:
            return self.get_default()

        @property
        def type_(self) -> Any:
            return self.field_info.annotation

        def __post_init__(self) -> None:
            with warnings.catch_warnings():
                # Pydantic >= 2.12.0 warns about field specific metadata that is unused
                # (e.g. `TypeAdapter(Annotated[int, Field(alias='b')])`). In some cases, we
                # end up building the type adapter from a model field annotation so we
                # need to ignore the warning:
                if PYDANTIC_VERSION_MINOR_TUPLE >= (2, 12):
                    from pydantic.warnings import UnsupportedFieldAttributeWarning

                    warnings.simplefilter(
                        "ignore", category=UnsupportedFieldAttributeWarning
                    )
                self._type_adapter: TypeAdapter[Any] = TypeAdapter(
                    Annotated[self.field_info.annotation, self.field_info]
                )

        def get_default(self) -> Any:
            if self.field_info.is_required():
                return Undefined
            return self.field_info.get_default(call_default_factory=True)

        def validate(
            self,
            value: Any,
            values: Dict[str, Any] = {},  # noqa: B006
            *,
            loc: Tuple[Union[int, str], ...] = (),
        ) -> Tuple[Any, Union[List[Dict[str, Any]], None]]:
            try:
                return (
                    self._type_adapter.validate_python(value, from_attributes=True),
                    None,
                )
            except ValidationError as exc:
                return None, _regenerate_error_with_loc(
                    errors=exc.errors(include_url=False), loc_prefix=loc
                )

        def serialize(
            self,
            value: Any,
            *,
            mode: Literal["json", "python"] = "json",
            include: Union[IncEx, None] = None,