[lxc-devel] [pylxd/master] Small updates to use Python 3 features
d0ugal on Github
lxc-bot at linuxcontainers.org
Fri Dec 4 16:10:23 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 302 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201204/1ca4b14c/attachment.bin>
-------------- next part --------------
From 5a4156ec9759c56e6519464ee6615321975d9fa7 Mon Sep 17 00:00:00 2001
From: Dougal Matthews <dougal at dougalmatthews.com>
Date: Fri, 4 Dec 2020 16:04:38 +0000
Subject: [PATCH 1/2] Modernize the super() calls to make use of Python 3
features
---
pylxd/deprecated/exceptions.py | 2 +-
pylxd/deprecated/tests/__init__.py | 2 +-
pylxd/deprecated/tests/test_connection.py | 2 +-
pylxd/deprecated/tests/test_image.py | 2 +-
pylxd/exceptions.py | 4 ++--
pylxd/managers.py | 4 ++--
pylxd/models/_model.py | 8 ++++----
pylxd/models/cluster.py | 2 +-
pylxd/models/instance.py | 6 +++---
pylxd/models/network.py | 2 +-
pylxd/models/operation.py | 2 +-
pylxd/models/storage_pool.py | 18 +++++++++---------
pylxd/tests/models/test_instance.py | 6 +++---
pylxd/tests/models/test_model.py | 2 +-
14 files changed, 31 insertions(+), 31 deletions(-)
diff --git a/pylxd/deprecated/exceptions.py b/pylxd/deprecated/exceptions.py
index 55965790..cd6c8848 100644
--- a/pylxd/deprecated/exceptions.py
+++ b/pylxd/deprecated/exceptions.py
@@ -40,6 +40,6 @@ class ImageInvalidSize(PyLXDException):
class APIError(PyLXDException):
def __init__(self, error, status_code):
msg = "Error %s - %s." % (status_code, error)
- super(APIError, self).__init__(msg)
+ super().__init__(msg)
self.status_code = status_code
self.error = error
diff --git a/pylxd/deprecated/tests/__init__.py b/pylxd/deprecated/tests/__init__.py
index 2cd41518..12fca5ff 100644
--- a/pylxd/deprecated/tests/__init__.py
+++ b/pylxd/deprecated/tests/__init__.py
@@ -21,7 +21,7 @@
class LXDAPITestBase(unittest.TestCase):
def setUp(self):
- super(LXDAPITestBase, self).setUp()
+ super().setUp()
self.lxd = api.API()
diff --git a/pylxd/deprecated/tests/test_connection.py b/pylxd/deprecated/tests/test_connection.py
index 9665f389..d4027ec3 100644
--- a/pylxd/deprecated/tests/test_connection.py
+++ b/pylxd/deprecated/tests/test_connection.py
@@ -102,7 +102,7 @@ def __init__(self, status, data):
@mock.patch("pylxd.deprecated.connection.LXDConnection.get_connection")
class LXDConnectionTest(unittest.TestCase):
def setUp(self):
- super(LXDConnectionTest, self).setUp()
+ super().setUp()
self.conn = connection.LXDConnection()
@annotated_data(
diff --git a/pylxd/deprecated/tests/test_image.py b/pylxd/deprecated/tests/test_image.py
index 9131e01f..35763654 100644
--- a/pylxd/deprecated/tests/test_image.py
+++ b/pylxd/deprecated/tests/test_image.py
@@ -200,7 +200,7 @@ def test_image_export_fail(self, ms):
)
class LXDAPIImageInfoTest(unittest.TestCase):
def setUp(self):
- super(LXDAPIImageInfoTest, self).setUp()
+ super().setUp()
self.image = image.LXDImage()
info_list = (
diff --git a/pylxd/exceptions.py b/pylxd/exceptions.py
index 50e6aff0..8b6309b5 100644
--- a/pylxd/exceptions.py
+++ b/pylxd/exceptions.py
@@ -11,7 +11,7 @@ class LXDAPIException(Exception):
"""
def __init__(self, response):
- super(LXDAPIException, self).__init__()
+ super().__init__()
self.response = response
def __str__(self):
@@ -44,7 +44,7 @@ def __init__(self, name, *args, **kwargs):
:param name: the api_extension that was needed.
:type name: str
"""
- super(LXDAPIExtensionNotAvailable, self).__init__(
+ super().__init__(
"LXD API extension '{}' is not available".format(name), *args, **kwargs
)
diff --git a/pylxd/managers.py b/pylxd/managers.py
index 477d70df..f6256eac 100644
--- a/pylxd/managers.py
+++ b/pylxd/managers.py
@@ -22,7 +22,7 @@ def __init__(self, *args, **kwargs):
for name, method in methods:
func = functools.partial(method, *args, **kwargs)
setattr(self, name, func)
- return super(BaseManager, self).__init__()
+ return super().__init__()
class CertificateManager(BaseManager):
@@ -74,7 +74,7 @@ class ClusterManager(BaseManager):
manager_for = "pylxd.models.Cluster"
def __init__(self, client, *args, **kwargs):
- super(ClusterManager, self).__init__(client, *args, **kwargs)
+ super().__init__(client, *args, **kwargs)
self._client = client
self.members = ClusterMemberManager(client)
diff --git a/pylxd/models/_model.py b/pylxd/models/_model.py
index b605e1f9..13d5bb84 100644
--- a/pylxd/models/_model.py
+++ b/pylxd/models/_model.py
@@ -82,7 +82,7 @@ def __new__(cls, name, bases, attrs):
attrs["__slots__"] = slots
attrs["__attributes__"] = attributes
- return super(ModelType, cls).__new__(cls, name, bases, attrs)
+ return super().__new__(cls, name, bases, attrs)
# Global used to record which warnings have been issues already for unknown
@@ -140,11 +140,11 @@ def __init__(self, client, **kwargs):
def __getattribute__(self, name):
try:
- return super(Model, self).__getattribute__(name)
+ return super().__getattribute__(name)
except AttributeError:
if name in self.__attributes__:
self.sync()
- return super(Model, self).__getattribute__(name)
+ return super().__getattribute__(name)
else:
raise
@@ -156,7 +156,7 @@ def __setattr__(self, name, value):
if attribute.validator is not type(value):
value = attribute.validator(value)
self.__dirty__.add(name)
- return super(Model, self).__setattr__(name, value)
+ return super().__setattr__(name, value)
def __iter__(self):
for attr in self.__attributes__.keys():
diff --git a/pylxd/models/cluster.py b/pylxd/models/cluster.py
index afea7786..99fc8fa1 100644
--- a/pylxd/models/cluster.py
+++ b/pylxd/models/cluster.py
@@ -25,7 +25,7 @@ class Cluster(model.Model):
members = model.Manager()
def __init__(self, *args, **kwargs):
- super(Cluster, self).__init__(*args, **kwargs)
+ super().__init__(*args, **kwargs)
self.members = managers.ClusterMemberManager(self.client, self)
@property
diff --git a/pylxd/models/instance.py b/pylxd/models/instance.py
index c14ae4d4..800a3eed 100644
--- a/pylxd/models/instance.py
+++ b/pylxd/models/instance.py
@@ -329,7 +329,7 @@ def create(cls, client, config, wait=False, target=None):
return cls(client, name=config["name"])
def __init__(self, *args, **kwargs):
- super(Instance, self).__init__(*args, **kwargs)
+ super().__init__(*args, **kwargs)
self.snapshots = managers.SnapshotManager(self.client, self)
self.files = self.FilesManager(self)
@@ -688,7 +688,7 @@ def __init__(self, manager, *args, **kwargs):
self.finished = False
self.last_message_empty = False
self.buffer = []
- super(_CommandWebsocketClient, self).__init__(*args, **kwargs)
+ super().__init__(*args, **kwargs)
def handshake_ok(self):
self.manager.add(self)
@@ -744,7 +744,7 @@ class _StdinWebsocket(WebSocketBaseClient): # pragma: no cover
def __init__(self, url, payload=None, **kwargs):
self.encoding = kwargs.pop("encoding", None)
self.payload = payload
- super(_StdinWebsocket, self).__init__(url, **kwargs)
+ super().__init__(url, **kwargs)
def _smart_encode(self, msg):
if type(msg) == str and self.encoding:
diff --git a/pylxd/models/network.py b/pylxd/models/network.py
index 481d0d77..61b3a0c8 100644
--- a/pylxd/models/network.py
+++ b/pylxd/models/network.py
@@ -123,7 +123,7 @@ def rename(self, new_name):
def save(self, *args, **kwargs):
self.client.assert_has_api_extension("network")
- super(Network, self).save(*args, **kwargs)
+ super().save(*args, **kwargs)
@property
def api(self):
diff --git a/pylxd/models/operation.py b/pylxd/models/operation.py
index def60f83..9826f78c 100644
--- a/pylxd/models/operation.py
+++ b/pylxd/models/operation.py
@@ -68,7 +68,7 @@ def get(cls, client, operation_id):
return cls(_client=client, **response.json()["metadata"])
def __init__(self, **kwargs):
- super(Operation, self).__init__()
+ super().__init__()
for key, value in kwargs.items():
try:
setattr(self, key, value)
diff --git a/pylxd/models/storage_pool.py b/pylxd/models/storage_pool.py
index 7cfec312..3e0ec0da 100644
--- a/pylxd/models/storage_pool.py
+++ b/pylxd/models/storage_pool.py
@@ -37,7 +37,7 @@ class StoragePool(model.Model):
volumes = model.Manager()
def __init__(self, *args, **kwargs):
- super(StoragePool, self).__init__(*args, **kwargs)
+ super().__init__(*args, **kwargs)
self.resources = StorageResourcesManager(self)
self.volumes = StorageVolumeManager(self)
@@ -188,7 +188,7 @@ def save(self, wait=False):
can't be deleted.
"""
# Note this method exists so that it is documented via sphinx.
- super(StoragePool, self).save(wait=wait)
+ super().save(wait=wait)
def delete(self):
"""Delete the storage pool.
@@ -202,7 +202,7 @@ def delete(self):
can't be deleted.
"""
# Note this method exists so that it is documented via sphinx.
- super(StoragePool, self).delete()
+ super().delete()
def put(self, put_object, wait=False):
"""Put the storage pool.
@@ -226,7 +226,7 @@ def put(self, put_object, wait=False):
can't be modified.
"""
# Note this method exists so that it is documented via sphinx.
- super(StoragePool, self).put(put_object, wait)
+ super().put(put_object, wait)
def patch(self, patch_object, wait=False):
"""Patch the storage pool.
@@ -246,7 +246,7 @@ def patch(self, patch_object, wait=False):
can't be modified.
"""
# Note this method exists so that it is documented via sphinx.
- super(StoragePool, self).patch(patch_object, wait)
+ super().patch(patch_object, wait)
class StorageResourcesManager(managers.BaseManager):
@@ -547,7 +547,7 @@ def put(self, put_object, wait=False):
can't be modified.
"""
# Note this method exists so that it is documented via sphinx.
- super(StorageVolume, self).put(put_object, wait)
+ super().put(put_object, wait)
def patch(self, patch_object, wait=False):
"""Patch the storage volume.
@@ -569,7 +569,7 @@ def patch(self, patch_object, wait=False):
volume can't be modified.
"""
# Note this method exists so that it is documented via sphinx.
- super(StorageVolume, self).patch(patch_object, wait)
+ super().patch(patch_object, wait)
def save(self, wait=False):
"""Save the model using PUT back to the LXD server.
@@ -593,7 +593,7 @@ def save(self, wait=False):
volume can't be deleted.
"""
# Note this method exists so that it is documented via sphinx.
- super(StorageVolume, self).save(wait=wait)
+ super().save(wait=wait)
def delete(self):
"""Delete the storage pool.
@@ -609,4 +609,4 @@ def delete(self):
can't be deleted.
"""
# Note this method exists so that it is documented via sphinx.
- super(StorageVolume, self).delete()
+ super().delete()
diff --git a/pylxd/tests/models/test_instance.py b/pylxd/tests/models/test_instance.py
index c6fb380b..a67813e9 100644
--- a/pylxd/tests/models/test_instance.py
+++ b/pylxd/tests/models/test_instance.py
@@ -476,7 +476,7 @@ class TestInstanceSnapshots(testing.PyLXDTestCase):
"""Tests for pylxd.models.Instance.snapshots."""
def setUp(self):
- super(TestInstanceSnapshots, self).setUp()
+ super().setUp()
self.instance = models.Instance.get(self.client, "an-instance")
def test_get(self):
@@ -507,7 +507,7 @@ class TestSnapshot(testing.PyLXDTestCase):
"""Tests for pylxd.models.Snapshot."""
def setUp(self):
- super(TestSnapshot, self).setUp()
+ super().setUp()
self.instance = models.Instance.get(self.client, "an-instance")
def test_rename(self):
@@ -602,7 +602,7 @@ class TestFiles(testing.PyLXDTestCase):
"""Tests for pylxd.models.Instance.files."""
def setUp(self):
- super(TestFiles, self).setUp()
+ super().setUp()
self.instance = models.Instance.get(self.client, "an-instance")
def test_put_delete(self):
diff --git a/pylxd/tests/models/test_model.py b/pylxd/tests/models/test_model.py
index 3aa4ce3d..1b626132 100644
--- a/pylxd/tests/models/test_model.py
+++ b/pylxd/tests/models/test_model.py
@@ -37,7 +37,7 @@ class TestModel(testing.PyLXDTestCase):
"""Tests for pylxd.model.Model."""
def setUp(self):
- super(TestModel, self).setUp()
+ super().setUp()
self.add_rule(
{
From bb4441ac77787bb944f699f3a13089327787e5ee Mon Sep 17 00:00:00 2001
From: Dougal Matthews <dougal at dougalmatthews.com>
Date: Fri, 4 Dec 2020 16:06:30 +0000
Subject: [PATCH 2/2] Don't inherit from object, this is not required in Python
3
---
integration/busybox.py | 2 +-
migration/busybox.py | 2 +-
pylxd/client.py | 4 ++--
pylxd/deprecated/api.py | 2 +-
pylxd/deprecated/base.py | 2 +-
pylxd/deprecated/connection.py | 2 +-
pylxd/deprecated/tests/test_connection.py | 2 +-
pylxd/managers.py | 2 +-
pylxd/models/_model.py | 6 +++---
pylxd/models/instance.py | 4 ++--
pylxd/models/operation.py | 2 +-
11 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/integration/busybox.py b/integration/busybox.py
index 72bbca7e..af08e0e7 100644
--- a/integration/busybox.py
+++ b/integration/busybox.py
@@ -26,7 +26,7 @@ def find_on_path(command):
return False
-class Busybox(object):
+class Busybox:
workdir = None
def __init__(self):
diff --git a/migration/busybox.py b/migration/busybox.py
index 72bbca7e..af08e0e7 100644
--- a/migration/busybox.py
+++ b/migration/busybox.py
@@ -26,7 +26,7 @@ def find_on_path(command):
return False
-class Busybox(object):
+class Busybox:
workdir = None
def __init__(self):
diff --git a/pylxd/client.py b/pylxd/client.py
index 78c848ee..c632db11 100644
--- a/pylxd/client.py
+++ b/pylxd/client.py
@@ -60,7 +60,7 @@ class EventType(Enum):
Lifecycle = "lifecycle"
-class _APINode(object):
+class _APINode:
"""An api node object."""
def __init__(self, api_endpoint, cert=None, verify=True, timeout=None):
@@ -224,7 +224,7 @@ def received_message(self, message):
self.messages.append(json_message)
-class Client(object):
+class Client:
"""Client class for LXD REST API.
This client wraps all the functionality required to interact with
diff --git a/pylxd/deprecated/api.py b/pylxd/deprecated/api.py
index bc16e755..63fcd20b 100644
--- a/pylxd/deprecated/api.py
+++ b/pylxd/deprecated/api.py
@@ -25,7 +25,7 @@
)
-class API(object):
+class API:
def __init__(self, host=None, port=8443):
warnings.warn(
"pylxd.api.API is deprecated. Please use pylxd.Client.", DeprecationWarning
diff --git a/pylxd/deprecated/base.py b/pylxd/deprecated/base.py
index b046fac2..a5466eb0 100644
--- a/pylxd/deprecated/base.py
+++ b/pylxd/deprecated/base.py
@@ -16,6 +16,6 @@
from pylxd.deprecated import connection
-class LXDBase(object):
+class LXDBase:
def __init__(self, conn=None):
self.connection = conn or connection.LXDConnection()
diff --git a/pylxd/deprecated/connection.py b/pylxd/deprecated/connection.py
index 83e64204..d9ee24ff 100644
--- a/pylxd/deprecated/connection.py
+++ b/pylxd/deprecated/connection.py
@@ -125,7 +125,7 @@ def receive(self):
return message
-class LXDConnection(object):
+class LXDConnection:
def __init__(self, host=None, port=8443):
if host:
self.host = host
diff --git a/pylxd/deprecated/tests/test_connection.py b/pylxd/deprecated/tests/test_connection.py
index d4027ec3..63284ac5 100644
--- a/pylxd/deprecated/tests/test_connection.py
+++ b/pylxd/deprecated/tests/test_connection.py
@@ -92,7 +92,7 @@ def test_get_connection(self, mode, args, env, path, mc, ms):
ms.assert_called_once_with(args[0], len(args) == 2 and args[1] or 8443)
-class FakeResponse(object):
+class FakeResponse:
def __init__(self, status, data):
self.status = status
self.read = BytesIO(bytes(data, "utf-8")).read
diff --git a/pylxd/managers.py b/pylxd/managers.py
index f6256eac..896719ed 100644
--- a/pylxd/managers.py
+++ b/pylxd/managers.py
@@ -4,7 +4,7 @@
from contextlib import contextmanager
-class BaseManager(object):
+class BaseManager:
"""A BaseManager class for handling collection operations."""
@property
diff --git a/pylxd/models/_model.py b/pylxd/models/_model.py
index 13d5bb84..f05f4eae 100644
--- a/pylxd/models/_model.py
+++ b/pylxd/models/_model.py
@@ -19,7 +19,7 @@
MISSING = object()
-class Attribute(object):
+class Attribute:
"""A metadata class for model attributes."""
def __init__(self, validator=None, readonly=False, optional=False):
@@ -28,7 +28,7 @@ def __init__(self, validator=None, readonly=False, optional=False):
self.optional = optional
-class Manager(object):
+class Manager:
"""A manager declaration.
This class signals to the model that it will have a Manager
@@ -36,7 +36,7 @@ class Manager(object):
"""
-class Parent(object):
+class Parent:
"""A parent declaration.
Child managers must keep a reference to their parent.
diff --git a/pylxd/models/instance.py b/pylxd/models/instance.py
index 800a3eed..01ed452b 100644
--- a/pylxd/models/instance.py
+++ b/pylxd/models/instance.py
@@ -34,7 +34,7 @@
from pylxd.models.operation import Operation
-class InstanceState(object):
+class InstanceState:
"""A simple object for representing instance state."""
def __init__(self, **kwargs):
@@ -81,7 +81,7 @@ class Instance(model.Model):
def api(self):
return self.client.api[self._endpoint][self.name]
- class FilesManager(object):
+ class FilesManager:
"""A pseudo-manager for namespacing file operations."""
def __init__(self, instance):
diff --git a/pylxd/models/operation.py b/pylxd/models/operation.py
index 9826f78c..ea4d14b6 100644
--- a/pylxd/models/operation.py
+++ b/pylxd/models/operation.py
@@ -23,7 +23,7 @@
_seen_attribute_warnings = set()
-class Operation(object):
+class Operation:
"""An LXD operation.
If the LXD server sends attributes that this version of pylxd is unaware of
More information about the lxc-devel
mailing list