From cb7ac13a2185cda98e578d9578f98e7e9a844a4f Mon Sep 17 00:00:00 2001 From: Giancarlo Romeo Date: Mon, 7 Oct 2024 14:49:55 +0200 Subject: [PATCH] add common modules --- .../{utils => }/pydantic_fields_extension.py | 0 .../pydantic_networks_extension.py | 5 +++++ .../{utils => }/serialization.py | 2 +- ...n.py => test_pydantic_fields_extension.py} | 6 +----- .../tests/test_pydantic_networks_extension.py | 20 +++++++++++++++++++ ...serialization.py => test_serialization.py} | 2 +- 6 files changed, 28 insertions(+), 7 deletions(-) rename packages/common-library/src/common_library/{utils => }/pydantic_fields_extension.py (100%) create mode 100644 packages/common-library/src/common_library/pydantic_networks_extension.py rename packages/common-library/src/common_library/{utils => }/serialization.py (93%) rename packages/common-library/tests/{test_utils_pydantic_extension.py => test_pydantic_fields_extension.py} (92%) create mode 100644 packages/common-library/tests/test_pydantic_networks_extension.py rename packages/common-library/tests/{test_utils_serialization.py => test_serialization.py} (90%) diff --git a/packages/common-library/src/common_library/utils/pydantic_fields_extension.py b/packages/common-library/src/common_library/pydantic_fields_extension.py similarity index 100% rename from packages/common-library/src/common_library/utils/pydantic_fields_extension.py rename to packages/common-library/src/common_library/pydantic_fields_extension.py diff --git a/packages/common-library/src/common_library/pydantic_networks_extension.py b/packages/common-library/src/common_library/pydantic_networks_extension.py new file mode 100644 index 00000000000..b53a2bfc8ae --- /dev/null +++ b/packages/common-library/src/common_library/pydantic_networks_extension.py @@ -0,0 +1,5 @@ +from typing import Annotated, TypeAlias +from pydantic import AfterValidator, AnyHttpUrl + + +AnyHttpUrlLegacy: TypeAlias = Annotated[str, AnyHttpUrl, AfterValidator(lambda u: u.rstrip("/"))] diff --git a/packages/common-library/src/common_library/utils/serialization.py b/packages/common-library/src/common_library/serialization.py similarity index 93% rename from packages/common-library/src/common_library/utils/serialization.py rename to packages/common-library/src/common_library/serialization.py index 91f41aad795..510bdf6a469 100644 --- a/packages/common-library/src/common_library/utils/serialization.py +++ b/packages/common-library/src/common_library/serialization.py @@ -1,6 +1,6 @@ from typing import Any -from common_library.utils.pydantic_fields_extension import get_type +from common_library.pydantic_fields_extension import get_type from pydantic import BaseModel, SecretStr diff --git a/packages/common-library/tests/test_utils_pydantic_extension.py b/packages/common-library/tests/test_pydantic_fields_extension.py similarity index 92% rename from packages/common-library/tests/test_utils_pydantic_extension.py rename to packages/common-library/tests/test_pydantic_fields_extension.py index 72634f5762d..50ff5443c41 100644 --- a/packages/common-library/tests/test_utils_pydantic_extension.py +++ b/packages/common-library/tests/test_pydantic_fields_extension.py @@ -1,11 +1,7 @@ from typing import Literal import pytest -from common_library.utils.pydantic_fields_extension import ( - get_type, - is_literal, - is_nullable, -) +from common_library.pydantic_fields_extension import get_type, is_literal, is_nullable from pydantic import BaseModel, Field diff --git a/packages/common-library/tests/test_pydantic_networks_extension.py b/packages/common-library/tests/test_pydantic_networks_extension.py new file mode 100644 index 00000000000..3390f7c2acf --- /dev/null +++ b/packages/common-library/tests/test_pydantic_networks_extension.py @@ -0,0 +1,20 @@ +from common_library.pydantic_networks_extension import AnyHttpUrlLegacy +from pydantic import AnyHttpUrl, TypeAdapter +from pydantic_core import Url + + +def test_any_http_url(): + url = TypeAdapter(AnyHttpUrl).validate_python( + "http://backgroud.testserver.io", + ) + + assert isinstance(url, Url) + assert f"{url}" == "http://backgroud.testserver.io/" # NOTE: trailing '/' added in Pydantic v2 + +def test_any_http_url_legacy(): + url = TypeAdapter(AnyHttpUrlLegacy).validate_python( + "http://backgroud.testserver.io", + ) + + assert isinstance(url, str) + assert url == "http://backgroud.testserver.io" diff --git a/packages/common-library/tests/test_utils_serialization.py b/packages/common-library/tests/test_serialization.py similarity index 90% rename from packages/common-library/tests/test_utils_serialization.py rename to packages/common-library/tests/test_serialization.py index 3a55de6026b..d53db58809c 100644 --- a/packages/common-library/tests/test_utils_serialization.py +++ b/packages/common-library/tests/test_serialization.py @@ -1,7 +1,7 @@ from typing import Final import pytest -from models_library.utils.serialization import model_dump_with_secrets +from common_library.serialization import model_dump_with_secrets from pydantic import BaseModel, SecretStr