From 81e2eefe6c5b606844eb76536091c9fe4b86d9e1 Mon Sep 17 00:00:00 2001 From: Edmilson Monteiro Rodrigues Neto Date: Thu, 16 Jan 2025 07:52:49 +0000 Subject: [PATCH] refacor: change monkeypatch to use local imports --- juju/__init__.py | 4 ---- juju/client/gocookies.py | 4 +++- juju/machine.py | 9 ++++----- juju/unit.py | 9 ++++----- juju/user.py | 5 +++-- tests/unit/test_gocookies.py | 7 +++---- tox.ini | 2 -- 7 files changed, 17 insertions(+), 23 deletions(-) diff --git a/juju/__init__.py b/juju/__init__.py index 742d5acee..9da2b17aa 100644 --- a/juju/__init__.py +++ b/juju/__init__.py @@ -1,7 +1,3 @@ # Copyright 2024 Canonical Ltd. # Licensed under the Apache V2, see LICENCE file for details. """Python Library for Juju.""" - -from backports.datetime_fromisoformat import MonkeyPatch - -MonkeyPatch.patch_fromisoformat() diff --git a/juju/client/gocookies.py b/juju/client/gocookies.py index 9997d47f1..f86d42984 100644 --- a/juju/client/gocookies.py +++ b/juju/client/gocookies.py @@ -6,6 +6,8 @@ import json import time +from backports.datetime_fromisoformat import datetime_fromisoformat + class GoCookieJar(cookiejar.FileCookieJar): """A CookieJar implementation that reads and writes cookies @@ -50,7 +52,7 @@ def go_to_py_cookie(go_cookie): """Convert a Go-style JSON-unmarshaled cookie into a Python cookie""" expires = None if go_cookie.get("Expires") is not None: - t = datetime.datetime.fromisoformat(go_cookie["Expires"]) + t = datetime_fromisoformat(go_cookie["Expires"]) expires = t.timestamp() return cookiejar.Cookie( version=0, diff --git a/juju/machine.py b/juju/machine.py index e2499f050..11c4ad8a7 100644 --- a/juju/machine.py +++ b/juju/machine.py @@ -1,12 +1,13 @@ # Copyright 2023 Canonical Ltd. # Licensed under the Apache V2, see LICENCE file for details. -import datetime import asyncio import ipaddress import logging import typing +from backports.datetime_fromisoformat import datetime_fromisoformat + from juju.utils import block_until, juju_ssh_key_paths from . import model, tag @@ -238,7 +239,7 @@ def agent_status(self): @property def agent_status_since(self): """Get the time when the `agent_status` was last updated.""" - return datetime.datetime.fromisoformat(self.safe_data["agent-status"]["since"]) + return datetime_fromisoformat(self.safe_data["agent-status"]["since"]) @property def agent_version(self): @@ -265,9 +266,7 @@ def status_message(self): @property def status_since(self): """Get the time when the `status` was last updated.""" - return datetime.datetime.fromisoformat( - self.safe_data["instance-status"]["since"] - ) + return datetime_fromisoformat(self.safe_data["instance-status"]["since"]) @property def dns_name(self): diff --git a/juju/unit.py b/juju/unit.py index e6e7d3f75..9679b9769 100644 --- a/juju/unit.py +++ b/juju/unit.py @@ -1,9 +1,10 @@ # Copyright 2023 Canonical Ltd. # Licensed under the Apache V2, see LICENCE file for details. -import datetime import logging +from backports.datetime_fromisoformat import datetime_fromisoformat + from juju.errors import JujuAPIError, JujuError from . import model, tag @@ -24,7 +25,7 @@ def agent_status(self): @property def agent_status_since(self): """Get the time when the `agent_status` was last updated.""" - return datetime.datetime.fromisoformat(self.safe_data["agent-status"]["since"]) + return datetime_fromisoformat(self.safe_data["agent-status"]["since"]) @property def is_subordinate(self): @@ -51,9 +52,7 @@ def workload_status(self): @property def workload_status_since(self): """Get the time when the `workload_status` was last updated.""" - return datetime.datetime.fromisoformat( - self.safe_data["workload-status"]["since"] - ) + return datetime_fromisoformat(self.safe_data["workload-status"]["since"]) @property def workload_status_message(self): diff --git a/juju/user.py b/juju/user.py index 1f2709325..204d02b1c 100644 --- a/juju/user.py +++ b/juju/user.py @@ -1,9 +1,10 @@ # Copyright 2023 Canonical Ltd. # Licensed under the Apache V2, see LICENCE file for details. -import datetime import logging +from backports.datetime_fromisoformat import datetime_fromisoformat + from . import errors, tag from .client import client @@ -30,7 +31,7 @@ def display_name(self): @property def last_connection(self): - return datetime.datetime.fromisoformat(self._user_info.last_connection) + return datetime_fromisoformat(self._user_info.last_connection) @property def access(self): diff --git a/tests/unit/test_gocookies.py b/tests/unit/test_gocookies.py index c6e554c38..44d1e7f35 100644 --- a/tests/unit/test_gocookies.py +++ b/tests/unit/test_gocookies.py @@ -2,13 +2,14 @@ # Licensed under the Apache V2, see LICENCE file for details. """Tests for the gocookies code.""" -import datetime import os import shutil import tempfile import unittest import urllib.request +from backports.datetime_fromisoformat import datetime_fromisoformat + from juju.client.gocookies import GoCookieJar # cookie_content holds the JSON contents of a Go-produced @@ -223,9 +224,7 @@ def test_expiry_time(self): ]""" jar = self.load_jar(content) got_expires = tuple(jar)[0].expires - want_expires = int( - datetime.datetime.fromisoformat("2345-11-15T18:16:08Z").timestamp() - ) + want_expires = int(datetime_fromisoformat("2345-11-15T18:16:08Z").timestamp()) self.assertEqual(got_expires, want_expires) def load_jar(self, content): diff --git a/tox.ini b/tox.ini index c9b8f9757..875a54eb5 100644 --- a/tox.ini +++ b/tox.ini @@ -52,8 +52,6 @@ commands = envdir = {toxworkdir}/py3 deps = backports-datetime-fromisoformat -allowlist_externals = - pytest commands = pytest {toxinidir}/tests/unit {posargs}