Skip to content

Commit

Permalink
Merge pull request #688 from tremble/pylint/use-a-generator
Browse files Browse the repository at this point in the history
use a generator rather than list comprehension when using any()/all()

SUMMARY
pylint
See also https://www.python.org/dev/peps/pep-0289/#rationale
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
plugins/modules/aws_kms_info.py
plugins/modules/aws_ses_rule_set.py
plugins/modules/cloudwatchevent_rule.py
plugins/modules/elb_target_group.py
plugins/modules/iam.py
plugins/modules/iam_group.py
plugins/modules/iam_role.py
plugins/modules/iam_user.py
plugins/modules/rds_instance.py
ADDITIONAL INFORMATION

Reviewed-by: Felix Fontein <None>
Reviewed-by: Markus Bergholz <git@osuv.de>
Reviewed-by: None <None>
  • Loading branch information
ansible-zuul[bot] authored Aug 13, 2021
2 parents f577b66 + 87304dd commit d78b08c
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 20 deletions.
10 changes: 10 additions & 0 deletions changelogs/fragments/688-pylint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
minor_changes:
- aws_kms_info - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- aws_ses_rule_set - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- cloudwatchevent_rule - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- elb_target_group - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- iam - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- iam_group - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- iam_role - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- iam_user - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
- rds_instance - use a generator rather than list comprehension (https://github.com/ansible-collections/community.aws/pull/688).
2 changes: 1 addition & 1 deletion plugins/modules/aws_kms_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def key_matches_filters(key, filters):
if not filters:
return True
else:
return all([key_matches_filter(key, filtr) for filtr in filters.items()])
return all(key_matches_filter(key, filtr) for filtr in filters.items())


def get_key_details(connection, module, key_id, tokens=None):
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/aws_ses_rule_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def list_rule_sets(client, module):


def rule_set_in(name, rule_sets):
return any([s for s in rule_sets if s['Name'] == name])
return any(s for s in rule_sets if s['Name'] == name)


def ruleset_active(client, module, name):
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/cloudwatchevent_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,10 +388,10 @@ def _rule_matches_aws(self):

# The rule matches AWS only if all rule data fields are equal
# to their corresponding local value defined in the task
return all([
return all(
getattr(self.rule, field) == aws_rule_data.get(field, None)
for field in self.RULE_FIELDS
])
)

def _targets_to_put(self):
"""Returns a list of targets that need to be updated or added remotely"""
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/elb_target_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def create_or_update_target_group(connection, module):
"health_check_path", "health_check_protocol", "health_check_interval", "health_check_timeout",
"healthy_threshold_count", "unhealthy_threshold_count", "successful_response_codes"
]
health_options = any([module.params[health_option_key] is not None for health_option_key in health_option_keys])
health_options = any(module.params[health_option_key] is not None for health_option_key in health_option_keys)

# Set health check if anything set
if health_options:
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ def main():

if key_state:
key_state = key_state.lower()
if any([n in key_state for n in ['active', 'inactive']]) and not key_ids:
if any(n in key_state for n in ['active', 'inactive']) and not key_ids:
module.fail_json(changed=False, msg="At least one access key has to be defined in order"
" to use 'active' or 'inactive'")

Expand Down Expand Up @@ -735,7 +735,7 @@ def main():
if iam_type == 'user':
been_updated = False
user_groups = None
user_exists = any([n in [name, new_name] for n in orig_user_list])
user_exists = any(n in [name, new_name] for n in orig_user_list)
if user_exists:
current_path = iam.get_user(name).get_user_result.user['path']
if not new_path and current_path != path:
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/iam_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def compare_group_members(current_group_members, new_group_members):

def convert_friendly_names_to_arns(connection, module, policy_names):

if not any([not policy.startswith('arn:') for policy in policy_names if policy is not None]):
if not any(not policy.startswith('arn:') for policy in policy_names if policy is not None):
return policy_names
allpolicies = {}
paginator = connection.get_paginator('list_policies')
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/iam_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _list_policies(connection):


def convert_friendly_names_to_arns(connection, module, policy_names):
if not any([not policy.startswith('arn:') for policy in policy_names]):
if not any(not policy.startswith('arn:') for policy in policy_names):
return policy_names
allpolicies = {}
policies = _list_policies(connection)
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/iam_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def convert_friendly_names_to_arns(connection, module, policy_names):
# List comprehension that looks for any policy in the 'policy_names' list
# that does not begin with 'arn'. If there aren't any, short circuit.
# If there are, translate friendly name to the full arn
if not any([not policy.startswith('arn:') for policy in policy_names if policy is not None]):
if not any(not policy.startswith('arn:') for policy in policy_names if policy is not None):
return policy_names
allpolicies = {}
paginator = connection.get_paginator('list_policies')
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/rds_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def get_parameters(client, module, parameters, method_name):
parameters['TargetDBInstanceIdentifier'] = module.params['db_instance_identifier']

required_options = get_boto3_client_method_parameters(client, method_name, required=True)
if any([parameters.get(k) is None for k in required_options]):
if any(parameters.get(k) is None for k in required_options):
module.fail_json(msg='To {0} requires the parameters: {1}'.format(
get_rds_method_attribute(method_name, module).operation_description, required_options))
options = get_boto3_client_method_parameters(client, method_name)
Expand Down
9 changes: 0 additions & 9 deletions tests/sanity/ignore-2.12.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
plugins/modules/aws_kms_info.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/aws_ses_rule_set.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/cloudfront_info.py pylint:unnecessary-comprehension # (new test) Should be an easy fix, but testing is a challenge - test are broken and aliases require a wildcard cert in ACM
plugins/modules/cloudwatchevent_rule.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/elb_target_group.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/iam_group.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/iam.py pylint:use-a-generator # no tests and module is deprecated
plugins/modules/iam.py pylint:unnecessary-comprehension # no tests and module is deprecated
plugins/modules/iam_role.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/iam_user.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/rds_instance.py pylint:use-a-generator # (new test) Should be an easy fix but not worth blocking gating
plugins/modules/route53.py validate-modules:parameter-state-invalid-choice # route53_info needs improvements before we can deprecate this

0 comments on commit d78b08c

Please sign in to comment.