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

Code before:
        mode: Literal["validation", "serialization"] = "validation"

        @property
        def alias(self) -> str:
            a = self.field_info.alias
            return a if a is not None else self.name

        @property
        def required(self) -> bool:
            return self.field_info.is_required()

        @property
        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):  # pragma: no cover
                    from pydantic.warnings import (  # type: ignore[attr-defined]
                        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 (

Review comment (suggestion):
Once Pydantic 2.12 is released, we'll be able to remove the `no cover` statement here.

Code after:
        mode: Literal["validation", "serialization"] = "validation"

        @property
        def alias(self) -> str:
            a = self.field_info.alias
            return a if a is not None else self.name

        @property
        def required(self) -> bool:
            return self.field_info.is_required()

        @property
        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,