From e0bd7e334e49d5d0a0b7d969e5b38846138bf936 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 5 Jan 2026 18:57:50 +0100 Subject: [PATCH] [PR #11379/b3dc06a7 backport][stable-12] Clean up other Python files (#11382) Clean up other Python files (#11379) * Address issues found by ruff check. * Make mypy happy; remove some Python 2 compat code. * Also declare port1. (cherry picked from commit b3dc06a7dd81b3c5fd5bb8caee1630ad40efa452) Co-authored-by: Felix Fontein --- .azure-pipelines/scripts/combine-coverage.py | 10 +++++----- .azure-pipelines/scripts/publish-codecov.py | 7 +++---- .azure-pipelines/scripts/time-command.py | 2 +- .mypy.ini | 20 +++++++++++++++++++ docs/docsite/reformat-yaml.py | 2 +- noxfile.py | 4 ++-- .../targets/callback/filter_plugins/helper.py | 1 - .../action_plugins/_unsafe_assert.py | 2 +- .../targets/cmd_runner/library/cmd_echo.py | 3 +-- .../targets/flatpak/files/serve.py | 15 +++++--------- .../targets/java_cert/files/setupSSLServer.py | 8 ++------ .../targets/mail/files/smtpserver.py | 4 +++- .../targets/module_helper/library/mdepfail.py | 2 +- .../targets/monit/files/httpd_echo.py | 11 +++------- .../supervisorctl/files/sendProcessStdin.py | 2 +- tests/sanity/extra/botmeta.py | 6 +++--- 16 files changed, 52 insertions(+), 47 deletions(-) diff --git a/.azure-pipelines/scripts/combine-coverage.py b/.azure-pipelines/scripts/combine-coverage.py index bfc5d9cd89..bf1592a0b2 100755 --- a/.azure-pipelines/scripts/combine-coverage.py +++ b/.azure-pipelines/scripts/combine-coverage.py @@ -43,20 +43,20 @@ def main(): jobs[label] = max(attempt, jobs.get(label, 0)) for label, attempt in jobs.items(): - name = "Coverage {attempt} {label}".format(label=label, attempt=attempt) + name = f"Coverage {attempt} {label}" source = os.path.join(source_directory, name) source_files = os.listdir(source) for source_file in source_files: source_path = os.path.join(source, source_file) destination_path = os.path.join(destination_directory, source_file + "." + label) - print('"%s" -> "%s"' % (source_path, destination_path)) + print(f'"{source_path}" -> "{destination_path}"') shutil.copyfile(source_path, destination_path) count += 1 - print("Coverage file count: %d" % count) - print("##vso[task.setVariable variable=coverageFileCount]%d" % count) - print("##vso[task.setVariable variable=outputPath]%s" % output_path) + print(f"Coverage file count: {count}") + print(f"##vso[task.setVariable variable=coverageFileCount]{count}") + print(f"##vso[task.setVariable variable=outputPath]{output_path}") if __name__ == "__main__": diff --git a/.azure-pipelines/scripts/publish-codecov.py b/.azure-pipelines/scripts/publish-codecov.py index a7355d9695..8f2f7ea36c 100755 --- a/.azure-pipelines/scripts/publish-codecov.py +++ b/.azure-pipelines/scripts/publish-codecov.py @@ -15,7 +15,6 @@ import pathlib import shutil import subprocess import tempfile -import typing as t import urllib.request @@ -23,7 +22,7 @@ import urllib.request class CoverageFile: name: str path: pathlib.Path - flags: t.List[str] + flags: list[str] @dataclasses.dataclass(frozen=True) @@ -46,7 +45,7 @@ def parse_args() -> Args: return Args(**kwargs) -def process_files(directory: pathlib.Path) -> t.Tuple[CoverageFile, ...]: +def process_files(directory: pathlib.Path) -> tuple[CoverageFile, ...]: processed = [] for file in directory.joinpath("reports").glob("coverage*.xml"): name = file.stem.replace("coverage=", "") @@ -62,7 +61,7 @@ def process_files(directory: pathlib.Path) -> t.Tuple[CoverageFile, ...]: return tuple(processed) -def upload_files(codecov_bin: pathlib.Path, files: t.Tuple[CoverageFile, ...], dry_run: bool = False) -> None: +def upload_files(codecov_bin: pathlib.Path, files: tuple[CoverageFile, ...], dry_run: bool = False) -> None: for file in files: cmd = [ str(codecov_bin), diff --git a/.azure-pipelines/scripts/time-command.py b/.azure-pipelines/scripts/time-command.py index b99c68426c..faad7e386c 100755 --- a/.azure-pipelines/scripts/time-command.py +++ b/.azure-pipelines/scripts/time-command.py @@ -20,7 +20,7 @@ def main(): for line in sys.stdin: seconds = time.time() - start - sys.stdout.write("%02d:%02d %s" % (seconds // 60, seconds % 60, line)) + sys.stdout.write(f"{seconds // 60:02}:{seconds % 60:02} {line}") sys.stdout.flush() diff --git a/.mypy.ini b/.mypy.ini index 1ec80d77db..e3128f32ff 100644 --- a/.mypy.ini +++ b/.mypy.ini @@ -14,6 +14,8 @@ warn_redundant_casts = True # warn_return_any = True warn_unreachable = True +exclude = tests/integration/targets/django_.*/files/.* + [mypy-ansible.*] # ansible-core has partial typing information follow_untyped_imports = True @@ -24,6 +26,10 @@ follow_untyped_imports = True # 3. That have no types and type stubs. [mypy-aerospike.*] ignore_missing_imports = True +[mypy-antsibull_nox.*] +ignore_missing_imports = True +[mypy-asyncore.*] +ignore_missing_imports = True [mypy-boto3.*] ignore_missing_imports = True [mypy-bs4.*] @@ -38,6 +44,8 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-crypt.*] ignore_missing_imports = True +[mypy-daemon.*] +ignore_missing_imports = True [mypy-datadog.*] ignore_missing_imports = True [mypy-dbus.*] @@ -122,6 +130,10 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-nomad.*] ignore_missing_imports = True +[mypy-nopackagewiththisname.*] +ignore_missing_imports = True +[mypy-nox.*] +ignore_missing_imports = True [mypy-oci.*] ignore_missing_imports = True [mypy-oneandone.*] @@ -174,6 +186,8 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-rpm.*] ignore_missing_imports = True +[mypy-ruamel.yaml.*] +ignore_missing_imports = True [mypy-salt.*] ignore_missing_imports = True [mypy-selinux.*] @@ -186,6 +200,10 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-sha.*] ignore_missing_imports = True +[mypy-smtpd.*] +ignore_missing_imports = True +[mypy-smtpd_tls.*] +ignore_missing_imports = True [mypy-SoftLayer.*] ignore_missing_imports = True [mypy-spotinst_sdk.*] @@ -202,6 +220,8 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-vexatapi.*] ignore_missing_imports = True +[mypy-voluptuous.*] +ignore_missing_imports = True [mypy-websocket.*] ignore_missing_imports = True [mypy-XenAPI.*] diff --git a/docs/docsite/reformat-yaml.py b/docs/docsite/reformat-yaml.py index c7d83ce8a4..0667fa7418 100644 --- a/docs/docsite/reformat-yaml.py +++ b/docs/docsite/reformat-yaml.py @@ -6,7 +6,7 @@ import sys from io import StringIO -from ruamel.yaml import YAML +from ruamel.yaml import YAML # type: ignore[import-not-found] def main() -> None: diff --git a/noxfile.py b/noxfile.py index dc1899a407..2459ca5da8 100644 --- a/noxfile.py +++ b/noxfile.py @@ -9,14 +9,14 @@ import os import sys -import nox +import nox # type: ignore[import-not-found] # Whether the noxfile is running in CI: IN_CI = os.environ.get("CI") == "true" try: - import antsibull_nox + import antsibull_nox # type: ignore[import-not-found] except ImportError: print("You need to install antsibull-nox in the same Python environment as nox.") sys.exit(1) diff --git a/tests/integration/targets/callback/filter_plugins/helper.py b/tests/integration/targets/callback/filter_plugins/helper.py index 74a778c49a..dc7400dda4 100644 --- a/tests/integration/targets/callback/filter_plugins/helper.py +++ b/tests/integration/targets/callback/filter_plugins/helper.py @@ -12,7 +12,6 @@ def callback_results_extractor(outputs_results): expected_output = result["test"]["expected_output"] stdout_lines = result["stdout_lines"] for i in range(max(len(expected_output), len(stdout_lines))): - line = "line_%s" % (i + 1) test_line = stdout_lines[i] if i < len(stdout_lines) else None expected_lines = expected_output[i] if i < len(expected_output) else None if not isinstance(expected_lines, str) and expected_lines is not None: diff --git a/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py b/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py index e853e6f72e..1effb81346 100644 --- a/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py +++ b/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py @@ -9,7 +9,7 @@ from ansible.playbook.conditional import Conditional from ansible.plugins.action import ActionBase try: - from ansible.utils.datatag import trust_value as _trust_value + from ansible.utils.datatag import trust_value as _trust_value # type: ignore[import-not-found] except ImportError: def _trust_value(input): diff --git a/tests/integration/targets/cmd_runner/library/cmd_echo.py b/tests/integration/targets/cmd_runner/library/cmd_echo.py index 1b5968b3b7..93f2c27746 100644 --- a/tests/integration/targets/cmd_runner/library/cmd_echo.py +++ b/tests/integration/targets/cmd_runner/library/cmd_echo.py @@ -51,11 +51,10 @@ def main(): with runner.context(p["arg_order"], check_mode_skip=p["check_mode_skip"]) as ctx: result = ctx.run(**p["arg_values"]) info = ctx.run_info - check = "check" rc, out, err = result if result is not None else (None, None, None) module.exit_json(rc=rc, out=out, err=err, info=info) - except Exception as exc: + except Exception: module.fail_json(rc=1, module_stderr=traceback.format_exc(), msg="Module crashed with exception") diff --git a/tests/integration/targets/flatpak/files/serve.py b/tests/integration/targets/flatpak/files/serve.py index df5ad800c6..44cdc718cb 100644 --- a/tests/integration/targets/flatpak/files/serve.py +++ b/tests/integration/targets/flatpak/files/serve.py @@ -9,13 +9,8 @@ import os import posixpath import sys -try: - from http.server import SimpleHTTPRequestHandler, HTTPServer - from urllib.parse import unquote -except ImportError: - from SimpleHTTPServer import SimpleHTTPRequestHandler - from BaseHTTPServer import HTTPServer - from urllib import unquote +from http.server import SimpleHTTPRequestHandler, HTTPServer +from urllib.parse import unquote # Argument parsing @@ -23,8 +18,8 @@ if len(sys.argv) != 4: print(f"Syntax: {sys.argv[0]} ") sys.exit(-1) -HOST, PORT, PATH = sys.argv[1:4] -PORT = int(PORT) +HOST, PORT_str, PATH = sys.argv[1:4] +PORT = int(PORT_str) # The HTTP request handler @@ -40,7 +35,7 @@ class Handler(SimpleHTTPRequestHandler): trailing_slash = path.rstrip().endswith("/") try: path = unquote(path, errors="surrogatepass") - except (UnicodeDecodeError, TypeError) as exc: + except (UnicodeDecodeError, TypeError): path = unquote(path) path = posixpath.normpath(path) words = path.split("/") diff --git a/tests/integration/targets/java_cert/files/setupSSLServer.py b/tests/integration/targets/java_cert/files/setupSSLServer.py index 450fad0190..5e40a7e496 100644 --- a/tests/integration/targets/java_cert/files/setupSSLServer.py +++ b/tests/integration/targets/java_cert/files/setupSSLServer.py @@ -10,15 +10,11 @@ import sys root_dir = sys.argv[1] port = int(sys.argv[2]) -try: - from BaseHTTPServer import HTTPServer - from SimpleHTTPServer import SimpleHTTPRequestHandler -except ModuleNotFoundError: - from http.server import HTTPServer, SimpleHTTPRequestHandler +from http.server import HTTPServer, SimpleHTTPRequestHandler httpd = HTTPServer(("localhost", port), SimpleHTTPRequestHandler) try: - httpd.socket = ssl.wrap_socket( + httpd.socket = ssl.wrap_socket( # type: ignore[attr-defined] httpd.socket, server_side=True, certfile=os.path.join(root_dir, "cert.pem"), diff --git a/tests/integration/targets/mail/files/smtpserver.py b/tests/integration/targets/mail/files/smtpserver.py index 377b848092..91978267c8 100644 --- a/tests/integration/targets/mail/files/smtpserver.py +++ b/tests/integration/targets/mail/files/smtpserver.py @@ -27,6 +27,8 @@ port = "25:465" if len(sys.argv) > 1: port = sys.argv[1] ports = port.split(":") +port1: int +port2: int | None if len(ports) > 1: port1, port2 = int(ports[0]), int(ports[1]) else: @@ -58,6 +60,6 @@ else: print("Start SMTP server on port", port1) smtp_server1 = smtpd.DebuggingServer(("127.0.0.1", port1), None) # pylint: disable=used-before-assignment if port2: - print("WARNING: TLS is NOT supported on this system, not listening on port %s." % port2) + print(f"WARNING: TLS is NOT supported on this system, not listening on port {port2}.") asyncore.loop() diff --git a/tests/integration/targets/module_helper/library/mdepfail.py b/tests/integration/targets/module_helper/library/mdepfail.py index 6125d2980d..3168a41e11 100644 --- a/tests/integration/targets/module_helper/library/mdepfail.py +++ b/tests/integration/targets/module_helper/library/mdepfail.py @@ -32,7 +32,7 @@ from ansible_collections.community.general.plugins.module_utils import deps from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper with deps.declare("nopackagewiththisname"): - import nopackagewiththisname # noqa: F401, pylint: disable=unused-import + import nopackagewiththisname # noqa: F401, pylint: disable=unused-import # type: ignore[import-not-found] class MSimple(ModuleHelper): diff --git a/tests/integration/targets/monit/files/httpd_echo.py b/tests/integration/targets/monit/files/httpd_echo.py index bbe3f46e6a..d1941e9323 100644 --- a/tests/integration/targets/monit/files/httpd_echo.py +++ b/tests/integration/targets/monit/files/httpd_echo.py @@ -6,16 +6,11 @@ from __future__ import annotations import daemon -try: - from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer +from http.server import BaseHTTPRequestHandler, HTTPServer - def write_to_output(stream, content): - stream.write(content) -except ImportError: - from http.server import BaseHTTPRequestHandler, HTTPServer - def write_to_output(stream, content): - stream.write(bytes(content, "utf-8")) +def write_to_output(stream, content): + stream.write(bytes(content, "utf-8")) hostname = "localhost" diff --git a/tests/integration/targets/supervisorctl/files/sendProcessStdin.py b/tests/integration/targets/supervisorctl/files/sendProcessStdin.py index 41760ee993..28f9c78279 100644 --- a/tests/integration/targets/supervisorctl/files/sendProcessStdin.py +++ b/tests/integration/targets/supervisorctl/files/sendProcessStdin.py @@ -22,4 +22,4 @@ else: url = "http://127.0.0.1:9001/RPC2" server = ServerProxy(url, verbose=True) -server.supervisor.sendProcessStdin(proc, "import sys; print(%s); sys.stdout.flush();\n" % value) +server.supervisor.sendProcessStdin(proc, f"import sys; print({value}); sys.stdout.flush();\n") diff --git a/tests/sanity/extra/botmeta.py b/tests/sanity/extra/botmeta.py index ed7dd603b2..c1c9d149a1 100644 --- a/tests/sanity/extra/botmeta.py +++ b/tests/sanity/extra/botmeta.py @@ -56,7 +56,7 @@ IGNORE_NO_MAINTAINERS = [ class BotmetaCheck: - def __init__(self): + def __init__(self) -> None: self.errors: list[str] = [] self.botmeta_filename = ".github/BOTMETA.yml" self.list_entries = frozenset(("supershipit", "maintainers", "labels", "keywords", "notify", "ignore")) @@ -121,8 +121,8 @@ class BotmetaCheck: ) ): maintainers = self.read_authors(filename) - for maintainer in maintainers: - maintainer = self.extract_author_name(maintainer) + for maintainer_str in maintainers: + maintainer = self.extract_author_name(maintainer_str) if maintainer is not None and maintainer not in all_maintainers: others = ", ".join(all_maintainers) msg = f"Author {maintainer} not mentioned as active or inactive maintainer for {filename} (mentioned are: {others})"