Skip to content

Commit a0fac06

Browse files
committed
chore: pull types from wip PR for upcoming Model.wait_for_idle
1 parent ebf3c0d commit a0fac06

6 files changed

Lines changed: 79 additions & 33 deletions

File tree

juju/model.py

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from datetime import datetime, timedelta
2020
from functools import partial
2121
from pathlib import Path
22-
from typing import Any
22+
from typing import TYPE_CHECKING, Any, Literal, Mapping, overload
2323

2424
import websockets
2525
import yaml
@@ -58,6 +58,13 @@
5858
from .url import URL, Schema
5959
from .version import DEFAULT_ARCHITECTURE
6060

61+
if TYPE_CHECKING:
62+
from .application import Application
63+
from .machine import Machine
64+
from .relation import Relation
65+
from .remoteapplication import ApplicationOffer, RemoteApplication
66+
from .unit import Unit
67+
6168
log = logging.getLogger(__name__)
6269

6370

@@ -134,7 +141,35 @@ def __init__(self, model):
134141
self.model = model
135142
self.state = dict()
136143

137-
def _live_entity_map(self, entity_type):
144+
@overload
145+
def _live_entity_map(
146+
self, entity_type: Literal["application"]
147+
) -> dict[str, Application]: ...
148+
149+
@overload
150+
def _live_entity_map(
151+
self, entity_type: Literal["applicationOffer"]
152+
) -> dict[str, ApplicationOffer]: ...
153+
154+
@overload
155+
def _live_entity_map(
156+
self, entity_type: Literal["machine"]
157+
) -> dict[str, Machine]: ...
158+
159+
@overload
160+
def _live_entity_map(
161+
self, entity_type: Literal["relation"]
162+
) -> dict[str, Relation]: ...
163+
164+
@overload
165+
def _live_entity_map(
166+
self, entity_type: Literal["remoteApplication"]
167+
) -> dict[str, RemoteApplication]: ...
168+
169+
@overload
170+
def _live_entity_map(self, entity_type: Literal["unit"]) -> dict[str, Unit]: ...
171+
172+
def _live_entity_map(self, entity_type: str) -> Mapping[str, ModelEntity]:
138173
"""Return an id:Entity map of all the living entities of
139174
type ``entity_type``.
140175
@@ -146,51 +181,51 @@ def _live_entity_map(self, entity_type):
146181
}
147182

148183
@property
149-
def applications(self):
184+
def applications(self) -> dict[str, Application]:
150185
"""Return a map of application-name:Application for all applications
151186
currently in the model.
152187
153188
"""
154189
return self._live_entity_map("application")
155190

156191
@property
157-
def remote_applications(self):
192+
def remote_applications(self) -> dict[str, RemoteApplication]:
158193
"""Return a map of application-name:Application for all remote
159194
applications currently in the model.
160195
161196
"""
162197
return self._live_entity_map("remoteApplication")
163198

164199
@property
165-
def application_offers(self):
200+
def application_offers(self) -> dict[str, ApplicationOffer]:
166201
"""Return a map of application-name:Application for all applications
167202
offers currently in the model.
168203
"""
169204
return self._live_entity_map("applicationOffer")
170205

171206
@property
172-
def machines(self):
207+
def machines(self) -> dict[str, Machine]:
173208
"""Return a map of machine-id:Machine for all machines currently in
174209
the model.
175210
176211
"""
177212
return self._live_entity_map("machine")
178213

179214
@property
180-
def units(self):
215+
def units(self) -> dict[str, Unit]:
181216
"""Return a map of unit-id:Unit for all units currently in
182217
the model.
183218
184219
"""
185220
return self._live_entity_map("unit")
186221

187222
@property
188-
def subordinate_units(self):
223+
def subordinate_units(self) -> dict[str, Unit]:
189224
"""Return a map of unit-id:Unit for all subordinate units"""
190225
return {u_name: u for u_name, u in self.units.items() if u.is_subordinate}
191226

192227
@property
193-
def relations(self):
228+
def relations(self) -> dict[str, Relation]:
194229
"""Return a map of relation-id:Relation for all relations currently in
195230
the model.
196231
@@ -1110,54 +1145,54 @@ def tag(self):
11101145
return tag.model(self.uuid)
11111146

11121147
@property
1113-
def applications(self):
1148+
def applications(self) -> dict[str, Application]:
11141149
"""Return a map of application-name:Application for all applications
11151150
currently in the model.
11161151
11171152
"""
11181153
return self.state.applications
11191154

11201155
@property
1121-
def remote_applications(self):
1156+
def remote_applications(self) -> dict[str, RemoteApplication]:
11221157
"""Return a map of application-name:Application for all remote
11231158
applications currently in the model.
11241159
11251160
"""
11261161
return self.state.remote_applications
11271162

11281163
@property
1129-
def application_offers(self):
1164+
def application_offers(self) -> dict[str, ApplicationOffer]:
11301165
"""Return a map of application-name:Application for all applications
11311166
offers currently in the model.
11321167
"""
11331168
return self.state.application_offers
11341169

