Skip to content

Commit a11dfa0

Browse files
committed
chore: few type hints for model
1 parent f830d9f commit a11dfa0

2 files changed

Lines changed: 29 additions & 25 deletions

File tree

juju/model.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from datetime import datetime, timedelta
2020
from functools import partial
2121
from pathlib import Path
22+
from typing import Any
2223

2324
import websockets
2425
import yaml
@@ -28,6 +29,7 @@
2829
from .bundle import BundleHandler, get_charm_series, is_local_charm
2930
from .charmhub import CharmHub
3031
from .client import client, connector
32+
from .client.connection import Connection
3133
from .client.overrides import Caveat, Macaroon
3234
from .constraints import parse as parse_constraints
3335
from .controller import ConnectedController, Controller
@@ -257,7 +259,9 @@ def get_entity(self, entity_type, entity_id, history_index=-1, connected=True):
257259
class ModelEntity:
258260
"""An object in the Model tree"""
259261

260-
def __init__(self, entity_id, model, history_index=-1, connected=True):
262+
entity_id: str
263+
264+
def __init__(self, entity_id: str, model: Model, history_index=-1, connected=True):
261265
"""Initialize a new entity
262266
263267
:param entity_id str: The unique id of the object in the model
@@ -279,7 +283,7 @@ def __init__(self, entity_id, model, history_index=-1, connected=True):
279283
def __repr__(self):
280284
return f'<{type(self).__name__} entity_id="{self.entity_id}">'
281285

282-
def __getattr__(self, name):
286+
def __getattr__(self, name: str) -> Any:
283287
"""Fetch object attributes from the underlying data dict held in the
284288
model.
285289
@@ -615,7 +619,7 @@ def is_connected(self):
615619
"""Reports whether the Model is currently connected."""
616620
return self._connector.is_connected()
617621

618-
def connection(self):
622+
def connection(self) -> Connection:
619623
"""Return the current Connection object. It raises an exception
620624
if the Model is disconnected
621625
"""
@@ -3227,16 +3231,16 @@ def make_archive(self, path):
32273231
zf.close()
32283232
return path
32293233

3230-
def _check_type(self, path):
3234+
def _check_type(self, path: str) -> str:
32313235
"""Check the path"""
3232-
s = os.stat(str(path))
3236+
s = os.stat(path)
32333237
if stat.S_ISDIR(s.st_mode) or stat.S_ISREG(s.st_mode):
32343238
return path
32353239
raise ValueError(
32363240
"Invalid Charm at %s %s" % (path, "Invalid file type for a charm")
32373241
)
32383242

3239-
def _check_link(self, path):
3243+
def _check_link(self, path: str) -> None:
32403244
link_path = os.readlink(path)
32413245
if link_path[0] == "/":
32423246
raise ValueError(
@@ -3249,7 +3253,9 @@ def _check_link(self, path):
32493253
"Invalid charm at %s %s" % (path, "Only internal symlinks are allowed")
32503254
)
32513255

3252-
def _write_symlink(self, zf, link_target, link_path):
3256+
def _write_symlink(
3257+
self, zf: zipfile.ZipFile, link_target: str, link_path: str
3258+
) -> None:
32533259
"""Package symlinks with appropriate zipfile metadata."""
32543260
info = zipfile.ZipInfo()
32553261
info.filename = link_path
@@ -3259,11 +3265,8 @@ def _write_symlink(self, zf, link_target, link_path):
32593265
info.external_attr = 2716663808
32603266
zf.writestr(info, link_target)
32613267

3262-
def _ignore(self, path):
3263-
if path == "build" or path.startswith("build/"):
3264-
return True
3265-
if path.startswith("."):
3266-
return True
3268+
def _ignore(self, path: str) -> bool:
3269+
return path == "build" or path.startswith("build/") or path.startswith(".")
32673270

32683271

32693272
class ModelInfo(ModelEntity):

juju/tag.py

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

45
# TODO: Tags should be a proper class, so that we can distinguish whether
56
# something is already a tag or not. For example, 'user-foo' is a valid
67
# username, but is ambiguous with the already-tagged username 'foo'.
78

89

9-
def _prefix(prefix, s):
10+
def _prefix(prefix: str, s: str) -> str:
1011
if s and not s.startswith(prefix):
1112
return f"{prefix}{s}"
1213
return s
1314

1415

15-
def untag(prefix, s):
16+
def untag(prefix: str, s: str) -> str:
1617
if s and s.startswith(prefix):
1718
return s[len(prefix) :]
1819
return s
1920

2021

21-
def cloud(cloud_name):
22+
def cloud(cloud_name: str) -> str:
2223
return _prefix("cloud-", cloud_name)
2324

2425

25-
def controller(controller_uuid):
26+
def controller(controller_uuid: str) -> str:
2627
return _prefix("controller-", controller_uuid)
2728

2829

29-
def credential(cloud, user, credential_name):
30+
def credential(cloud: str, user: str, credential_name: str) -> str:
3031
credential_string = f"{cloud}_{user}_{credential_name}"
3132
return _prefix("cloudcred-", credential_string)
3233

3334

34-
def model(model_uuid):
35+
def model(model_uuid: str) -> str:
3536
return _prefix("model-", model_uuid)
3637

3738

38-
def machine(machine_id):
39+
def machine(machine_id: str) -> str:
3940
return _prefix("machine-", machine_id)
4041

4142

42-
def user(username):
43+
def user(username: str) -> str:
4344
return _prefix("user-", username)
4445

4546

46-
def application(app_name):
47+
def application(app_name: str) -> str:
4748
return _prefix("application-", app_name)
4849

4950

50-
def storage(app_name):
51+
def storage(app_name: str) -> str:
5152
return _prefix("storage-", app_name)
5253

5354

54-
def unit(unit_name):
55+
def unit(unit_name: str) -> str:
5556
return _prefix("unit-", unit_name.replace("/", "-"))
5657

5758

58-
def action(action_uuid):
59+
def action(action_uuid: str) -> str:
5960
return _prefix("action-", action_uuid)
6061

6162

62-
def space(space_name):
63+
def space(space_name: str) -> str:
6364
return _prefix("space-", space_name)

0 commit comments

Comments
 (0)