Skip to content

Commit ee9e19c

Browse files
refacor: change monkeypatch to use local imports
1 parent d2a7c17 commit ee9e19c

File tree

7 files changed

+17
-23
lines changed

7 files changed

+17
-23
lines changed

juju/__init__.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
# Copyright 2024 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
33
"""Python Library for Juju."""
4-
5-
from backports.datetime_fromisoformat import MonkeyPatch
6-
7-
MonkeyPatch.patch_fromisoformat()

juju/client/gocookies.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import json
77
import time
88

9+
from backports.datetime_fromisoformat import datetime_fromisoformat
10+
911

1012
class GoCookieJar(cookiejar.FileCookieJar):
1113
"""A CookieJar implementation that reads and writes cookies
@@ -50,7 +52,7 @@ def go_to_py_cookie(go_cookie):
5052
"""Convert a Go-style JSON-unmarshaled cookie into a Python cookie"""
5153
expires = None
5254
if go_cookie.get("Expires") is not None:
53-
t = datetime.datetime.fromisoformat(go_cookie["Expires"])
55+
t = datetime_fromisoformat(go_cookie["Expires"])
5456
expires = t.timestamp()
5557
return cookiejar.Cookie(
5658
version=0,

juju/machine.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
33

4-
import datetime
54
import asyncio
65
import ipaddress
76
import logging
87
import typing
98

9+
from backports.datetime_fromisoformat import datetime_fromisoformat
10+
1011
from juju.utils import block_until, juju_ssh_key_paths
1112

1213
from . import model, tag
@@ -238,7 +239,7 @@ def agent_status(self):
238239
@property
239240
def agent_status_since(self):
240241
"""Get the time when the `agent_status` was last updated."""
241-
return datetime.datetime.fromisoformat(self.safe_data["agent-status"]["since"])
242+
return datetime_fromisoformat(self.safe_data["agent-status"]["since"])
242243

243244
@property
244245
def agent_version(self):
@@ -265,9 +266,7 @@ def status_message(self):
265266
@property
266267
def status_since(self):
267268
"""Get the time when the `status` was last updated."""
268-
return datetime.datetime.fromisoformat(
269-
self.safe_data["instance-status"]["since"]
270-
)
269+
return datetime_fromisoformat(self.safe_data["instance-status"]["since"])
271270

272271
@property
273272
def dns_name(self):

juju/unit.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
33

4-
import datetime
54
import logging
65

6+
from backports.datetime_fromisoformat import datetime_fromisoformat
7+
78
from juju.errors import JujuAPIError, JujuError
89

910
from . import model, tag
@@ -24,7 +25,7 @@ def agent_status(self):
2425
@property
2526
def agent_status_since(self):
2627
"""Get the time when the `agent_status` was last updated."""
27-
return datetime.datetime.fromisoformat(self.safe_data["agent-status"]["since"])
28+
return datetime_fromisoformat(self.safe_data["agent-status"]["since"])
2829

2930
@property
3031
def is_subordinate(self):
@@ -51,9 +52,7 @@ def workload_status(self):
5152
@property
5253
def workload_status_since(self):
5354
"""Get the time when the `workload_status` was last updated."""
54-
return datetime.datetime.fromisoformat(
55-
self.safe_data["workload-status"]["since"]
56-
)
55+
return datetime_fromisoformat(self.safe_data["workload-status"]["since"])
5756

5857
@property
5958
def workload_status_message(self):

juju/user.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# Copyright 2023 Canonical Ltd.
22
# Licensed under the Apache V2, see LICENCE file for details.
33

4-
import datetime
54
import logging
65

6+
from backports.datetime_fromisoformat import datetime_fromisoformat
7+
78
from . import errors, tag
89
from .client import client
910

@@ -30,7 +31,7 @@ def display_name(self):
3031

3132
@property
3233
def last_connection(self):
33-
return datetime.datetime.fromisoformat(self._user_info.last_connection)
34+
return datetime_fromisoformat(self._user_info.last_connection)
3435

3536
@property
3637
def access(self):

tests/unit/test_gocookies.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# Licensed under the Apache V2, see LICENCE file for details.
33
"""Tests for the gocookies code."""
44

5-
import datetime
65
import os
76
import shutil
87
import tempfile
98
import unittest
109
import urllib.request
1110

11+
from backports.datetime_fromisoformat import datetime_fromisoformat
12+
1213
from juju.client.gocookies import GoCookieJar
1314

1415
# cookie_content holds the JSON contents of a Go-produced
@@ -223,9 +224,7 @@ def test_expiry_time(self):
223224
]"""
224225
jar = self.load_jar(content)
225226
got_expires = tuple(jar)[0].expires
226-
want_expires = int(
227-
datetime.datetime.fromisoformat("2345-11-15T18:16:08Z").timestamp()
228-
)
227+
want_expires = int(datetime_fromisoformat("2345-11-15T18:16:08Z").timestamp())
229228
self.assertEqual(got_expires, want_expires)
230229

231230
def load_jar(self, content):

tox.ini

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ commands =
5252
envdir = {toxworkdir}/py3
5353
deps =
5454
backports-datetime-fromisoformat
55-
allowlist_externals =
56-
pytest
5755
commands =
5856
pytest {toxinidir}/tests/unit {posargs}
5957

0 commit comments

Comments
 (0)