From ebf45260cefd466e8e95a9b68265302475c11c15 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sun, 9 Nov 2025 05:21:46 +1300 Subject: [PATCH] remove conditional code for old snakes (#11048) * remove conditional code for old snakes * remove conditional code for old snakes * reformat * add changelog frag --- changelogs/fragments/11048-py3-cond.yml | 3 +++ plugins/module_utils/datetime.py | 14 +++---------- plugins/modules/jenkins_node.py | 9 +-------- .../launchd/files/ansible_test_service.py | 20 +++++++------------ .../supervisorctl/files/sendProcessStdin.py | 10 +++------- tests/unit/plugins/callback/test_elastic.py | 5 ----- .../plugins/callback/test_opentelemetry.py | 9 --------- tests/unit/plugins/modules/gitlab.py | 20 ------------------- tests/unit/plugins/modules/test_dnsimple.py | 4 ---- .../plugins/modules/test_gitlab_deploy_key.py | 4 +--- .../unit/plugins/modules/test_gitlab_group.py | 4 +--- .../unit/plugins/modules/test_gitlab_hook.py | 4 +--- .../plugins/modules/test_gitlab_project.py | 4 +--- .../modules/test_gitlab_protected_branch.py | 7 +++---- .../plugins/modules/test_gitlab_runner.py | 4 +--- .../unit/plugins/modules/test_gitlab_user.py | 4 +--- tests/unit/plugins/modules/test_modprobe.py | 6 ------ 17 files changed, 26 insertions(+), 105 deletions(-) create mode 100644 changelogs/fragments/11048-py3-cond.yml diff --git a/changelogs/fragments/11048-py3-cond.yml b/changelogs/fragments/11048-py3-cond.yml new file mode 100644 index 0000000000..482448dd41 --- /dev/null +++ b/changelogs/fragments/11048-py3-cond.yml @@ -0,0 +1,3 @@ +minor_changes: + - datetime module utils - remove code for unsupported Python version (https://github.com/ansible-collections/community.general/pull/11048). + - jenkins_node - remove code for unsupported Python version (https://github.com/ansible-collections/community.general/pull/11048). diff --git a/plugins/module_utils/datetime.py b/plugins/module_utils/datetime.py index f11375f0eb..907ebc61ad 100644 --- a/plugins/module_utils/datetime.py +++ b/plugins/module_utils/datetime.py @@ -6,25 +6,17 @@ from __future__ import annotations import datetime as _datetime -import sys - - -_USE_TIMEZONE = sys.version_info >= (3, 6) def ensure_timezone_info(value): - if not _USE_TIMEZONE or value.tzinfo is not None: + if value.tzinfo is not None: return value return value.astimezone(_datetime.timezone.utc) def fromtimestamp(value): - if _USE_TIMEZONE: - return _datetime.fromtimestamp(value, tz=_datetime.timezone.utc) - return _datetime.utcfromtimestamp(value) + return _datetime.fromtimestamp(value, tz=_datetime.timezone.utc) def now(): - if _USE_TIMEZONE: - return _datetime.datetime.now(tz=_datetime.timezone.utc) - return _datetime.datetime.utcnow() + return _datetime.datetime.now(tz=_datetime.timezone.utc) diff --git a/plugins/modules/jenkins_node.py b/plugins/modules/jenkins_node.py index de16d6b0ef..d3a927bdad 100644 --- a/plugins/modules/jenkins_node.py +++ b/plugins/modules/jenkins_node.py @@ -145,7 +145,6 @@ configured: type: bool """ -import sys import traceback from xml.etree import ElementTree as et @@ -160,9 +159,6 @@ with deps.declare( import jenkins -IS_PYTHON_2 = sys.version_info[0] <= 2 - - class JenkinsNode: def __init__(self, module: AnsibleModule) -> None: self.module = module @@ -246,10 +242,7 @@ class JenkinsNode: configured = True if configured: - if IS_PYTHON_2: - data = et.tostring(root) - else: - data = et.tostring(root, encoding="unicode") + data = et.tostring(root, encoding="unicode") self.instance.reconfig_node(self.name, data) diff --git a/tests/integration/targets/launchd/files/ansible_test_service.py b/tests/integration/targets/launchd/files/ansible_test_service.py index c8835075c5..1cb0f4e76f 100644 --- a/tests/integration/targets/launchd/files/ansible_test_service.py +++ b/tests/integration/targets/launchd/files/ansible_test_service.py @@ -6,18 +6,12 @@ from __future__ import annotations import sys +import http.server +import socketserver + if __name__ == '__main__': - if sys.version_info[0] >= 3: - import http.server - import socketserver - PORT = int(sys.argv[1]) - Handler = http.server.SimpleHTTPRequestHandler - httpd = socketserver.TCPServer(("", PORT), Handler) - httpd.serve_forever() - else: - import mimetypes - mimetypes.init() - mimetypes.add_type('application/json', '.json') - import SimpleHTTPServer - SimpleHTTPServer.test() + PORT = int(sys.argv[1]) + Handler = http.server.SimpleHTTPRequestHandler + httpd = socketserver.TCPServer(("", PORT), Handler) + httpd.serve_forever() diff --git a/tests/integration/targets/supervisorctl/files/sendProcessStdin.py b/tests/integration/targets/supervisorctl/files/sendProcessStdin.py index d510a79029..f2fa4e8022 100644 --- a/tests/integration/targets/supervisorctl/files/sendProcessStdin.py +++ b/tests/integration/targets/supervisorctl/files/sendProcessStdin.py @@ -7,19 +7,15 @@ from __future__ import annotations import sys +from xmlrpc.client import ServerProxy +from urllib.parse import quote + proc = sys.argv[1] value = sys.argv[2] username = sys.argv[3] password = sys.argv[4] -if sys.version_info[0] == 2: - from xmlrpclib import ServerProxy - from urllib import quote -else: - from xmlrpc.client import ServerProxy - from urllib.parse import quote - if username: url = 'http://%s:%s@127.0.0.1:9001/RPC2' % (quote(username, safe=''), quote(password, safe='')) else: diff --git a/tests/unit/plugins/callback/test_elastic.py b/tests/unit/plugins/callback/test_elastic.py index d06781d44f..3ddff23af1 100644 --- a/tests/unit/plugins/callback/test_elastic.py +++ b/tests/unit/plugins/callback/test_elastic.py @@ -4,7 +4,6 @@ from __future__ import annotations -import sys import unittest from collections import OrderedDict from unittest.mock import patch, MagicMock, Mock @@ -13,14 +12,10 @@ from ansible.playbook.task import Task from ansible.executor.task_result import TaskResult from ansible_collections.community.general.plugins.callback.elastic import ElasticSource, TaskData -ELASTIC_MINIMUM_PYTHON_VERSION = (3, 6) - class TestOpentelemetry(unittest.TestCase): @patch("ansible_collections.community.general.plugins.callback.elastic.socket") def setUp(self, mock_socket): - if sys.version_info < ELASTIC_MINIMUM_PYTHON_VERSION: - self.skipTest(f"Python {'.'.join(map(str, ELASTIC_MINIMUM_PYTHON_VERSION))}+ is needed for Elastic") mock_socket.gethostname.return_value = "my-host" mock_socket.gethostbyname.return_value = "1.2.3.4" self.elastic = ElasticSource(display=None) diff --git a/tests/unit/plugins/callback/test_opentelemetry.py b/tests/unit/plugins/callback/test_opentelemetry.py index 11ba2fcb1f..044851cfa4 100644 --- a/tests/unit/plugins/callback/test_opentelemetry.py +++ b/tests/unit/plugins/callback/test_opentelemetry.py @@ -4,7 +4,6 @@ from __future__ import annotations -import sys import unittest from collections import OrderedDict from unittest.mock import patch, MagicMock, Mock @@ -13,18 +12,10 @@ from ansible.playbook.task import Task from ansible.executor.task_result import TaskResult from ansible_collections.community.general.plugins.callback.opentelemetry import OpenTelemetrySource, TaskData -OPENTELEMETRY_MINIMUM_PYTHON_VERSION = (3, 7) - class TestOpentelemetry(unittest.TestCase): @patch("ansible_collections.community.general.plugins.callback.opentelemetry.socket") def setUp(self, mock_socket): - # TODO: this python version validation won't be needed as long as the _time_ns call is mocked. - if sys.version_info < OPENTELEMETRY_MINIMUM_PYTHON_VERSION: - self.skipTest( - f"Python {'.'.join(map(str, OPENTELEMETRY_MINIMUM_PYTHON_VERSION))}+ is needed for OpenTelemetry" - ) - mock_socket.gethostname.return_value = "my-host" mock_socket.gethostbyname.return_value = "1.2.3.4" self.opentelemetry = OpenTelemetrySource(display=None) diff --git a/tests/unit/plugins/modules/gitlab.py b/tests/unit/plugins/modules/gitlab.py index 8e29664f11..3b80298926 100644 --- a/tests/unit/plugins/modules/gitlab.py +++ b/tests/unit/plugins/modules/gitlab.py @@ -4,7 +4,6 @@ from __future__ import annotations -import sys import unittest from httmock import response # noqa @@ -27,22 +26,11 @@ class FakeAnsibleModule: class GitlabModuleTestCase(unittest.TestCase): def setUp(self): - unitest_python_version_check_requirement(self) - self.mock_module = FakeAnsibleModule() self.gitlab_instance = gitlab.Gitlab("http://localhost", private_token="private_token", api_version=4) -# Python 2.7+ is needed for python-gitlab -GITLAB_MINIMUM_PYTHON_VERSION = (2, 7) - - -# Verify if the current Python version is higher than GITLAB_MINIMUM_PYTHON_VERSION -def python_version_match_requirement(): - return sys.version_info >= GITLAB_MINIMUM_PYTHON_VERSION - - def python_gitlab_module_version(): return gitlab.__version__ @@ -51,14 +39,6 @@ def python_gitlab_version_match_requirement(): return "2.3.0" -# Skip unittest test case if python version don't match requirement -def unitest_python_version_check_requirement(unittest_testcase): - if not python_version_match_requirement(): - unittest_testcase.skipTest( - f"Python {'.'.join(map(str, GITLAB_MINIMUM_PYTHON_VERSION))}+ is needed for python-gitlab" - ) - - """ USER API """ diff --git a/tests/unit/plugins/modules/test_dnsimple.py b/tests/unit/plugins/modules/test_dnsimple.py index ccfc85b1f6..6db2d2bf62 100644 --- a/tests/unit/plugins/modules/test_dnsimple.py +++ b/tests/unit/plugins/modules/test_dnsimple.py @@ -12,12 +12,8 @@ from ansible_collections.community.internal_test_tools.tests.unit.plugins.module ) from unittest.mock import patch import pytest -import sys dnsimple = pytest.importorskip("dnsimple") -mandatory_py_version = pytest.mark.skipif( - sys.version_info < (3, 6), reason="The dnsimple dependency requires python3.6 or higher" -) from dnsimple import DNSimpleException diff --git a/tests/unit/plugins/modules/test_gitlab_deploy_key.py b/tests/unit/plugins/modules/test_gitlab_deploy_key.py index bb90aaa1ac..7dd3823445 100644 --- a/tests/unit/plugins/modules/test_gitlab_deploy_key.py +++ b/tests/unit/plugins/modules/test_gitlab_deploy_key.py @@ -19,7 +19,6 @@ pytestmark = [] try: from .gitlab import ( GitlabModuleTestCase, - python_version_match_requirement, resp_get_project, resp_find_project_deploy_key, resp_create_project_deploy_key, @@ -27,8 +26,7 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import ProjectKey + from gitlab.v4.objects import ProjectKey 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 diff --git a/tests/unit/plugins/modules/test_gitlab_group.py b/tests/unit/plugins/modules/test_gitlab_group.py index 1d44cb5d44..71e81e0c77 100644 --- a/tests/unit/plugins/modules/test_gitlab_group.py +++ b/tests/unit/plugins/modules/test_gitlab_group.py @@ -19,7 +19,6 @@ pytestmark = [] try: from .gitlab import ( GitlabModuleTestCase, - python_version_match_requirement, resp_get_group, resp_get_missing_group, resp_create_group, @@ -29,8 +28,7 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import Group + from gitlab.v4.objects import Group 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 diff --git a/tests/unit/plugins/modules/test_gitlab_hook.py b/tests/unit/plugins/modules/test_gitlab_hook.py index 71743a51b1..7dc8055766 100644 --- a/tests/unit/plugins/modules/test_gitlab_hook.py +++ b/tests/unit/plugins/modules/test_gitlab_hook.py @@ -19,7 +19,6 @@ pytestmark = [] try: from .gitlab import ( GitlabModuleTestCase, - python_version_match_requirement, resp_get_project, resp_find_project_hook, resp_create_project_hook, @@ -27,8 +26,7 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import ProjectHook + from gitlab.v4.objects import ProjectHook 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 diff --git a/tests/unit/plugins/modules/test_gitlab_project.py b/tests/unit/plugins/modules/test_gitlab_project.py index fd7460d7e2..5298469bd1 100644 --- a/tests/unit/plugins/modules/test_gitlab_project.py +++ b/tests/unit/plugins/modules/test_gitlab_project.py @@ -19,7 +19,6 @@ pytestmark = [] try: from .gitlab import ( GitlabModuleTestCase, - python_version_match_requirement, resp_get_group, resp_get_project_by_name, resp_create_project, @@ -29,8 +28,7 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import Project + from gitlab.v4.objects import Project 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 diff --git a/tests/unit/plugins/modules/test_gitlab_protected_branch.py b/tests/unit/plugins/modules/test_gitlab_protected_branch.py index 39917d8d41..d42193cd25 100644 --- a/tests/unit/plugins/modules/test_gitlab_protected_branch.py +++ b/tests/unit/plugins/modules/test_gitlab_protected_branch.py @@ -21,7 +21,6 @@ pytestmark = [] try: from .gitlab import ( GitlabModuleTestCase, - python_version_match_requirement, python_gitlab_module_version, python_gitlab_version_match_requirement, resp_get_protected_branch, @@ -32,12 +31,12 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import Project # noqa: F401, pylint: disable=unused-import + from gitlab.v4.objects import Project # noqa: F401, pylint: disable=unused-import + gitlab_req_version = python_gitlab_version_match_requirement() gitlab_module_version = python_gitlab_module_version() if LooseVersion(gitlab_module_version) < LooseVersion(gitlab_req_version): - pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)")) + pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing (Wrong version)")) except ImportError: pytestmark.append(pytest.mark.skip("Could not load gitlab module required for testing")) diff --git a/tests/unit/plugins/modules/test_gitlab_runner.py b/tests/unit/plugins/modules/test_gitlab_runner.py index 37ce965bbc..b9197b24c6 100644 --- a/tests/unit/plugins/modules/test_gitlab_runner.py +++ b/tests/unit/plugins/modules/test_gitlab_runner.py @@ -22,7 +22,6 @@ try: from .gitlab import ( FakeAnsibleModule, GitlabModuleTestCase, - python_version_match_requirement, resp_find_runners_all, resp_find_runners_list, resp_find_project_runners, @@ -35,8 +34,7 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import Runner + from gitlab.v4.objects import Runner 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 diff --git a/tests/unit/plugins/modules/test_gitlab_user.py b/tests/unit/plugins/modules/test_gitlab_user.py index 25c8e04217..2dbae4edff 100644 --- a/tests/unit/plugins/modules/test_gitlab_user.py +++ b/tests/unit/plugins/modules/test_gitlab_user.py @@ -19,7 +19,6 @@ pytestmark = [] try: from .gitlab import ( GitlabModuleTestCase, - python_version_match_requirement, resp_find_user, resp_get_user, resp_get_user_keys, @@ -34,8 +33,7 @@ try: ) # GitLab module requirements - if python_version_match_requirement(): - from gitlab.v4.objects import User + from gitlab.v4.objects import User 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 diff --git a/tests/unit/plugins/modules/test_modprobe.py b/tests/unit/plugins/modules/test_modprobe.py index 2cd879c93a..6fcc6a525e 100644 --- a/tests/unit/plugins/modules/test_modprobe.py +++ b/tests/unit/plugins/modules/test_modprobe.py @@ -4,7 +4,6 @@ from __future__ import annotations -import sys from unittest.mock import patch, Mock, mock_open from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( ModuleTestCase, @@ -152,9 +151,6 @@ class TestUnloadModule(ModuleTestCase): class TestModuleIsLoadedPersistently(ModuleTestCase): def setUp(self): - if sys.version_info[0] == 3 and sys.version_info[1] < 7: - self.skipTest("open_mock doesn't support readline in earlier python versions") - super().setUp() self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") @@ -216,8 +212,6 @@ class TestModuleIsLoadedPersistently(ModuleTestCase): class TestPermanentParams(ModuleTestCase): def setUp(self): - if sys.version_info[0] == 3 and sys.version_info[1] < 7: - self.skipTest("open_mock doesn't support readline in earlier python versions") super().setUp() self.mock_get_bin_path = patch("ansible.module_utils.basic.AnsibleModule.get_bin_path")