Skip to content

Commit c200bd0

Browse files
committed
Various pylint fixups (#2444)
SUMMARY fixes various linting warnings: redefined-builtin redefined-outer-name no-else-continue simplifiable-if-statement unused-import ISSUE TYPE Bugfix Pull Request COMPONENT NAME plugins/modules/ec2_ami.py plugins/modules/ec2_vpc_vpn.py plugins/modules/s3_bucket.py plugins/modules/s3_object.py tests/unit/ ADDITIONAL INFORMATION Also applies the "maybe_sleep" fixture to the ACM tests which have retries attached to them. Reviewed-by: Alina Buzachis
1 parent c5b0e34 commit c200bd0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+217
-367
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
minor_changes:
3+
- ec2_ami - avoid redefining ``delete_snapshot`` inside ``DeregisterImage.do`` (https://github.com/ansible-collections/amazon.aws/pull/2444).
4+
- s3_bucket - avoid redefining ``id`` inside ``handle_bucket_inventory`` and ``delete_bucket_inventory`` (https://github.com/ansible-collections/amazon.aws/pull/2444).
5+
- s3_object - avoid redefining ``key_check`` inside ``_head_object`` (https://github.com/ansible-collections/amazon.aws/pull/2444).
6+
- s3_object - simplify ``path_check`` logic (https://github.com/ansible-collections/amazon.aws/pull/2444).
7+
- ec2_vpc_vpn - minor linting fixups (https://github.com/ansible-collections/amazon.aws/pull/2444).

plugins/modules/ec2_ami.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,6 @@ def timeout(connection, image_id, wait_timeout):
646646
@classmethod
647647
def do(cls, module, connection, image_id):
648648
"""Entry point to deregister an image"""
649-
delete_snapshot = module.params.get("delete_snapshot")
650649
wait = module.params.get("wait")
651650
wait_timeout = module.params.get("wait_timeout")
652651
image = get_image_by_id(connection, image_id)
@@ -671,7 +670,7 @@ def do(cls, module, connection, image_id):
671670

672671
exit_params = {"msg": "AMI deregister operation complete.", "changed": True}
673672

674-
if delete_snapshot:
673+
if module.params.get("delete_snapshot"):
675674
exit_params["snapshots_deleted"] = list(purge_snapshots(connection))
676675

677676
module.exit_json(**exit_params)

plugins/modules/ec2_vpc_vpn.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,9 @@ def create_filter(module, provided_filters: Dict[str, Any]) -> List[Dict[str, An
565565
flat_filter_dict[param] = [str(provided_filters[param])]
566566

567567
# if customer_gateway, vpn_gateway, or vpn_connection was specified in the task but not the filter, add it
568-
for param in param_to_filter:
569-
if param_to_filter[param] not in flat_filter_dict and module.params.get(param):
570-
flat_filter_dict[param_to_filter[param]] = [module.params.get(param)]
568+
for param, param_value in param_to_filter.items():
569+
if param_value not in flat_filter_dict and module.params.get(param):
570+
flat_filter_dict[param_value] = [module.params.get(param)]
571571

572572
# change the flat dict into something boto3 will understand
573573
formatted_filter = [{"Name": key, "Values": value} for key, value in flat_filter_dict.items()]
@@ -710,7 +710,7 @@ def check_for_routes_update(client, module: AnsibleAWSModule, vpn_connection_id:
710710
for attribute in current_attrs:
711711
if attribute in ("tags", "routes", "state"):
712712
continue
713-
elif attribute == "options":
713+
if attribute == "options":
714714
will_be = module.params.get("static_only")
715715
is_now = bool(current_attrs[attribute]["static_routes_only"])
716716
attribute = "static_only"

plugins/modules/s3_bucket.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,9 +1209,9 @@ def handle_bucket_inventory(s3_client, module: AnsibleAWSModule, name: str) -> T
12091209

12101210
results.append(declared_inventory_api)
12111211

1212-
for id in present_inventories.keys():
1212+
for inventory_id in present_inventories.keys():
12131213
try:
1214-
delete_bucket_inventory(s3_client, name, id)
1214+
delete_bucket_inventory(s3_client, name, inventory_id)
12151215
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
12161216
module.fail_json_aws(e, msg="Failed to delete bucket inventory")
12171217
bucket_changed = True
@@ -1476,7 +1476,7 @@ def put_bucket_tagging(s3_client, bucket_name: str, tags: dict):
14761476

14771477

14781478
@AWSRetry.exponential_backoff(max_delay=120, catch_extra_error_codes=["NoSuchBucket", "OperationAborted"])
1479-
def delete_bucket_inventory(s3_client, bucket_name: str, id: str) -> None:
1479+
def delete_bucket_inventory(s3_client, bucket_name: str, inventory_id: str) -> None:
14801480
"""
14811481
Delete the inventory settings for an S3 bucket.
14821482
Parameters:
@@ -1486,7 +1486,7 @@ def delete_bucket_inventory(s3_client, bucket_name: str, id: str) -> None:
14861486
Returns:
14871487
None
14881488
"""
1489-
s3_client.delete_bucket_inventory_configuration(Bucket=bucket_name, Id=id)
1489+
s3_client.delete_bucket_inventory_configuration(Bucket=bucket_name, Id=inventory_id)
14901490

14911491

14921492
@AWSRetry.exponential_backoff(max_delay=120, catch_extra_error_codes=["NoSuchBucket", "OperationAborted"])

plugins/modules/s3_object.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -500,13 +500,13 @@ def etag_compare(module, s3, bucket, obj, version=None, local_file=None, content
500500
def _head_object(s3, bucket, obj, version=None):
501501
try:
502502
if version:
503-
key_check = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)
503+
obj_head = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj, VersionId=version)
504504
else:
505-
key_check = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj)
506-
if not key_check:
505+
obj_head = s3.head_object(aws_retry=True, Bucket=bucket, Key=obj)
506+
if not obj_head:
507507
return {}
508-
key_check.pop("ResponseMetadata")
509-
return key_check
508+
obj_head.pop("ResponseMetadata")
509+
return obj_head
510510
except is_boto3_error_code("404"):
511511
return {}
512512

@@ -656,10 +656,7 @@ def create_dirkey(module, s3, bucket, obj, encrypt, expiry):
656656

657657

658658
def path_check(path):
659-
if os.path.exists(path):
660-
return True
661-
else:
662-
return False
659+
return bool(os.path.exists(path))
663660

664661

665662
def guess_content_type(src):

tests/integration/targets/s3_object/library/test_s3_upload_multipart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def main():
9393
)
9494

9595
bucket = module.params.get("bucket")
96-
object = module.params.get("object")
96+
object_key = module.params.get("object")
9797
part_size = module.params.get("part_size")
9898
parts = module.params.get("parts")
9999

@@ -109,13 +109,13 @@ def main():
109109
module.fail_json_aws(e, msg="Failed to connect to AWS")
110110

111111
# create multipart upload
112-
response = s3.create_multipart_upload(Bucket=bucket, Key=object)
112+
response = s3.create_multipart_upload(Bucket=bucket, Key=object_key)
113113
upload_id = response.get("UploadId")
114114

115115
# upload parts
116116
upload_params = {
117117
"Bucket": bucket,
118-
"Key": object,
118+
"Key": object_key,
119119
"UploadId": upload_id,
120120
}
121121

@@ -124,7 +124,7 @@ def main():
124124
# complete the upload
125125
response = s3.complete_multipart_upload(
126126
Bucket=bucket,
127-
Key=object,
127+
Key=object_key,
128128
MultipartUpload={"Parts": multiparts},
129129
UploadId=upload_id,
130130
)

tests/unit/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file is part of Ansible
2+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
4+
# pylint: disable=unused-import
5+
6+
import pytest
7+
8+
from .utils.amazon_placebo_fixtures import fixture_maybe_sleep
9+
from .utils.amazon_placebo_fixtures import fixture_placeboify

tests/unit/module_utils/botocore/test_aws_region.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ class FailException(Exception):
2323
pass
2424

2525

26-
@pytest.fixture
27-
def aws_module(monkeypatch):
26+
@pytest.fixture(name="aws_module")
27+
def fixture_aws_module(monkeypatch):
2828
aws_module = MagicMock()
2929
aws_module.fail_json.side_effect = FailException()
3030
aws_module.fail_json_aws.side_effect = FailException()
3131
monkeypatch.setattr(aws_module, "params", sentinel.MODULE_PARAMS)
3232
return aws_module
3333

3434

35-
@pytest.fixture
36-
def fake_boto3(monkeypatch):
35+
@pytest.fixture(name="fake_boto3")
36+
def fixture_fake_boto3(monkeypatch):
3737
# Note: this isn't a monkey-patched real-botocore, this is a complete fake.
3838
fake_session = MagicMock()
3939
fake_session.region_name = sentinel.BOTO3_REGION
@@ -47,8 +47,8 @@ def fake_boto3(monkeypatch):
4747
return fake_boto3
4848

4949

50-
@pytest.fixture
51-
def botocore_utils(monkeypatch):
50+
@pytest.fixture(name="botocore_utils")
51+
def fixture_botocore_utils(monkeypatch):
5252
return utils_botocore
5353

5454

tests/unit/module_utils/botocore/test_boto3_conn.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class FailException(Exception):
2121
pass
2222

2323

24-
@pytest.fixture
25-
def aws_module(monkeypatch):
24+
@pytest.fixture(name="aws_module")
25+
def fixture_aws_module(monkeypatch):
2626
aws_module = MagicMock()
2727
aws_module.fail_json.side_effect = FailException()
2828
monkeypatch.setattr(aws_module, "_name", sentinel.MODULE_NAME)
2929
return aws_module
3030

3131

32-
@pytest.fixture
33-
def botocore_utils(monkeypatch):
32+
@pytest.fixture(name="botocore_utils")
33+
def fixture_botocore_utils(monkeypatch):
3434
return utils_botocore
3535

3636

tests/unit/module_utils/botocore/test_connection_info.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ class FailException(Exception):
3131
pass
3232

3333

34-
@pytest.fixture
35-
def aws_module(monkeypatch):
34+
@pytest.fixture(name="aws_module")
35+
def fixture_aws_module(monkeypatch):
3636
aws_module = MagicMock()
3737
aws_module.fail_json.side_effect = FailException()
3838
aws_module.fail_json_aws.side_effect = FailException()
3939
monkeypatch.setattr(aws_module, "params", sentinel.MODULE_PARAMS)
4040
return aws_module
4141

4242

43-
@pytest.fixture
44-
def fake_botocore(monkeypatch):
43+
@pytest.fixture(name="fake_botocore")
44+
def fixture_fake_botocore(monkeypatch):
4545
# Note: this isn't a monkey-patched real-botocore, this is a complete fake.
4646
fake_session = MagicMock()
4747
fake_session.get_config_variable.return_value = sentinel.BOTO3_REGION
@@ -58,8 +58,8 @@ def fake_botocore(monkeypatch):
5858
return fake_botocore
5959

6060

61-
@pytest.fixture
62-
def botocore_utils(monkeypatch):
61+
@pytest.fixture(name="botocore_utils")
62+
def fixture_botocore_utils(monkeypatch):
6363
region_method = MagicMock(name="_aws_region")
6464
monkeypatch.setattr(utils_botocore, "_aws_region", region_method)
6565
region_method.return_value = sentinel.RETURNED_REGION
@@ -204,7 +204,7 @@ def test_aws_connection_info_validation(monkeypatch, botocore_utils, options, ex
204204
assert region is sentinel.RETURNED_REGION
205205
assert endpoint_url is None
206206
assert boto_params == expected_params
207-
boto_params["verify"] is expected_validate
207+
assert boto_params["verify"] == expected_validate
208208

209209

210210
def test_aws_connection_info_profile(monkeypatch, botocore_utils):

0 commit comments

Comments
 (0)