Skip to content

Commit 3a83ed7

Browse files
authored
refactor: allow differences between api object properties and slots (#421)
Allow differences between the classes slots and the API object properties.
1 parent c517c85 commit 3a83ed7

22 files changed

Lines changed: 127 additions & 61 deletions

File tree

hcloud/actions/domain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Action(BaseDomain):
3131
STATUS_ERROR = "error"
3232
"""Action Status error"""
3333

34-
__slots__ = (
34+
__api_properties__ = (
3535
"id",
3636
"command",
3737
"status",
@@ -41,6 +41,7 @@ class Action(BaseDomain):
4141
"started",
4242
"finished",
4343
)
44+
__slots__ = __api_properties__
4445

4546
def __init__(
4647
self,

hcloud/certificates/domain.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Certificate(BaseDomain, DomainIdentityMixin):
3131
:param status: ManagedCertificateStatus Current status of a type managed Certificate, always none for type uploaded Certificates
3232
"""
3333

34-
__slots__ = (
34+
__api_properties__ = (
3535
"id",
3636
"name",
3737
"certificate",
@@ -44,6 +44,8 @@ class Certificate(BaseDomain, DomainIdentityMixin):
4444
"type",
4545
"status",
4646
)
47+
__slots__ = __api_properties__
48+
4749
TYPE_UPLOADED = "uploaded"
4850
TYPE_MANAGED = "managed"
4951

@@ -119,7 +121,8 @@ class CreateManagedCertificateResponse(BaseDomain):
119121
Shows the progress of the certificate creation
120122
"""
121123

122-
__slots__ = ("certificate", "action")
124+
__api_properties__ = ("certificate", "action")
125+
__slots__ = __api_properties__
123126

124127
def __init__(
125128
self,

hcloud/core/domain.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33

44
class BaseDomain:
5-
__slots__ = ()
5+
__api_properties__: tuple
66

77
@classmethod
88
def from_dict(cls, data: dict): # type: ignore[no-untyped-def]
99
"""
1010
Build the domain object from the data dict.
1111
"""
12-
supported_data = {k: v for k, v in data.items() if k in cls.__slots__}
12+
supported_data = {k: v for k, v in data.items() if k in cls.__api_properties__}
1313
return cls(**supported_data)
1414

1515
def __repr__(self) -> str:
16-
kwargs = [f"{key}={getattr(self, key)!r}" for key in self.__slots__] # type: ignore[var-annotated]
16+
kwargs = [f"{key}={getattr(self, key)!r}" for key in self.__api_properties__] # type: ignore[var-annotated]
1717
return f"{self.__class__.__qualname__}({', '.join(kwargs)})"
1818

1919

2020
class DomainIdentityMixin:
21-
__slots__ = ()
2221

2322
id: int | None
2423
name: str | None
@@ -54,14 +53,15 @@ def has_id_or_name(self, id_or_name: int | str) -> bool:
5453

5554

5655
class Pagination(BaseDomain):
57-
__slots__ = (
56+
__api_properties__ = (
5857
"page",
5958
"per_page",
6059
"previous_page",
6160
"next_page",
6261
"last_page",
6362
"total_entries",
6463
)
64+
__slots__ = __api_properties__
6565

6666
def __init__(
6767
self,
@@ -81,7 +81,8 @@ def __init__(
8181

8282

8383
class Meta(BaseDomain):
84-
__slots__ = ("pagination",)
84+
__api_properties__ = ("pagination",)
85+
__slots__ = __api_properties__
8586

8687
def __init__(self, pagination: Pagination | None = None):
8788
self.pagination = pagination

hcloud/datacenters/domain.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Datacenter(BaseDomain, DomainIdentityMixin):
1919
:param server_types: :class:`DatacenterServerTypes <hcloud.datacenters.domain.DatacenterServerTypes>`
2020
"""
2121

22-
__slots__ = ("id", "name", "description", "location", "server_types")
22+
__api_properties__ = ("id", "name", "description", "location", "server_types")
23+
__slots__ = __api_properties__
2324

2425
def __init__(
2526
self,
@@ -47,7 +48,8 @@ class DatacenterServerTypes(BaseDomain):
4748
All available for migration (change type) server types for this datacenter
4849
"""
4950

50-
__slots__ = ("available", "supported", "available_for_migration")
51+
__api_properties__ = ("available", "supported", "available_for_migration")
52+
__slots__ = __api_properties__
5153

5254
def __init__(
5355
self,

hcloud/deprecation/domain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ class DeprecationInfo(BaseDomain):
1717
new servers with this image after the mentioned date.
1818
"""
1919

20-
__slots__ = (
20+
__api_properties__ = (
2121
"announced",
2222
"unavailable_after",
2323
)
24+
__slots__ = __api_properties__
2425

2526
def __init__(
2627
self,

hcloud/firewalls/domain.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class Firewall(BaseDomain, DomainIdentityMixin):
2929
Point in time when the image was created
3030
"""
3131

32-
__slots__ = ("id", "name", "labels", "rules", "applied_to", "created")
32+
__api_properties__ = ("id", "name", "labels", "rules", "applied_to", "created")
33+
__slots__ = __api_properties__
3334

3435
def __init__(
3536
self,
@@ -66,14 +67,15 @@ class FirewallRule(BaseDomain):
6667
Short description of the firewall rule
6768
"""
6869

69-
__slots__ = (
70+
__api_properties__ = (
7071
"direction",
7172
"port",
7273
"protocol",
7374
"source_ips",
7475
"destination_ips",
7576
"description",
7677
)
78+
__slots__ = __api_properties__
7779

7880
DIRECTION_IN = "in"
7981
"""Firewall Rule Direction In"""
@@ -138,7 +140,8 @@ class FirewallResource(BaseDomain):
138140
applied to.
139141
"""
140142

141-
__slots__ = ("type", "server", "label_selector", "applied_to_resources")
143+
__api_properties__ = ("type", "server", "label_selector", "applied_to_resources")
144+
__slots__ = __api_properties__
142145

143146
TYPE_SERVER = "server"
144147
"""Firewall Used By Type Server"""
@@ -177,7 +180,8 @@ class FirewallResourceAppliedToResources(BaseDomain):
177180
:param server: Server the Firewall is applied to
178181
"""
179182

180-
__slots__ = ("type", "server")
183+
__api_properties__ = ("type", "server")
184+
__slots__ = __api_properties__
181185

182186
def __init__(
183187
self,
@@ -207,7 +211,8 @@ class CreateFirewallResponse(BaseDomain):
207211
The Action which shows the progress of the Firewall Creation
208212
"""
209213

210-
__slots__ = ("firewall", "actions")
214+
__api_properties__ = ("firewall", "actions")
215+
__slots__ = __api_properties__
211216

212217
def __init__(
213218
self,

hcloud/floating_ips/domain.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class FloatingIP(BaseDomain, DomainIdentityMixin):
4242
Name of the Floating IP
4343
"""
4444

45-
__slots__ = (
45+
__api_properties__ = (
4646
"id",
4747
"type",
4848
"description",
@@ -56,6 +56,7 @@ class FloatingIP(BaseDomain, DomainIdentityMixin):
5656
"name",
5757
"created",
5858
)
59+
__slots__ = __api_properties__
5960

6061
def __init__(
6162
self,
@@ -95,7 +96,8 @@ class CreateFloatingIPResponse(BaseDomain):
9596
The Action which shows the progress of the Floating IP Creation
9697
"""
9798

98-
__slots__ = ("floating_ip", "action")
99+
__api_properties__ = ("floating_ip", "action")
100+
__slots__ = __api_properties__
99101

100102
def __init__(
101103
self,

hcloud/images/domain.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Image(BaseDomain, DomainIdentityMixin):
5151
User-defined labels (key-value pairs)
5252
"""
5353

54-
__slots__ = (
54+
__api_properties__ = (
5555
"id",
5656
"name",
5757
"type",
@@ -70,6 +70,7 @@ class Image(BaseDomain, DomainIdentityMixin):
7070
"created",
7171
"deprecated",
7272
)
73+
__slots__ = __api_properties__
7374

7475
# pylint: disable=too-many-locals
7576
def __init__(
@@ -120,7 +121,8 @@ class CreateImageResponse(BaseDomain):
120121
The Action which shows the progress of the Floating IP Creation
121122
"""
122123

123-
__slots__ = ("action", "image")
124+
__api_properties__ = ("action", "image")
125+
__slots__ = __api_properties__
124126

125127
def __init__(
126128
self,

hcloud/isos/domain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ class Iso(BaseDomain, DomainIdentityMixin):
2727
deprecated. If it has a value, it is considered deprecated.
2828
"""
2929

30-
__slots__ = (
30+
__api_properties__ = (
3131
"id",
3232
"name",
3333
"type",
3434
"architecture",
3535
"description",
3636
"deprecation",
3737
)
38+
__slots__ = __api_properties__
3839

3940
def __init__(
4041
self,

hcloud/load_balancer_types/domain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LoadBalancerType(BaseDomain, DomainIdentityMixin):
2525
2626
"""
2727

28-
__slots__ = (
28+
__api_properties__ = (
2929
"id",
3030
"name",
3131
"description",
@@ -35,6 +35,7 @@ class LoadBalancerType(BaseDomain, DomainIdentityMixin):
3535
"max_assigned_certificates",
3636
"prices",
3737
)
38+
__slots__ = __api_properties__
3839

3940
def __init__(
4041
self,

0 commit comments

Comments
 (0)