11351170
@property
1136-
def machines(self):
1171+
def machines(self) -> dict[str, Machine]:
11371172
"""Return a map of machine-id:Machine for all machines currently in
11381173
the model.
11391174
11401175
"""
11411176
return self.state.machines
11421177

11431178
@property
1144-
def units(self):
1179+
def units(self) -> dict[str, Unit]:
11451180
"""Return a map of unit-id:Unit for all units currently in
11461181
the model.
11471182
11481183
"""
11491184
return self.state.units
11501185

11511186
@property
1152-
def subordinate_units(self):
1187+
def subordinate_units(self) -> dict[str, Unit]:
11531188
"""Return a map of unit-id:Unit for all subordiante units currently in
11541189
the model.
11551190
11561191
"""
11571192
return self.state.subordinate_units
11581193

11591194
@property
1160-
def relations(self):
1195+
def relations(self) -> list[Relation]:
11611196
"""Return a list of all Relations currently in the model."""
11621197
return list(self.state.relations.values())
11631198

juju/status.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
3+
from __future__ import annotations
34

45
import logging
56
import warnings
@@ -8,26 +9,26 @@
89

910
log = logging.getLogger(__name__)
1011

11-
""" derive_status is used to determine the application status from a set of unit
12-
status values.
1312

14-
:param statues: list of known unit workload statues
15-
16-
"""
13+
def derive_status(statuses: list[str]):
14+
"""Derive status from a set.
1715
16+
derive_status is used to determine the application status from a set of unit
17+
status values.
1818
19-
def derive_status(statues):
19+
:param statuses: list of known unit workload statuses
20+
"""
2021
current = "unknown"
21-
for status in statues:
22-
if status in severities and severities[status] > severities[current]:
22+
for status in statuses:
23+
if status in severity_map and severity_map[status] > severity_map[current]:
2324
current = status
2425
return current
2526

2627

27-
""" severities holds status values with a severity measure.
28+
""" severity_map holds status values with a severity measure.
2829
Status values with higher severity are used in preference to others.
2930
"""
30-
severities = {
31+
severity_map = {
3132
"error": 100,
3233
"blocked": 90,
3334
"waiting": 80,

juju/unit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616

1717
class Unit(model.ModelEntity):
18+
name: str
19+
1820
@property
1921
def agent_status(self):
2022
"""Returns the current agent status string."""

juju/url.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
3+
from __future__ import annotations
34

45
from enum import Enum
56
from urllib.parse import urlparse
@@ -8,6 +9,8 @@
89

910

1011
class Schema(Enum):
12+
"""Charm URL schema kinds."""
13+
1114
LOCAL = "local"
1215
CHARM_STORE = "cs"
1316
CHARM_HUB = "ch"
@@ -20,18 +23,23 @@ def __str__(self):
2023

2124

2225
class URL:
26+
"""Private URL class for this library internals only."""
27+
28+
name: str
29+
2330
def __init__(
2431
self,
2532
schema,
2633
user=None,
27-
name=None,
34+
name: str | None = None,
2835
revision=None,
2936
series=None,
3037
architecture=None,
3138
):
3239
self.schema = schema
3340
self.user = user
34-
self.name = name
41+
# the parse method will set the correct value later
42+
self.name = name # type: ignore
3543
self.series = series
3644

3745
# 0 can be a valid revision, hence the more verbose check.
@@ -41,7 +49,7 @@ def __init__(
4149
self.architecture = architecture
4250

4351
@staticmethod
44-
def parse(s, default_store=Schema.CHARM_HUB):
52+
def parse(s: str, default_store=Schema.CHARM_HUB) -> URL:
4553
"""Parse parses the provided charm URL string into its respective
4654
structure.
4755
@@ -103,7 +111,7 @@ def __str__(self):
103111
return f"{self.schema!s}:{self.path()}"
104112

105113

106-
def parse_v1_url(schema, u, s):
114+
def parse_v1_url(schema, u, s) -> URL:
107115
c = URL(schema)
108116

109117
parts = u.path.split("/")
@@ -135,7 +143,7 @@ def parse_v1_url(schema, u, s):
135143
return c
136144

137145

138-
def parse_v2_url(u, s, default_store):
146+
def parse_v2_url(u, s, default_store) -> URL:
139147
if not u.scheme:
140148
c = URL(default_store)
141149
elif Schema.CHARM_HUB.matches(u.scheme):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies = [
2525
"macaroonbakery>=1.1,<2.0",
2626
"pyRFC3339>=1.0,<2.0",
2727
"pyyaml>=5.1.2",
28-
"websockets>=8.1,<14.0",
28+
"websockets>=13.0.1,<14.0",
2929
"paramiko>=2.4.0",
3030
"pyasn1>=0.4.4",
3131
"toposort>=1.5,<2",

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"macaroonbakery>=1.1,<2.0",
2424
"pyRFC3339>=1.0,<2.0",
2525
"pyyaml>=5.1.2",
26-
"websockets>=8.1,<14.0",
26+
"websockets>=13.0.1,<14.0",
2727
"paramiko>=2.4.0",
2828
"pyasn1>=0.4.4",
2929
"toposort>=1.5,<2",

0 commit comments

Comments
 (0)