From e8d111e2ef2eaba1bd40146bbd736c721e81525f Mon Sep 17 00:00:00 2001 From: Julien Cristau Date: Tue, 3 Dec 2024 11:11:30 +0100 Subject: [PATCH] chore: replace %-formatting of strings with f-strings or `format` method Fixes ruff UP301 errors. --- src/taskgraph/generator.py | 15 +++++++++------ src/taskgraph/transforms/fetch.py | 4 ++-- src/taskgraph/util/shell.py | 2 +- test/test_optimize.py | 2 +- test/test_util_archive.py | 8 ++++---- test/test_util_docker.py | 4 ++-- test/test_util_parameterization.py | 4 ++-- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/taskgraph/generator.py b/src/taskgraph/generator.py index 3fea99f0..7f32cb77 100644 --- a/src/taskgraph/generator.py +++ b/src/taskgraph/generator.py @@ -338,8 +338,9 @@ def _run(self): all_tasks, Graph(frozenset(full_task_set.graph.nodes), frozenset(edges)) ) logger.info( - "Full task graph contains %d tasks and %d dependencies" - % (len(full_task_set.graph.nodes), len(edges)) + "Full task graph contains {} tasks and {} dependencies".format( + len(full_task_set.graph.nodes), len(edges) + ) ) yield self.verify("full_task_graph", full_task_graph, graph_config, parameters) @@ -356,8 +357,9 @@ def _run(self): Graph(frozenset(target_tasks), frozenset()), ) logger.info( - "Filter %s pruned %d tasks (%d remain)" - % (fltr.__name__, old_len - len(target_tasks), len(target_tasks)) + "Filter {} pruned {} tasks ({} remain)".format( + fltr.__name__, old_len - len(target_tasks), len(target_tasks) + ) ) yield self.verify("target_task_set", target_task_set, graph_config, parameters) @@ -375,8 +377,9 @@ def _run(self): else: always_target_tasks = set() logger.info( - "Adding %d tasks with `always_target` attribute" - % (len(always_target_tasks) - len(always_target_tasks & target_tasks)) # type: ignore + "Adding {} tasks with `always_target` attribute".format( + (len(always_target_tasks) - len(always_target_tasks & target_tasks)) + ) ) requested_tasks = target_tasks | always_target_tasks # type: ignore target_graph = full_task_graph.graph.transitive_closure(requested_tasks) diff --git a/src/taskgraph/transforms/fetch.py b/src/taskgraph/transforms/fetch.py index 895167a6..a38f0a9b 100644 --- a/src/taskgraph/transforms/fetch.py +++ b/src/taskgraph/transforms/fetch.py @@ -231,11 +231,11 @@ def create_fetch_url_task(config, name, fetch): "--sha256", fetch["sha256"], "--size", - "%d" % fetch["size"], + f"{fetch['size']}", ] if fetch.get("strip-components"): - args.extend(["--strip-components", "%d" % fetch["strip-components"]]) + args.extend(["--strip-components", f'{fetch["strip-components"]}']) if fetch.get("add-prefix"): args.extend(["--add-prefix", fetch["add-prefix"]]) diff --git a/src/taskgraph/util/shell.py b/src/taskgraph/util/shell.py index 16b71b7d..a6f3e0d5 100644 --- a/src/taskgraph/util/shell.py +++ b/src/taskgraph/util/shell.py @@ -15,7 +15,7 @@ def _quote(s): not enclosed in quotes. """ if isinstance(s, int): - return "%d" % s + return f"{s}" # Empty strings need to be quoted to have any significance if s and not SHELL_QUOTE_RE.search(s) and not s.startswith("~"): diff --git a/test/test_optimize.py b/test/test_optimize.py index 0769c544..cf2560d5 100644 --- a/test/test_optimize.py +++ b/test/test_optimize.py @@ -454,7 +454,7 @@ def test_get_subgraph(monkeypatch, graph, kwargs, exp_subgraph, exp_label_to_tas 4. The expected label_to_taskid. """ monkeypatch.setattr( - optimize_mod, "slugid", partial(next, ("tid%d" % i for i in range(1, 10))) + optimize_mod, "slugid", partial(next, (f"tid{i}" for i in range(1, 10))) ) kwargs.setdefault("removed_tasks", set()) diff --git a/test/test_util_archive.py b/test/test_util_archive.py index 6def3115..69c9c58c 100644 --- a/test/test_util_archive.py +++ b/test/test_util_archive.py @@ -38,22 +38,22 @@ class TestArchive(unittest.TestCase): def _create_files(self, root): files = {} for i in range(10): - p = os.path.join(root, "file%02d" % i) + p = os.path.join(root, f"file{i:02d}") with open(p, "wb") as fh: fh.write(b"file%02d" % i) # Need to set permissions or umask may influence testing. os.chmod(p, MODE_STANDARD) - files["file%02d" % i] = p + files[f"file{i:02d}"] = p for i in range(10): - files["file%02d" % (i + 10)] = io.BytesIO(b"file%02d" % (i + 10)) + files[f"file{i + 10:02d}"] = io.BytesIO(b"file%02d" % (i + 10)) return files def _verify_basic_tarfile(self, tf): self.assertEqual(len(tf.getmembers()), 20) - names = ["file%02d" % i for i in range(20)] + names = [f"file{i:02d}" for i in range(20)] self.assertEqual(tf.getnames(), names) for ti in tf.getmembers(): diff --git a/test/test_util_docker.py b/test/test_util_docker.py index b7c03841..18034bbe 100644 --- a/test/test_util_docker.py +++ b/test/test_util_docker.py @@ -204,7 +204,7 @@ def test_create_context_extra_directory(self): extra = os.path.join(tmp, "extra") os.mkdir(extra) for i in range(3): - p = os.path.join(extra, "file%d" % i) + p = os.path.join(extra, f"file{i}") with open(p, "wb") as fh: fh.write(b"file%d" % i) os.chmod(p, MODE_STANDARD) @@ -249,7 +249,7 @@ def test_stream_context_tar(self): extra = os.path.join(tmp, "extra") os.mkdir(extra) for i in range(3): - p = os.path.join(extra, "file%d" % i) + p = os.path.join(extra, f"file{i}") with open(p, "wb") as fh: fh.write(b"file%d" % i) os.chmod(p, MODE_STANDARD) diff --git a/test/test_util_parameterization.py b/test/test_util_parameterization.py index 15f1dbf0..5894b55a 100644 --- a/test/test_util_parameterization.py +++ b/test/test_util_parameterization.py @@ -42,7 +42,7 @@ def test_timestamps_appears_with_other_keys(): @pytest.fixture def assert_task_refs(): def inner(input, output): - taskid_for_edge_name = {"edge%d" % n: "tid%d" % n for n in range(1, 4)} + taskid_for_edge_name = {f"edge{n}": f"tid{n}" for n in range(1, 4)} assert ( resolve_task_references( "subject", @@ -151,7 +151,7 @@ def assert_artifact_refs(monkeypatch): def inner(input, output): # Clear memoized function get_root_url.cache_clear() - taskid_for_edge_name = {"edge%d" % n: "tid%d" % n for n in range(1, 4)} + taskid_for_edge_name = {f"edge{n}": f"tid{n}" for n in range(1, 4)} assert ( resolve_task_references( "subject", input, "tid-self", "tid-decision", taskid_for_edge_name