|
1 | 1 | # mypy: ignore-errors
|
2 |
| -# flake8: noqa |
3 | 2 | from __future__ import annotations
|
4 | 3 |
|
5 | 4 | from collections import deque
|
6 |
| -from copy import copy |
| 5 | +from collections.abc import Mapping, Sequence |
7 | 6 |
|
8 | 7 | # MAINTENANCE: remove when deprecating Pydantic v1. Mypy doesn't handle two different code paths that import different
|
9 | 8 | # versions of a module, so we need to ignore errors here.
|
10 |
| - |
11 | 9 | from dataclasses import dataclass, is_dataclass
|
12 |
| -from enum import Enum |
13 |
| -from typing import TYPE_CHECKING, Any, Deque, FrozenSet, List, Mapping, Sequence, Set, Tuple, Union |
14 |
| - |
15 |
| -from typing_extensions import Annotated, Literal, get_origin, get_args |
16 |
| - |
17 |
| -from pydantic import BaseModel, create_model |
18 |
| -from pydantic.fields import FieldInfo |
| 10 | +from typing import TYPE_CHECKING, Any, Deque, FrozenSet, List, Set, Tuple, Union |
19 | 11 |
|
20 |
| -from aws_lambda_powertools.event_handler.openapi.types import COMPONENT_REF_PREFIX, UnionType |
21 |
| - |
22 |
| -from pydantic import TypeAdapter, ValidationError |
| 12 | +from pydantic import BaseModel, TypeAdapter, ValidationError, create_model |
23 | 13 |
|
24 | 14 | # Importing from internal libraries in Pydantic may introduce potential risks, as these internal libraries
|
25 | 15 | # are not part of the public API and may change without notice in future releases.
|
26 | 16 | # We use this for forward reference, as it allows us to handle forward references in type annotations.
|
27 | 17 | from pydantic._internal._typing_extra import eval_type_lenient
|
28 |
| -from pydantic.fields import FieldInfo |
29 | 18 | from pydantic._internal._utils import lenient_issubclass
|
30 |
| -from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue |
31 | 19 | from pydantic_core import PydanticUndefined, PydanticUndefinedType
|
| 20 | +from typing_extensions import Annotated, Literal, get_args, get_origin |
| 21 | + |
| 22 | +from aws_lambda_powertools.event_handler.openapi.types import UnionType |
32 | 23 |
|
33 | 24 | if TYPE_CHECKING:
|
| 25 | + from pydantic.fields import FieldInfo |
| 26 | + from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue |
| 27 | + |
34 | 28 | from aws_lambda_powertools.event_handler.openapi.types import IncEx, ModelNameMap
|
35 | 29 |
|
36 | 30 | Undefined = PydanticUndefined
|
@@ -119,7 +113,10 @@ def serialize(
|
119 | 113 | )
|
120 | 114 |
|
121 | 115 | def validate(
|
122 |
| - self, value: Any, values: dict[str, Any] = {}, *, loc: tuple[int | str, ...] = () |
| 116 | + self, |
| 117 | + value: Any, |
| 118 | + *, |
| 119 | + loc: tuple[int | str, ...] = (), |
123 | 120 | ) -> tuple[Any, list[dict[str, Any]] | None]:
|
124 | 121 | try:
|
125 | 122 | return (self._type_adapter.validate_python(value, from_attributes=True), None)
|
@@ -184,7 +181,8 @@ def copy_field_info(*, field_info: FieldInfo, annotation: Any) -> FieldInfo:
|
184 | 181 |
|
185 | 182 | def get_missing_field_error(loc: tuple[str, ...]) -> dict[str, Any]:
|
186 | 183 | error = ValidationError.from_exception_data(
|
187 |
| - "Field required", [{"type": "missing", "loc": loc, "input": {}}] |
| 184 | + "Field required", |
| 185 | + [{"type": "missing", "loc": loc, "input": {}}], |
188 | 186 | ).errors()[0]
|
189 | 187 | error["input"] = None
|
190 | 188 | return error
|
@@ -308,7 +306,7 @@ def value_is_sequence(value: Any) -> bool:
|
308 | 306 |
|
309 | 307 | def _annotation_is_complex(annotation: type[Any] | None) -> bool:
|
310 | 308 | return (
|
311 |
| - lenient_issubclass(annotation, (BaseModel, Mapping)) # TODO: UploadFile |
| 309 | + lenient_issubclass(annotation, (BaseModel, Mapping)) # Keep it to UploadFile |
312 | 310 | or _annotation_is_sequence(annotation)
|
313 | 311 | or is_dataclass(annotation)
|
314 | 312 | )
|
|
0 commit comments