From 8930d03c7ce455ccccfc115381596fb90c4fa04e Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Tue, 25 Nov 2025 21:59:06 +0100 Subject: [PATCH] [PR #11215/862fe79a backport][stable-12] fix ruff case SIM110 (#11217) fix ruff case SIM110 (#11215) * fix ruff case SIM110 * Update plugins/module_utils/xenserver.py * add changelog frag --------- (cherry picked from commit 862fe79a22c979d97d113f80fa8598c9f89b81ec) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Felix Fontein --- changelogs/fragments/11215-ruff-cases-8.yml | 13 +++++++++++++ plugins/module_utils/database.py | 6 +----- plugins/module_utils/hwc_utils.py | 12 ++---------- plugins/module_utils/known_hosts.py | 5 +---- plugins/module_utils/oracle/oci_utils.py | 10 ++-------- plugins/module_utils/utm_utils.py | 5 +---- plugins/module_utils/xenserver.py | 6 +----- plugins/modules/aix_filesystem.py | 6 +----- plugins/modules/composer.py | 9 ++++----- plugins/modules/flatpak.py | 5 +---- plugins/modules/gitlab_group_members.py | 5 +---- plugins/modules/gitlab_project_members.py | 5 +---- plugins/modules/lxd_container.py | 7 +++---- ruff.toml | 1 - tests/unit/plugins/modules/test_jenkins_node.py | 6 +----- tests/unit/plugins/modules/test_jenkins_plugin.py | 5 +---- 16 files changed, 34 insertions(+), 72 deletions(-) create mode 100644 changelogs/fragments/11215-ruff-cases-8.yml diff --git a/changelogs/fragments/11215-ruff-cases-8.yml b/changelogs/fragments/11215-ruff-cases-8.yml new file mode 100644 index 0000000000..878bf779f7 --- /dev/null +++ b/changelogs/fragments/11215-ruff-cases-8.yml @@ -0,0 +1,13 @@ +minor_changes: + - database module utils - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - hwc_utils module utils - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - known_hosts module utils - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - oci_utils module utils - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - utm_utils module utils - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - xenserver module utils - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - aix_filesystem - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - composer - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - flatpak - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - gitlab_group_members - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - gitlab_project_members - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). + - lxd_container - improve code by using native Python construct (https://github.com/ansible-collections/community.general/pull/11215). diff --git a/plugins/module_utils/database.py b/plugins/module_utils/database.py index 7ec20e18a4..0557ebe731 100644 --- a/plugins/module_utils/database.py +++ b/plugins/module_utils/database.py @@ -159,11 +159,7 @@ def is_input_dangerous(string): if not string: return False - for pattern in (PATTERN_1, PATTERN_2, PATTERN_3): - if re.search(pattern, string): - return True - - return False + return any(pattern.search(string) for pattern in (PATTERN_1, PATTERN_2, PATTERN_3)) def check_input(module, *args): diff --git a/plugins/module_utils/hwc_utils.py b/plugins/module_utils/hwc_utils.py index 926351655c..dfb9069d28 100644 --- a/plugins/module_utils/hwc_utils.py +++ b/plugins/module_utils/hwc_utils.py @@ -269,11 +269,7 @@ class _DictComparison: if set(dict1.keys()) != set(dict2.keys()): return False - for k in dict1: - if not self._compare_value(dict1.get(k), dict2.get(k)): - return False - - return True + return all(self._compare_value(dict1[k], dict2[k]) for k in dict1) def _compare_lists(self, list1, list2): """Takes in two lists and compares them.""" @@ -283,11 +279,7 @@ class _DictComparison: if len(list1) != len(list2): return False - for i in range(len(list1)): - if not self._compare_value(list1[i], list2[i]): - return False - - return True + return all(self._compare_value(l1, l2) for l1, l2 in zip(list1, list2)) def _compare_value(self, value1, value2): """ diff --git a/plugins/module_utils/known_hosts.py b/plugins/module_utils/known_hosts.py index 4996ee7ef1..7626fea03e 100644 --- a/plugins/module_utils/known_hosts.py +++ b/plugins/module_utils/known_hosts.py @@ -32,10 +32,7 @@ def is_ssh_url(url): if "@" in url and "://" not in url: return True - for scheme in "ssh://", "git+ssh://", "ssh+git://": - if url.startswith(scheme): - return True - return False + return any(url.startswith(scheme) for scheme in ("ssh://", "git+ssh://", "ssh+git://")) def get_fqdn_and_port(repo_url): diff --git a/plugins/module_utils/oracle/oci_utils.py b/plugins/module_utils/oracle/oci_utils.py index 37ffe885d7..2802a8d46a 100644 --- a/plugins/module_utils/oracle/oci_utils.py +++ b/plugins/module_utils/oracle/oci_utils.py @@ -507,10 +507,7 @@ def is_dictionary_subset(sub, super_dict): :param super_dict: super dictionary, for example resources_attr_value. :return: True if sub is contained in super. """ - for key in sub: - if sub[key] != super_dict[key]: - return False - return True + return all(sub[key] == super_dict[key] for key in sub) def are_lists_equal(s, t): @@ -533,10 +530,7 @@ def are_lists_equal(s, t): # `service_id` is provided by the user in the update call. sorted_s = sort_list_of_dictionary(s) sorted_t = sort_list_of_dictionary(t) - for index, d in enumerate(sorted_s): - if not is_dictionary_subset(d, sorted_t[index]): - return False - return True + return all(is_dictionary_subset(d, sorted_t[index]) for index, d in enumerate(sorted_s)) else: # Handle lists of primitive types. try: diff --git a/plugins/module_utils/utm_utils.py b/plugins/module_utils/utm_utils.py index 624c4f313b..b3c6810215 100644 --- a/plugins/module_utils/utm_utils.py +++ b/plugins/module_utils/utm_utils.py @@ -230,7 +230,4 @@ class UTM: :param result: The result from the query :return: """ - for key in keys: - if module.params.get(key) != result[key]: - return True - return False + return any(module.params.get(key) != result[key] for key in keys) diff --git a/plugins/module_utils/xenserver.py b/plugins/module_utils/xenserver.py index 53b32276d8..c51cef85db 100644 --- a/plugins/module_utils/xenserver.py +++ b/plugins/module_utils/xenserver.py @@ -219,11 +219,7 @@ def is_valid_ip6_addr(ip6_addr): ip6_addr_hextet_regex = re.compile("^[0-9a-f]{1,4}$") - for ip6_addr_hextet in ip6_addr_split: - if not bool(ip6_addr_hextet_regex.match(ip6_addr_hextet)): - return False - - return True + return all(ip6_addr_hextet_regex.match(ip6_addr_hextet) for ip6_addr_hextet in ip6_addr_split) def is_valid_ip6_prefix(ip6_prefix): diff --git a/plugins/modules/aix_filesystem.py b/plugins/modules/aix_filesystem.py index 2019e2bf83..5a4693a4c0 100644 --- a/plugins/modules/aix_filesystem.py +++ b/plugins/modules/aix_filesystem.py @@ -205,11 +205,7 @@ def _check_nfs_device(module, nfs_host, device): module.fail_json(msg=f"Failed to run showmount. Error message: {err}") else: showmount_data = showmount_out.splitlines() - for line in showmount_data: - if line.split(":")[1] == device: - return True - - return False + return any(line.split(":")[1] == device for line in showmount_data) def _validate_vg(module, vg): diff --git a/plugins/modules/composer.py b/plugins/modules/composer.py index bdb1bc983d..a9a7422e0c 100644 --- a/plugins/modules/composer.py +++ b/plugins/modules/composer.py @@ -150,11 +150,10 @@ def parse_out(string): def has_changed(string): - for no_change in ["Nothing to install or update", "Nothing to install, update or remove"]: - if no_change in string: - return False - - return True + return all( + no_change not in string + for no_change in ["Nothing to install or update", "Nothing to install, update or remove"] + ) def get_available_options(module, command="install"): diff --git a/plugins/modules/flatpak.py b/plugins/modules/flatpak.py index f1b5a71343..9fd96a9aed 100644 --- a/plugins/modules/flatpak.py +++ b/plugins/modules/flatpak.py @@ -313,10 +313,7 @@ def _is_flatpak_id(part): domain = sections[0] if not domain.islower(): return False - for section in sections[1:]: - if not section.isalnum(): - return False - return True + return all(section.isalnum() for section in sections[1:]) def _parse_flatpak_name(name): diff --git a/plugins/modules/gitlab_group_members.py b/plugins/modules/gitlab_group_members.py index 30454d5d63..b8cab67bbc 100644 --- a/plugins/modules/gitlab_group_members.py +++ b/plugins/modules/gitlab_group_members.py @@ -202,10 +202,7 @@ class GitLabGroup: # check if the user is a member of the group def is_user_a_member(self, members, gitlab_user_id): - for member in members: - if member.id == gitlab_user_id: - return True - return False + return any(member.id == gitlab_user_id for member in members) # add user to a group def add_member_to_group(self, gitlab_user_id, gitlab_group_id, access_level): diff --git a/plugins/modules/gitlab_project_members.py b/plugins/modules/gitlab_project_members.py index ce04dfc42f..b1d77e27af 100644 --- a/plugins/modules/gitlab_project_members.py +++ b/plugins/modules/gitlab_project_members.py @@ -205,10 +205,7 @@ class GitLabProjectMembers: # check if the user is a member of the project def is_user_a_member(self, members, gitlab_user_id): - for member in members: - if member.id == gitlab_user_id: - return True - return False + return any(member.id == gitlab_user_id for member in members) # add user to a project def add_member_to_project(self, gitlab_user_id, gitlab_project_id, access_level): diff --git a/plugins/modules/lxd_container.py b/plugins/modules/lxd_container.py index fea7771e47..9b884cecf4 100644 --- a/plugins/modules/lxd_container.py +++ b/plugins/modules/lxd_container.py @@ -682,10 +682,9 @@ class LXDContainerManagement: return self.config[key] != old_configs def _needs_to_apply_instance_configs(self): - for param in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS): - if self._needs_to_change_instance_config(param): - return True - return False + return any( + self._needs_to_change_instance_config(param) for param in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS) + ) def _apply_instance_configs(self): old_metadata = copy.deepcopy(self.old_instance_json).get("metadata", None) or {} diff --git a/ruff.toml b/ruff.toml index 50abc3d78c..cba7b0db7f 100644 --- a/ruff.toml +++ b/ruff.toml @@ -26,7 +26,6 @@ ignore = [ "UP041", # Replace aliased errors with `TimeoutError` "B026", # star-arg-unpacking-after-keyword-arg "SIM102", # collapsible-if - "SIM110", # reimplemented-builtin "SIM114", # if-with-same-arms "SIM115", # open-file-with-context-handler "SIM116", # if-else-block-instead-of-dict-lookup diff --git a/tests/unit/plugins/modules/test_jenkins_node.py b/tests/unit/plugins/modules/test_jenkins_node.py index ef5fb8cd87..5d8272aa98 100644 --- a/tests/unit/plugins/modules/test_jenkins_node.py +++ b/tests/unit/plugins/modules/test_jenkins_node.py @@ -45,11 +45,7 @@ def xml_equal(x, y): if len(x_children) != len(y_children): return False - for x, y in zip(x_children, y_children): - if not xml_equal(x, y): - return False - - return True + return all(xml_equal(x, y) for x, y in zip(x_children, y_children)) def assert_xml_equal(x, y): diff --git a/tests/unit/plugins/modules/test_jenkins_plugin.py b/tests/unit/plugins/modules/test_jenkins_plugin.py index 8d468785ee..38db65af7b 100644 --- a/tests/unit/plugins/modules/test_jenkins_plugin.py +++ b/tests/unit/plugins/modules/test_jenkins_plugin.py @@ -188,10 +188,7 @@ def test__new_fallback_urls(mocker): def isInList(l, i): print(f"checking if {i} in {l}") - for item in l: - if item == i: - return True - return False + return any(item == i for item in l) # TODO: check if return `i in l` works @patch("ansible_collections.community.general.plugins.modules.jenkins_plugin.fetch_url")