Skip to content

Commit

Permalink
enhance: support pydantic<=1.8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
shabbywu committed Apr 8, 2024
1 parent 49d6f3e commit 2c8347e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
10 changes: 8 additions & 2 deletions moby_distribution/registry/resources/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
from pathlib import Path
from typing import List, Optional

from pydantic import BaseModel, Field, __version__
from pydantic import BaseModel, Field

try:
from pydantic import __version__ as pydantic_version
except ImportError:
# pydantic <= 1.8.2 does not have __version__
from pydantic import VERSION as pydantic_version

from moby_distribution.registry.client import DockerRegistryV2Client, default_client
from moby_distribution.registry.resources import RepositoryResource
Expand Down Expand Up @@ -344,7 +350,7 @@ def image_json_str(self) -> str:
if not self._dirty:
return self._initial_config
image_json = self.image_json
if __version__.startswith("2."):
if pydantic_version.startswith("2."):
return json.dumps(
image_json.model_dump(
mode="json", exclude_unset=True, exclude_defaults=True
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/resources/test_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

import pytest

from pydantic import __version__
try:
from pydantic import __version__ as pydantic_version
except ImportError:
# pydantic <= 1.8.2 does not have __version__
from pydantic import VERSION as pydantic_version
from moby_distribution.registry.resources.manifests import ManifestRef, ManifestSchema1


Expand Down Expand Up @@ -53,7 +57,7 @@ def test_get_metadata(
):
ref = ManifestRef(repo=repo, client=registry_client, reference=reference)

if __version__.startswith("2."):
if pydantic_version.startswith("2."):
dumped = json.dumps(
ref.get(media_type).model_dump(mode="json", exclude_unset=True),
indent=3,
Expand Down
10 changes: 7 additions & 3 deletions tests/registry/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import requests
import requests_mock

from pydantic import __version__
try:
from pydantic import __version__ as pydantic_version
except ImportError:
# pydantic <= 1.8.2 does not have __version__
from pydantic import VERSION as pydantic_version
from moby_distribution.registry.auth import (
DockerRegistryTokenAuthentication,
HTTPBasicAuthentication,
Expand All @@ -25,7 +29,7 @@ def mock_adapter():

@pytest.fixture
def auth_response():
if __version__.startswith("2."):
if pydantic_version.startswith("2."):
return {
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiIsImtpZCI6IlBZWU86VEVXVTpWN0pIOjI2SlY6QVFUWjpMSkMzOlNYVko6WEdIQTozNEYyOjJMQVE6WlJNSzpaN1E2In0.eyJpc3MiOiJhdXRoLmRvY2tlci5jb20iLCJzdWIiOiJqbGhhd24iLCJhdWQiOiJyZWdpc3RyeS5kb2NrZXIuY29tIiwiZXhwIjoxNDE1Mzg3MzE1LCJuYmYiOjE0MTUzODcwMTUsImlhdCI6MTQxNTM4NzAxNSwianRpIjoidFlKQ08xYzZjbnl5N2tBbjBjN3JLUGdiVjFIMWJGd3MiLCJhY2Nlc3MiOlt7InR5cGUiOiJyZXBvc2l0b3J5IiwibmFtZSI6InNhbWFsYmEvbXktYXBwIiwiYWN0aW9ucyI6WyJwdXNoIl19XX0.QhflHPfbd6eVF4lM9bwYpFZIV0PfikbyXuLx959ykRTBpe3CYnzs6YBK8FToVb5R47920PVLrh8zuLzdCr9t3w",
"issued_at": "2009-11-10T23:00:00Z",
Expand Down Expand Up @@ -79,7 +83,7 @@ def test_authenticate(self, mock_adapter, auth_response):

authed = auth.authenticate("username", "password")
assert isinstance(authed, TokenAuthorizationProvider)
if __version__.startswith("2."):
if pydantic_version.startswith("2."):
assert (
authed.token_response.model_dump(mode="json", exclude_unset=True)
== auth_response
Expand Down
10 changes: 7 additions & 3 deletions tests/spec/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import json
from pathlib import Path

from pydantic import __version__
try:
from pydantic import __version__ as pydantic_version
except ImportError:
# pydantic <= 1.8.2 does not have __version__
from pydantic import VERSION as pydantic_version
import pytest

assets = Path(__file__).parent / "assets"
Expand All @@ -24,15 +28,15 @@ def oci_manifest_schema1_dict():

@pytest.fixture
def auth_response():
if __version__.startswith("2."):
if pydantic_version.startswith("2."):
return json.loads((assets / "auth_response.pydantic_v2.json").read_text())
else:
return json.loads((assets / "auth_response.pydantic_v1.json").read_text())


@pytest.fixture
def image_json_dict():
if __version__.startswith("2."):
if pydantic_version.startswith("2."):
return json.loads((assets / "image_json.pydantic_v2.json").read_text())
else:
return json.loads((assets / "image_json.pydantic_v1.json").read_text())
8 changes: 6 additions & 2 deletions tests/spec/test_image_json.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import json

from pydantic import __version__
try:
from pydantic import __version__ as pydantic_version
except ImportError:
# pydantic <= 1.8.2 does not have __version__
from pydantic import VERSION as pydantic_version
from moby_distribution.spec.image_json import ImageJSON


def test_image_json(image_json_dict):
if __version__.startswith("2."):
if pydantic_version.startswith("2."):
assert (
ImageJSON(**image_json_dict).model_dump(mode="json", exclude_unset=True)
== image_json_dict
Expand Down

0 comments on commit 2c8347e

Please sign in to comment.