From bc85d19e2c62952f090829ad51ba598b3c207928 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Thu, 4 Aug 2022 13:44:55 +0200 Subject: [PATCH] Minor linting fixups --- plugins/module_utils/acm.py | 5 +++-- plugins/module_utils/botocore.py | 4 ++-- plugins/module_utils/iam.py | 2 +- plugins/module_utils/modules.py | 1 - .../ansible_aws_module/test_fail_json_aws.py | 20 +++++++++---------- .../test_minimal_versions.py | 14 ++++++------- .../test_require_at_least.py | 16 +++++++-------- tests/unit/module_utils/test_cloud.py | 2 -- tests/unit/module_utils/test_elbv2.py | 4 ++-- tests/unit/module_utils/test_retries.py | 2 +- 10 files changed, 34 insertions(+), 36 deletions(-) diff --git a/plugins/module_utils/acm.py b/plugins/module_utils/acm.py index 9e384115d5e..fbfec720cee 100644 --- a/plugins/module_utils/acm.py +++ b/plugins/module_utils/acm.py @@ -21,7 +21,8 @@ # on behalf of Telstra Corporation Limited # # Common functionality to be used by the modules: -# - acm +# - acm_certificate +# - acm_certificate_info from __future__ import (absolute_import, division, print_function) __metaclass__ = type @@ -213,7 +214,7 @@ def import_certificate(self, client, module, certificate, private_key, arn=None, module.debug("Attempting to delete the cert we just created, arn=%s" % arn) try: self.delete_certificate_with_backoff(client, arn) - except Exception as f: + except Exception: module.warn("Certificate %s exists, and is not tagged. So Ansible will not see it on the next run.") module.fail_json_aws(e, msg="Couldn't tag certificate %s, couldn't delete it either" % arn) module.fail_json_aws(e, msg="Couldn't tag certificate %s" % arn) diff --git a/plugins/module_utils/botocore.py b/plugins/module_utils/botocore.py index 33e79420848..791cac60333 100644 --- a/plugins/module_utils/botocore.py +++ b/plugins/module_utils/botocore.py @@ -72,7 +72,7 @@ def boto3_conn(module, conn_type=None, resource=None, region=None, endpoint=None except (botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError, botocore.exceptions.NoCredentialsError, botocore.exceptions.ConfigParseError) as e: module.fail_json(msg=to_native(e)) - except botocore.exceptions.NoRegionError as e: + except botocore.exceptions.NoRegionError: module.fail_json(msg="The %s module requires a region and none was found in configuration, " "environment variables or module parameters" % module._name) @@ -155,7 +155,7 @@ def get_aws_region(module, boto3=None): try: profile_name = module.params.get('profile') return botocore.session.Session(profile=profile_name).get_config_variable('region') - except botocore.exceptions.ProfileNotFound as e: + except botocore.exceptions.ProfileNotFound: return None diff --git a/plugins/module_utils/iam.py b/plugins/module_utils/iam.py index 760c15f8e65..6ebed23baca 100644 --- a/plugins/module_utils/iam.py +++ b/plugins/module_utils/iam.py @@ -47,7 +47,7 @@ def get_aws_account_info(module): except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError): try: iam_client = module.client('iam', retry_decorator=AWSRetry.jittered_backoff()) - arn, partition, service, reg, account_id, resource = iam_client.get_user(aws_retry=True)['User']['Arn'].split(':') + _arn, partition, _service, _reg, account_id, _resource = iam_client.get_user(aws_retry=True)['User']['Arn'].split(':') except is_boto3_error_code('AccessDenied') as e: try: except_msg = to_native(e.message) diff --git a/plugins/module_utils/modules.py b/plugins/module_utils/modules.py index 53555d7ae28..6c1cc4c86a1 100644 --- a/plugins/module_utils/modules.py +++ b/plugins/module_utils/modules.py @@ -124,7 +124,6 @@ def __init__(self, **kwargs): if not HAS_BOTO3: self._module.fail_json( msg=missing_required_lib('botocore or boto3')) - current_versions = self._gather_versions() if not self.botocore_at_least('1.21.0'): self.warn('botocore < 1.21.0 is not supported or tested.' ' Some features may not work.') diff --git a/tests/unit/module_utils/core/ansible_aws_module/test_fail_json_aws.py b/tests/unit/module_utils/core/ansible_aws_module/test_fail_json_aws.py index c7e53afca02..737d93fece6 100644 --- a/tests/unit/module_utils/core/ansible_aws_module/test_fail_json_aws.py +++ b/tests/unit/module_utils/core/ansible_aws_module/test_fail_json_aws.py @@ -61,7 +61,7 @@ def test_fail_client_minimal(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.EXAMPLE_MSG @@ -88,7 +88,7 @@ def test_fail_client_msg(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, msg=self.FAIL_MSG) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.FAIL_MSG + ": " + self.EXAMPLE_MSG @@ -115,7 +115,7 @@ def test_fail_client_positional_msg(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, self.FAIL_MSG) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.FAIL_MSG + ": " + self.EXAMPLE_MSG @@ -142,7 +142,7 @@ def test_fail_client_key(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, extra_key="Some Value") assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.EXAMPLE_MSG @@ -170,7 +170,7 @@ def test_fail_client_msg_and_key(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, extra_key="Some Value", msg=self.FAIL_MSG) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.FAIL_MSG + ": " + self.EXAMPLE_MSG @@ -198,7 +198,7 @@ def test_fail_botocore_minimal(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.DEFAULT_CORE_MSG @@ -225,7 +225,7 @@ def test_fail_botocore_msg(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, msg=self.FAIL_MSG) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.FAIL_MSG + ": " + self.DEFAULT_CORE_MSG @@ -253,7 +253,7 @@ def test_fail_botocore_positional_msg(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, self.FAIL_MSG) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.FAIL_MSG + ": " + self.DEFAULT_CORE_MSG @@ -280,7 +280,7 @@ def test_fail_botocore_key(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, extra_key="Some Value") assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.DEFAULT_CORE_MSG @@ -308,7 +308,7 @@ def test_fail_botocore_msg_and_key(self, monkeypatch, stdin, capfd): with pytest.raises(SystemExit) as ctx: module.fail_json_aws(e, extra_key="Some Value", msg=self.FAIL_MSG) assert ctx.value.code == 1 - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("msg") == self.FAIL_MSG + ": " + self.DEFAULT_CORE_MSG diff --git a/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py b/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py index 6dbf3e06411..7a07beafc4a 100644 --- a/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py +++ b/tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py @@ -36,10 +36,10 @@ def test_no_warn(self, monkeypatch, stdin, capfd): # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.exit_json() - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("exception") is None @@ -59,10 +59,10 @@ def test_no_check(self, monkeypatch, stdin, capfd): # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict(), check_boto3=False) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.exit_json() - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("exception") is None @@ -82,7 +82,7 @@ def test_warn_boto3(self, monkeypatch, stdin, capfd): # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.exit_json() out, err = capfd.readouterr() @@ -115,7 +115,7 @@ def test_warn_botocore(self, monkeypatch, stdin, capfd): # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.exit_json() out, err = capfd.readouterr() @@ -148,7 +148,7 @@ def test_warn_boto3_and_botocore(self, monkeypatch, stdin, capfd): # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.exit_json() out, err = capfd.readouterr() diff --git a/tests/unit/module_utils/core/ansible_aws_module/test_require_at_least.py b/tests/unit/module_utils/core/ansible_aws_module/test_require_at_least.py index c0e0e5b6c00..cd532cb7da3 100644 --- a/tests/unit/module_utils/core/ansible_aws_module/test_require_at_least.py +++ b/tests/unit/module_utils/core/ansible_aws_module/test_require_at_least.py @@ -85,11 +85,11 @@ def test_require_botocore_at_least(self, monkeypatch, stdin, desired_version, co # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.require_botocore_at_least(desired_version) module.exit_json() - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("exception") is None @@ -118,11 +118,11 @@ def test_require_boto3_at_least(self, monkeypatch, stdin, desired_version, compa # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.require_boto3_at_least(desired_version) module.exit_json() - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("exception") is None @@ -153,11 +153,11 @@ def test_require_botocore_at_least_with_reason(self, monkeypatch, stdin, desired # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.require_botocore_at_least(desired_version, reason=reason) module.exit_json() - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("exception") is None @@ -189,11 +189,11 @@ def test_require_boto3_at_least_with_reason(self, monkeypatch, stdin, desired_ve # Create a minimal module that we can call module = AnsibleAWSModule(argument_spec=dict()) - with pytest.raises(SystemExit) as e: + with pytest.raises(SystemExit): module.require_boto3_at_least(desired_version, reason=reason) module.exit_json() - out, err = capfd.readouterr() + out, _err = capfd.readouterr() return_val = json.loads(out) assert return_val.get("exception") is None diff --git a/tests/unit/module_utils/test_cloud.py b/tests/unit/module_utils/test_cloud.py index 1486da01f5f..9e96c0682ad 100644 --- a/tests/unit/module_utils/test_cloud.py +++ b/tests/unit/module_utils/test_cloud.py @@ -229,7 +229,6 @@ def _fail(): # the elapsed execution time should be closed to 2sec for _i in range(3): start = datetime.now() - raised = False with self.assertRaises(self.TestException): _fail() duration = (datetime.now() - start).seconds @@ -265,7 +264,6 @@ def _fail_exception(): duration = expection[2] start = datetime.now() - raised = False with self.assertRaises(Exception): decorator(function)() _duration = (datetime.now() - start).seconds diff --git a/tests/unit/module_utils/test_elbv2.py b/tests/unit/module_utils/test_elbv2.py index 7fe70b1b2a7..b38f529c97f 100644 --- a/tests/unit/module_utils/test_elbv2.py +++ b/tests/unit/module_utils/test_elbv2.py @@ -178,7 +178,7 @@ def test_get_elb_ip_address_type(self): # Test modify_ip_address_type idempotency def test_modify_ip_address_type_idempotency(self): # Run module - return_value = self.elbv2obj.modify_ip_address_type("ipv4") + self.elbv2obj.modify_ip_address_type("ipv4") # check that no method was called and this has been retrieved from elb attributes self.connection.set_ip_address_type.assert_not_called() # assert we got the expected value @@ -187,7 +187,7 @@ def test_modify_ip_address_type_idempotency(self): # Test modify_ip_address_type def test_modify_ip_address_type_update(self): # Run module - return_value = self.elbv2obj.modify_ip_address_type("dualstack") + self.elbv2obj.modify_ip_address_type("dualstack") # check that no method was called and this has been retrieved from elb attributes self.connection.set_ip_address_type.assert_called_once() # assert we got the expected value diff --git a/tests/unit/module_utils/test_retries.py b/tests/unit/module_utils/test_retries.py index 2655bcd0692..39ae40ec519 100644 --- a/tests/unit/module_utils/test_retries.py +++ b/tests/unit/module_utils/test_retries.py @@ -34,7 +34,7 @@ def test_no_failures(self): def no_failures(): self.counter += 1 - r = no_failures() + no_failures() self.assertEqual(self.counter, 1) def test_extend_boto3_failures(self):