From f71b48206a62ee569d534774799d2a836abe2190 Mon Sep 17 00:00:00 2001 From: jo Date: Fri, 20 Oct 2023 15:10:45 +0200 Subject: [PATCH] feat: prepare for iso deprecated field removal --- hcloud/isos/domain.py | 17 +++++++++++++---- tests/unit/isos/conftest.py | 14 +++++++++++++- tests/unit/isos/test_client.py | 9 ++++++++- tests/unit/isos/test_domain.py | 7 ++++--- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/hcloud/isos/domain.py b/hcloud/isos/domain.py index 17676ec..5444c5a 100644 --- a/hcloud/isos/domain.py +++ b/hcloud/isos/domain.py @@ -1,6 +1,7 @@ from __future__ import annotations -from dateutil.parser import isoparse +from datetime import datetime +from warnings import warn from ..core import BaseDomain, DomainIdentityMixin from ..deprecation import DeprecationInfo @@ -32,7 +33,6 @@ class Iso(BaseDomain, DomainIdentityMixin): "type", "architecture", "description", - "deprecated", "deprecation", ) @@ -43,7 +43,7 @@ def __init__( type: str | None = None, architecture: str | None = None, description: str | None = None, - deprecated: str | None = None, + deprecated: str | None = None, # pylint: disable=unused-argument deprecation: dict | None = None, ): self.id = id @@ -51,7 +51,16 @@ def __init__( self.type = type self.architecture = architecture self.description = description - self.deprecated = isoparse(deprecated) if deprecated else None self.deprecation = ( DeprecationInfo.from_dict(deprecation) if deprecation is not None else None ) + + @property + def deprecated(self) -> datetime | None: + warn( + "The `deprecated` field is deprecated, please use the `deprecation` field instead.", + DeprecationWarning, + ) + if self.deprecation is None: + return None + return self.deprecation.unavailable_after diff --git a/tests/unit/isos/conftest.py b/tests/unit/isos/conftest.py index 0c045f5..3052f4a 100644 --- a/tests/unit/isos/conftest.py +++ b/tests/unit/isos/conftest.py @@ -13,6 +13,10 @@ def iso_response(): "type": "public", "architecture": "x86", "deprecated": "2018-02-28T00:00:00+00:00", + "deprecation": { + "announced": "2018-01-28T00:00:00+00:00", + "unavailable_after": "2018-02-28T00:00:00+00:00", + }, } } @@ -28,6 +32,10 @@ def two_isos_response(): "type": "public", "architecture": "x86", "deprecated": "2018-02-28T00:00:00+00:00", + "deprecation": { + "announced": "2018-01-28T00:00:00+00:00", + "unavailable_after": "2018-02-28T00:00:00+00:00", + }, }, { "id": 4712, @@ -35,7 +43,7 @@ def two_isos_response(): "description": "FreeBSD 11.0 x64", "type": "public", "architecture": "x86", - "deprecated": "2018-02-28T00:00:00+00:00", + "deprecated": None, }, ] } @@ -52,6 +60,10 @@ def one_isos_response(): "type": "public", "architecture": "x86", "deprecated": "2018-02-28T00:00:00+00:00", + "deprecation": { + "announced": "2018-01-28T00:00:00+00:00", + "unavailable_after": "2018-02-28T00:00:00+00:00", + }, } ] } diff --git a/tests/unit/isos/test_client.py b/tests/unit/isos/test_client.py index 7472184..fc64ab4 100644 --- a/tests/unit/isos/test_client.py +++ b/tests/unit/isos/test_client.py @@ -22,7 +22,14 @@ def test_bound_iso_init(self, iso_response): assert bound_iso.description == "FreeBSD 11.0 x64" assert bound_iso.type == "public" assert bound_iso.architecture == "x86" - assert bound_iso.deprecated == datetime.datetime( + with pytest.deprecated_call(): + assert bound_iso.deprecated == datetime.datetime( + 2018, 2, 28, 0, 0, tzinfo=timezone.utc + ) + assert bound_iso.deprecation.announced == datetime.datetime( + 2018, 1, 28, 0, 0, tzinfo=timezone.utc + ) + assert bound_iso.deprecation.unavailable_after == datetime.datetime( 2018, 2, 28, 0, 0, tzinfo=timezone.utc ) diff --git a/tests/unit/isos/test_domain.py b/tests/unit/isos/test_domain.py index 0e32e50..5017b52 100644 --- a/tests/unit/isos/test_domain.py +++ b/tests/unit/isos/test_domain.py @@ -26,9 +26,10 @@ def deprecated_iso(self): ) def test_deprecation(self, deprecated_iso: Iso): - assert deprecated_iso.deprecated == datetime( - 2023, 11, 5, 8, 27, 1, tzinfo=timezone.utc - ) + with pytest.deprecated_call(): + assert deprecated_iso.deprecated == datetime( + 2023, 11, 5, 8, 27, 1, tzinfo=timezone.utc + ) assert deprecated_iso.deprecation is not None assert deprecated_iso.deprecation.announced == datetime( 2023, 10, 5, 8, 27, 1, tzinfo=timezone.utc