diff --git a/moby_distribution/registry/resources/image.py b/moby_distribution/registry/resources/image.py index ab81ea0..3d91869 100644 --- a/moby_distribution/registry/resources/image.py +++ b/moby_distribution/registry/resources/image.py @@ -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 @@ -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 diff --git a/tests/integration/resources/test_manifests.py b/tests/integration/resources/test_manifests.py index 200642f..64e27c4 100644 --- a/tests/integration/resources/test_manifests.py +++ b/tests/integration/resources/test_manifests.py @@ -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 @@ -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, diff --git a/tests/registry/test_auth.py b/tests/registry/test_auth.py index f7cfad4..c4ae82e 100644 --- a/tests/registry/test_auth.py +++ b/tests/registry/test_auth.py @@ -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, @@ -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", @@ -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 diff --git a/tests/spec/conftest.py b/tests/spec/conftest.py index a5200e0..bfb64d3 100644 --- a/tests/spec/conftest.py +++ b/tests/spec/conftest.py @@ -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" @@ -24,7 +28,7 @@ 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()) @@ -32,7 +36,7 @@ def auth_response(): @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()) diff --git a/tests/spec/test_image_json.py b/tests/spec/test_image_json.py index 962f5a9..138a452 100644 --- a/tests/spec/test_image_json.py +++ b/tests/spec/test_image_json.py @@ -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