diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0a4a7304..ed53ec5a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,16 +5,16 @@ ci: repos: - repo: https://github.com/psf/black - rev: 23.12.1 + rev: 24.8.0 hooks: - id: black - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.1.13 + rev: v0.5.7 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: - id: check-yaml - id: debug-statements diff --git a/docs/conf.py b/docs/conf.py index efe6cb9b..607b6456 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -4,6 +4,7 @@ http://sphinx-doc.org/config.html """ + import os import sys diff --git a/nailgun/__init__.py b/nailgun/__init__.py index d1ac40c5..a623cbf9 100644 --- a/nailgun/__init__.py +++ b/nailgun/__init__.py @@ -14,6 +14,7 @@ :doc:`/examples` before the documentation here. """ + from logging import basicConfig basicConfig() diff --git a/nailgun/client.py b/nailgun/client.py index 3bfae9f1..6224e429 100644 --- a/nailgun/client.py +++ b/nailgun/client.py @@ -15,6 +15,7 @@ http://docs.python-requests.org/en/latest/api/#main-interface """ + from json import dumps import logging from warnings import simplefilter diff --git a/nailgun/config.py b/nailgun/config.py index 757d048b..6586a835 100644 --- a/nailgun/config.py +++ b/nailgun/config.py @@ -7,6 +7,7 @@ presenting that information. """ + import json from os.path import isfile, join from threading import Lock diff --git a/nailgun/entities.py b/nailgun/entities.py index f3ce14fa..d8bf5dc8 100644 --- a/nailgun/entities.py +++ b/nailgun/entities.py @@ -20,6 +20,7 @@ workings of entity classes. """ + from datetime import datetime from functools import lru_cache import hashlib @@ -2785,7 +2786,7 @@ def read(self, entity=None, attrs=None, ignore=None, params=None): if self._server_config: entity._server_config = self._server_config result = super().read(entity, attrs, ignore, params) - if 'content_view_components' in attrs and attrs['content_view_components']: + if attrs.get('content_view_components'): result.content_view_component = [ ContentViewComponent( server_config=self._server_config, @@ -4667,7 +4668,7 @@ def read(self, entity=None, attrs=None, ignore=None, params=None): ) else: result.image = None - if 'interfaces' in attrs and attrs['interfaces']: + if attrs.get('interfaces'): result.interface = [ Interface( server_config=self._server_config, diff --git a/nailgun/entity_fields.py b/nailgun/entity_fields.py index 5276382c..d9b16e79 100644 --- a/nailgun/entity_fields.py +++ b/nailgun/entity_fields.py @@ -18,6 +18,7 @@ unpleasant to work with manually. """ + import random from fauxfactory import ( diff --git a/nailgun/entity_mixins.py b/nailgun/entity_mixins.py index c61a3a92..e00cae9c 100644 --- a/nailgun/entity_mixins.py +++ b/nailgun/entity_mixins.py @@ -1,4 +1,5 @@ """Defines a set of mixins that provide tools for interacting with entities.""" + import _thread as thread from collections.abc import Iterable import contextlib diff --git a/tests/test_client.py b/tests/test_client.py index cd5bb2b7..11bbc0ce 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,4 +1,5 @@ """Unit tests for :mod:`nailgun.client`.""" + import inspect from unittest import TestCase, mock diff --git a/tests/test_config.py b/tests/test_config.py index 457716a5..147d57c5 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,4 +1,5 @@ """Unit tests for :mod:`nailgun.config`.""" + import builtins import json from unittest import TestCase diff --git a/tests/test_entities.py b/tests/test_entities.py index 286e70a8..d2baf07e 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -1,4 +1,5 @@ """Tests for :mod:`nailgun.entities`.""" + from datetime import date, datetime from http.client import ACCEPTED, NO_CONTENT import inspect @@ -1281,8 +1282,9 @@ def test_ignore_arg_v1(self): ), ): with self.subTest(entity): - with mock.patch.object(EntityReadMixin, 'read') as read, mock.patch.object( - EntityReadMixin, 'read_json' + with ( + mock.patch.object(EntityReadMixin, 'read') as read, + mock.patch.object(EntityReadMixin, 'read_json'), ): with mock.patch.object( entities, @@ -1416,10 +1418,13 @@ def test_snapshot_ignore_arg(self): Assert that entity`s predefined values of ``ignore`` are always correctly passed on. """ - with mock.patch.object(EntityReadMixin, 'read') as read, mock.patch.object( - EntityReadMixin, - 'read_json', - return_value={'host': 3}, + with ( + mock.patch.object(EntityReadMixin, 'read') as read, + mock.patch.object( + EntityReadMixin, + 'read_json', + return_value={'host': 3}, + ), ): entities.Snapshot(self.cfg, id=2, host=3).read() # `call_args` is a two-tuple of (positional, keyword) args. @@ -1431,22 +1436,26 @@ def test_host_with_interface(self): Assert that host will have interfaces initialized and assigned correctly. """ - with mock.patch.object( - EntityReadMixin, - 'read', - return_value=entities.Host(self.cfg, id=2), - ), mock.patch.object( - EntityReadMixin, - 'read_json', - return_value={ - 'interfaces': [{'id': 2}, {'id': 3}], - 'parameters': None, - 'puppet_proxy': None, - }, - ), mock.patch.object( - entities, - '_feature_list', - return_value={'Puppet'}, + with ( + mock.patch.object( + EntityReadMixin, + 'read', + return_value=entities.Host(self.cfg, id=2), + ), + mock.patch.object( + EntityReadMixin, + 'read_json', + return_value={ + 'interfaces': [{'id': 2}, {'id': 3}], + 'parameters': None, + 'puppet_proxy': None, + }, + ), + mock.patch.object( + entities, + '_feature_list', + return_value={'Puppet'}, + ), ): host = entities.Host(self.cfg, id=2).read() self.assertTrue(hasattr(host, 'interface')) @@ -2401,14 +2410,17 @@ def test_content_upload_upload(self): """ filename = gen_string('alpha') filepath = os.path.join(gen_string('alpha'), filename) - with mock.patch.object( - entities.ContentUpload, - 'create', - ) as create, mock.patch.object( - entities.Repository, - 'import_uploads', - return_value={'status': 'success'}, - ) as import_uploads: + with ( + mock.patch.object( + entities.ContentUpload, + 'create', + ) as create, + mock.patch.object( + entities.Repository, + 'import_uploads', + return_value={'status': 'success'}, + ) as import_uploads, + ): mock_open = mock.mock_open(read_data=gen_string('alpha').encode('ascii')) with mock.patch(_BUILTIN_OPEN, mock_open, create=True): response = self.content_upload.upload(filepath, filename) @@ -2425,14 +2437,17 @@ def test_content_upload_no_filename(self): """ filename = gen_string('alpha') filepath = os.path.join(gen_string('alpha'), filename) - with mock.patch.object( - entities.ContentUpload, - 'create', - ) as create, mock.patch.object( - entities.Repository, - 'import_uploads', - return_value={'status': 'success'}, - ) as import_uploads: + with ( + mock.patch.object( + entities.ContentUpload, + 'create', + ) as create, + mock.patch.object( + entities.Repository, + 'import_uploads', + return_value={'status': 'success'}, + ) as import_uploads, + ): mock_open = mock.mock_open(read_data=gen_string('alpha').encode('ascii')) with mock.patch(_BUILTIN_OPEN, mock_open, create=True): response = self.content_upload.upload(filepath) @@ -3251,11 +3266,14 @@ def test_upload_content_v1(self): :meth:`tests.test_entities.GenericTestCase.test_generic`. """ kwargs = {'kwarg': gen_integer()} - with mock.patch.object(client, 'post') as post, mock.patch.object( - entities, - '_handle_response', - return_value={'status': 'success'}, - ) as handler: + with ( + mock.patch.object(client, 'post') as post, + mock.patch.object( + entities, + '_handle_response', + return_value={'status': 'success'}, + ) as handler, + ): response = self.repo.upload_content(**kwargs) self.assertEqual(post.call_count, 1) self.assertEqual(len(post.call_args[0]), 1) @@ -3270,11 +3288,15 @@ def test_upload_content_v2(self): the (mock) server fails to return a "success" status. """ kwargs = {'kwarg': gen_integer()} - with mock.patch.object(client, 'post') as post, mock.patch.object( - entities, - '_handle_response', - return_value={'status': 'failure'}, - ) as handler, self.assertRaises(entities.APIResponseError): + with ( + mock.patch.object(client, 'post') as post, + mock.patch.object( + entities, + '_handle_response', + return_value={'status': 'failure'}, + ) as handler, + self.assertRaises(entities.APIResponseError), + ): self.repo.upload_content(**kwargs) self.assertEqual(post.call_count, 1) self.assertEqual(len(post.call_args[0]), 1) @@ -3297,11 +3319,14 @@ def test_import_uploads_uploads(self): 'checksum': gen_string('numeric'), } ] - with mock.patch.object(client, 'put') as put, mock.patch.object( - entities, - '_handle_response', - return_value={'status': 'success'}, - ) as handler: + with ( + mock.patch.object(client, 'put') as put, + mock.patch.object( + entities, + '_handle_response', + return_value={'status': 'success'}, + ) as handler, + ): response = self.repo.import_uploads(uploads=uploads, **kwargs) self.assertEqual(put.call_count, 1) self.assertEqual(len(put.call_args[0]), 2) @@ -3318,11 +3343,14 @@ def test_import_uploads_upload_ids(self): """ kwargs = {'kwarg': gen_integer()} upload_ids = [gen_string('numeric')] - with mock.patch.object(client, 'put') as put, mock.patch.object( - entities, - '_handle_response', - return_value={'status': 'success'}, - ) as handler: + with ( + mock.patch.object(client, 'put') as put, + mock.patch.object( + entities, + '_handle_response', + return_value={'status': 'success'}, + ) as handler, + ): response = self.repo.import_uploads(upload_ids=upload_ids, **kwargs) self.assertEqual(put.call_count, 1) self.assertEqual(len(put.call_args[0]), 2) diff --git a/tests/test_entity_fields.py b/tests/test_entity_fields.py index 4a115b6e..0f541a5b 100644 --- a/tests/test_entity_fields.py +++ b/tests/test_entity_fields.py @@ -1,4 +1,5 @@ """Unit tests for :mod:`nailgun.entity_fields`.""" + import datetime from random import randint import socket diff --git a/tests/test_entity_mixins.py b/tests/test_entity_mixins.py index 2776c0f5..8417026a 100644 --- a/tests/test_entity_mixins.py +++ b/tests/test_entity_mixins.py @@ -1,4 +1,5 @@ """Tests for :mod:`nailgun.entity_mixins`.""" + import http.client as http_client from unittest import TestCase, mock @@ -631,9 +632,10 @@ def test_create_json_with_exception(self): response.json.return_value = return_value else: response.json.side_effect = JSONDecodeError("msg", "foo", 2) - with mock.patch.object( - self.entity, 'create_raw', return_value=response - ), self.assertRaises(HTTPError) as error: + with ( + mock.patch.object(self.entity, 'create_raw', return_value=response), + self.assertRaises(HTTPError) as error, + ): self.entity.create_json() self.assertEqual(response.raise_for_status.call_count, 1) self.assertEqual(response.json.call_count, 1) @@ -979,11 +981,14 @@ def test_delete_v1(self): """Check what happens if the server returns an error HTTP status code.""" response = mock.Mock() response.raise_for_status.side_effect = HTTPError('oh no!') - with mock.patch.object( - entity_mixins.EntityDeleteMixin, - 'delete_raw', - return_value=response, - ), self.assertRaises(HTTPError): + with ( + mock.patch.object( + entity_mixins.EntityDeleteMixin, + 'delete_raw', + return_value=response, + ), + self.assertRaises(HTTPError), + ): self.entity.delete() def test_delete_v2(self): @@ -991,11 +996,14 @@ def test_delete_v2(self): response = mock.Mock() response.status_code = http_client.ACCEPTED response.json.return_value = {'id': gen_integer()} - with mock.patch.object( - entity_mixins.EntityDeleteMixin, - 'delete_raw', - return_value=response, - ) as delete_raw, mock.patch.object(entity_mixins, '_poll_task') as poll_task: + with ( + mock.patch.object( + entity_mixins.EntityDeleteMixin, + 'delete_raw', + return_value=response, + ) as delete_raw, + mock.patch.object(entity_mixins, '_poll_task') as poll_task, + ): self.entity.delete() self.assertEqual(delete_raw.call_count, 1) self.assertEqual(poll_task.call_count, 1) @@ -1008,11 +1016,14 @@ def test_delete_v3(self): """Check what happens if the server returns an HTTP NO_CONTENT status.""" response = mock.Mock() response.status_code = http_client.NO_CONTENT - with mock.patch.object( - entity_mixins.EntityDeleteMixin, - 'delete_raw', - return_value=response, - ), mock.patch.object(entity_mixins, '_poll_task') as poll_task: + with ( + mock.patch.object( + entity_mixins.EntityDeleteMixin, + 'delete_raw', + return_value=response, + ), + mock.patch.object(entity_mixins, '_poll_task') as poll_task, + ): self.assertEqual(self.entity.delete(), None) self.assertEqual(poll_task.call_count, 0) @@ -1032,11 +1043,14 @@ def test_delete_v5(self): response = mock.Mock() response.status_code = http_client.OK response.content = '' - with mock.patch.object( - entity_mixins.EntityDeleteMixin, - 'delete_raw', - return_value=response, - ), mock.patch.object(entity_mixins, '_poll_task') as poll_task: + with ( + mock.patch.object( + entity_mixins.EntityDeleteMixin, + 'delete_raw', + return_value=response, + ), + mock.patch.object(entity_mixins, '_poll_task') as poll_task, + ): self.assertEqual(self.entity.delete(), None) self.assertEqual(poll_task.call_count, 0) @@ -1045,11 +1059,14 @@ def test_delete_v6(self): response = mock.Mock() response.status_code = http_client.OK response.content = ' ' - with mock.patch.object( - entity_mixins.EntityDeleteMixin, - 'delete_raw', - return_value=response, - ), mock.patch.object(entity_mixins, '_poll_task') as poll_task: + with ( + mock.patch.object( + entity_mixins.EntityDeleteMixin, + 'delete_raw', + return_value=response, + ), + mock.patch.object(entity_mixins, '_poll_task') as poll_task, + ): self.assertEqual(self.entity.delete(), None) self.assertEqual(poll_task.call_count, 0)