Skip to content

Commit

Permalink
add common modules
Browse files Browse the repository at this point in the history
  • Loading branch information
giancarloromeo committed Oct 7, 2024
1 parent 1e57220 commit cb7ac13
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Annotated, TypeAlias
from pydantic import AfterValidator, AnyHttpUrl


AnyHttpUrlLegacy: TypeAlias = Annotated[str, AnyHttpUrl, AfterValidator(lambda u: u.rstrip("/"))]
Original file line number Diff line number Diff line change
@@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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


Expand Down
20 changes: 20 additions & 0 deletions packages/common-library/tests/test_pydantic_networks_extension.py
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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


Expand Down

0 comments on commit cb7ac13

Please sign in to comment.