diff --git a/aws_lambda_powertools/event_handler/openapi/compat.py b/aws_lambda_powertools/event_handler/openapi/compat.py index 54b78f7e5f6..bd102aa7b93 100644 --- a/aws_lambda_powertools/event_handler/openapi/compat.py +++ b/aws_lambda_powertools/event_handler/openapi/compat.py @@ -15,9 +15,9 @@ from pydantic import BaseModel, create_model from pydantic.fields import FieldInfo +from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2 from aws_lambda_powertools.event_handler.openapi.types import ( COMPONENT_REF_PREFIX, - PYDANTIC_V2, ModelNameMap, UnionType, ) diff --git a/aws_lambda_powertools/event_handler/openapi/models.py b/aws_lambda_powertools/event_handler/openapi/models.py index bbbc160f1e6..ab97b6dc2e7 100644 --- a/aws_lambda_powertools/event_handler/openapi/models.py +++ b/aws_lambda_powertools/event_handler/openapi/models.py @@ -4,7 +4,7 @@ from pydantic import AnyUrl, BaseModel, Field from aws_lambda_powertools.event_handler.openapi.compat import model_rebuild -from aws_lambda_powertools.event_handler.openapi.types import PYDANTIC_V2 +from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2 from aws_lambda_powertools.shared.types import Annotated, Literal """ diff --git a/aws_lambda_powertools/event_handler/openapi/params.py b/aws_lambda_powertools/event_handler/openapi/params.py index 28154466ff6..f267a4841f5 100644 --- a/aws_lambda_powertools/event_handler/openapi/params.py +++ b/aws_lambda_powertools/event_handler/openapi/params.py @@ -15,7 +15,8 @@ field_annotation_is_scalar, get_annotation_from_field_info, ) -from aws_lambda_powertools.event_handler.openapi.types import PYDANTIC_V2, CacheKey +from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2 +from aws_lambda_powertools.event_handler.openapi.types import CacheKey from aws_lambda_powertools.shared.types import Annotated, Literal, get_args, get_origin """ diff --git a/aws_lambda_powertools/event_handler/openapi/pydantic_loader.py b/aws_lambda_powertools/event_handler/openapi/pydantic_loader.py new file mode 100644 index 00000000000..12f06dad899 --- /dev/null +++ b/aws_lambda_powertools/event_handler/openapi/pydantic_loader.py @@ -0,0 +1,6 @@ +try: + from pydantic.version import VERSION as PYDANTIC_VERSION + + PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.") +except ImportError: + PYDANTIC_V2 = False diff --git a/aws_lambda_powertools/event_handler/openapi/types.py b/aws_lambda_powertools/event_handler/openapi/types.py index 9161d8dc170..0d166de1131 100644 --- a/aws_lambda_powertools/event_handler/openapi/types.py +++ b/aws_lambda_powertools/event_handler/openapi/types.py @@ -16,13 +16,6 @@ COMPONENT_REF_TEMPLATE = "#/components/schemas/{model}" METHODS_WITH_BODY = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"} -try: - from pydantic.version import VERSION as PYDANTIC_VERSION - - PYDANTIC_V2 = PYDANTIC_VERSION.startswith("2.") -except ImportError: - PYDANTIC_V2 = False - validation_error_definition = { "title": "ValidationError", diff --git a/tests/functional/event_handler/test_bedrock_agent.py b/tests/functional/event_handler/test_bedrock_agent.py index 223f9bdbef1..df9fb66afc8 100644 --- a/tests/functional/event_handler/test_bedrock_agent.py +++ b/tests/functional/event_handler/test_bedrock_agent.py @@ -2,7 +2,7 @@ from typing import Any, Dict from aws_lambda_powertools.event_handler import BedrockAgentResolver, Response, content_types -from aws_lambda_powertools.event_handler.openapi.types import PYDANTIC_V2 +from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2 from aws_lambda_powertools.utilities.data_classes import BedrockAgentEvent from tests.functional.utils import load_event