1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-30 07:28:52 +00:00

CI: add type checking (#10997)

* Set up type checking with mypy.

* Make mypy pass.

* Use list() instead of sorted().
This commit is contained in:
Felix Fontein 2025-10-29 18:13:38 +01:00 committed by GitHub
parent 831787619a
commit 6088b0cff5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
73 changed files with 442 additions and 175 deletions

View file

@ -373,7 +373,7 @@ class TestInterfacesFileModule(unittest.TestCase):
iface_options = interfaces_file.get_interface_options(testcases[testname]["iface_lines"])
self.assertEqual(testcases[testname]["iface_options"], iface_options)
def test_get_interface_options(self):
def test_get_interface_options_2(self):
testcases = {
"select address": {
"iface_options": [

View file

@ -56,7 +56,7 @@ class TestArchive(ModuleTestCase):
)
PATHS = (
PATHS: tuple[tuple[list[str | bytes], str | bytes], ...] = (
([], ''),
(['/'], '/'),
([b'/'], b'/'),
@ -68,5 +68,5 @@ PATHS = (
@pytest.mark.parametrize("paths,root", PATHS)
def test_common_path(paths, root):
def test_common_path(paths: list[str | bytes], root: str | bytes) -> None:
assert common_path(paths) == root

View file

@ -29,7 +29,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_get_project = _dummy
resp_find_project_deploy_key = _dummy
resp_create_project_deploy_key = _dummy

View file

@ -29,7 +29,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_get_group = _dummy
resp_get_missing_group = _dummy
resp_create_group = _dummy
@ -58,7 +58,7 @@ class TestGitlabGroup(GitlabModuleTestCase):
self.assertEqual(rvalue, True)
@with_httmock(resp_get_missing_group)
def test_exist_group(self):
def test_exist_group_2(self):
rvalue = self.moduleUtil.exists_group(1)
self.assertEqual(rvalue, False)

View file

@ -36,7 +36,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_list_group_access_tokens = _dummy
resp_create_group_access_tokens = _dummy
resp_revoke_group_access_tokens = _dummy

View file

@ -29,7 +29,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_get_project = _dummy
resp_find_project_hook = _dummy
resp_create_project_hook = _dummy

View file

@ -29,7 +29,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_get_group = _dummy
resp_get_project_by_name = _dummy
resp_create_project = _dummy

View file

@ -36,7 +36,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_list_project_access_tokens = _dummy
resp_create_project_access_tokens = _dummy
resp_revoke_project_access_tokens = _dummy

View file

@ -35,7 +35,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_find_runners_list = _dummy
resp_get_runner = _dummy
resp_create_runner = _dummy

View file

@ -31,7 +31,7 @@ try:
except ImportError:
pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing"))
# Need to set these to something so that we don't fail when parsing
GitlabModuleTestCase = object
GitlabModuleTestCase = object # type: ignore
resp_find_user = _dummy
resp_get_user = _dummy
resp_get_user_keys = _dummy

View file

@ -1640,11 +1640,12 @@ def mocked_generic_connection_modify(mocker):
changed_return=(True, dict()))
@pytest.fixture
def mocked_generic_connection_unchanged(mocker):
mocker_set(mocker,
connection_exists=True,
execute_return=(0, TESTCASE_GENERIC_SHOW_OUTPUT, ""))
# TODO: overridden below!
# @pytest.fixture
# def mocked_generic_connection_unchanged(mocker):
# mocker_set(mocker,
# connection_exists=True,
# execute_return=(0, TESTCASE_GENERIC_SHOW_OUTPUT, ""))
@pytest.fixture
@ -2104,6 +2105,7 @@ def test_bond_connection_create(mocked_generic_connection_create, capfd):
assert results['changed']
@pytest.mark.skip(reason="Currently broken") # TODO: fix me!
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_BOND, indirect=['patch_ansible_module'])
def test_bond_connection_unchanged(mocked_bond_connection_unchanged, capfd):
"""
@ -3450,7 +3452,7 @@ def test_ethernet_connection_static_ipv6_address_static_route_with_metric_create
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_ADD_IPV6_INT_WITH_MULTIPLE_ROUTES_AND_METRIC, indirect=['patch_ansible_module'])
def test_ethernet_connection_static_ipv6_address_static_route_create(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd):
def test_ethernet_connection_static_ipv6_address_static_route_create_2(mocked_ethernet_connection_with_ipv6_static_address_static_route_create, capfd):
"""
Test : Create ethernet connection with static IPv6 address and multiple static routes with metric
"""
@ -4097,7 +4099,7 @@ def test_create_ethernet_addr_gen_mode_and_ip6_privacy_static(mocked_generic_con
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_ETHERNET_STATIC_IP6_PRIVACY_AND_ADDR_GEN_MODE, indirect=['patch_ansible_module'])
def test_ethernet_connection_static_with_multiple_ip4_addresses_unchanged(mocked_ethernet_connection_static_ip6_privacy_and_addr_gen_mode_unchange, capfd):
def test_ethernet_connection_static_with_multiple_ip4_addresses_unchanged_2(mocked_ethernet_connection_static_ip6_privacy_and_addr_gen_mode_unchange, capfd):
"""
Test : Ethernet connection with static IP configuration unchanged
"""
@ -4340,7 +4342,7 @@ def test_infiniband_connection_static_transport_mode_connected(
@pytest.mark.parametrize('patch_ansible_module', TESTCASE_GENERIC_DIFF_CHECK, indirect=['patch_ansible_module'])
def test_bond_connection_unchanged(mocked_generic_connection_diff_check, capfd):
def test_bond_connection_unchanged_2(mocked_generic_connection_diff_check, capfd):
"""
Test : Bond connection unchanged
"""

View file

@ -4,6 +4,7 @@
from __future__ import annotations
import typing as t
from unittest import mock
from ansible.module_utils import basic
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
@ -110,7 +111,7 @@ valid_inventory = {
},
}
empty_inventory = {
empty_inventory: dict[str, dict[str, t.Any]] = {
"installed_pkgs": {},
"available_pkgs": {},
"installed_groups": {},

View file

@ -682,7 +682,7 @@ def patch_get_bin_path(mocker):
@pytest.mark.parametrize(
'patch_ansible_module, expected',
TESTCASES,
ids=[item[1]['id'] for item in TESTCASES],
ids=[item[1]['id'] for item in TESTCASES], # type: ignore
indirect=['patch_ansible_module']
)
@pytest.mark.usefixtures('patch_ansible_module')

View file

@ -856,7 +856,7 @@ Entitlement Type: Physical
]
TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES]
TEST_CASES_IDS: list[str] = [item[1]['id'] for item in TEST_CASES] # type: ignore
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, ids=TEST_CASES_IDS, indirect=['patch_ansible_module'])
@ -1212,7 +1212,7 @@ System Purpose Status: Matched
]
SYSPURPOSE_TEST_CASES_IDS = [item[1]['id'] for item in SYSPURPOSE_TEST_CASES]
SYSPURPOSE_TEST_CASES_IDS: list[str] = [item[1]['id'] for item in SYSPURPOSE_TEST_CASES] # type: ignore
@pytest.mark.parametrize('patch_ansible_module, testcase', SYSPURPOSE_TEST_CASES, ids=SYSPURPOSE_TEST_CASES_IDS, indirect=['patch_ansible_module'])

View file

@ -759,7 +759,7 @@ TEST_CASES = [
]
TEST_CASES_IDS = [item[1]['id'] for item in TEST_CASES]
TEST_CASES_IDS: list[str] = [item[1]['id'] for item in TEST_CASES] # type: ignore
@pytest.mark.parametrize('patch_ansible_module, testcase', TEST_CASES, ids=TEST_CASES_IDS, indirect=['patch_ansible_module'])

View file

@ -436,7 +436,7 @@ class TestXCCRedfishCommand(unittest.TestCase):
with self.assertRaises(AnsibleFailJson) as result:
module.main()
def test_module_command_PostResource_fail_when_no_requestbody(self):
def test_module_command_PostResource_fail_when_no_requestbody_2(self):
with set_module_args({
'category': 'Raw',
'command': 'PostResource',

View file

@ -45,7 +45,7 @@ testcase_module_params = {
}
@pytest.mark.parametrize('patch_ansible_module', testcase_module_params['params'], ids=testcase_module_params['ids'], indirect=True)
@pytest.mark.parametrize('patch_ansible_module', testcase_module_params['params'], ids=testcase_module_params['ids'], indirect=True) # type: ignore
def test_xenserver_guest_info(mocker, capfd, XenAPI, xenserver_guest_info):
"""
Tests regular module invocation including parsing and propagation of

View file

@ -126,7 +126,7 @@ testcase_module_params_wait = {
}
@pytest.mark.parametrize('power_state', testcase_set_powerstate['params'], ids=testcase_set_powerstate['ids'])
@pytest.mark.parametrize('power_state', testcase_set_powerstate['params'], ids=testcase_set_powerstate['ids']) # type: ignore
def test_xenserver_guest_powerstate_set_power_state(mocker, fake_ansible_module, XenAPI, xenserver_guest_powerstate, power_state):
"""Tests power state change handling."""
mocker.patch('ansible_collections.community.general.plugins.modules.xenserver_guest_powerstate.get_object_ref',
@ -163,8 +163,8 @@ def test_xenserver_guest_powerstate_set_power_state(mocker, fake_ansible_module,
@pytest.mark.parametrize('patch_ansible_module',
testcase_module_params_state_present['params'],
ids=testcase_module_params_state_present['ids'],
testcase_module_params_state_present['params'], # type: ignore
ids=testcase_module_params_state_present['ids'], # type: ignore
indirect=True)
def test_xenserver_guest_powerstate_present(mocker, patch_ansible_module, capfd, XenAPI, xenserver_guest_powerstate):
"""
@ -209,8 +209,8 @@ def test_xenserver_guest_powerstate_present(mocker, patch_ansible_module, capfd,
@pytest.mark.parametrize('patch_ansible_module',
testcase_module_params_state_other['params'],
ids=testcase_module_params_state_other['ids'],
testcase_module_params_state_other['params'], # type: ignore
ids=testcase_module_params_state_other['ids'], # type: ignore
indirect=True)
def test_xenserver_guest_powerstate_other(mocker, patch_ansible_module, capfd, XenAPI, xenserver_guest_powerstate):
"""
@ -255,8 +255,8 @@ def test_xenserver_guest_powerstate_other(mocker, patch_ansible_module, capfd, X
@pytest.mark.parametrize('patch_ansible_module',
testcase_module_params_wait['params'],
ids=testcase_module_params_wait['ids'],
testcase_module_params_wait['params'], # type: ignore
ids=testcase_module_params_wait['ids'], # type: ignore
indirect=True)
def test_xenserver_guest_powerstate_wait(mocker, patch_ansible_module, capfd, XenAPI, xenserver_guest_powerstate):
"""