From a4ef186537848f3549191f430da09aede529bf18 Mon Sep 17 00:00:00 2001 From: tdadela Date: Sun, 14 Jan 2024 19:10:33 +0100 Subject: [PATCH 1/5] set python target-version for ruff --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 299cab6cdd..715904762c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ license-files = ["LICENSE"] include-package-data = false [tool.ruff] +target-version = "py38" line-length = 120 extend-exclude = [ "build", From b44bc0feacd72ca1a1c1386ec65429f64e3dd61b Mon Sep 17 00:00:00 2001 From: tdadela Date: Sun, 14 Jan 2024 19:12:09 +0100 Subject: [PATCH 2/5] ruff: add future-required-type-annotation & pyupgrade linter rules --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 715904762c..3151baae30 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,7 +68,7 @@ extend-exclude = [ "src/readthedocs-sphinx-search/", ] ignore = ["E402", "E501", "E713", "E731", "E741", "F401"] -select = ["E", "F", "W"] +select = ["E", "F", "W", "UP", "FA102"] [tool.ruff.per-file-ignores] "examples/*" = ["F841"] From 60f3bceacc4ab9567433d40ae3ed280750f55ff1 Mon Sep 17 00:00:00 2001 From: tdadela Date: Sun, 14 Jan 2024 19:45:33 +0100 Subject: [PATCH 3/5] upgrade code style to 3.8 using pyupgrade --- benchmarks/dispatch.py | 4 ++-- locust/dispatch.py | 2 +- locust/runners.py | 11 +++++------ locust/user/task.py | 6 ++---- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/benchmarks/dispatch.py b/benchmarks/dispatch.py index 3c5fed2fbc..1a8be67091 100644 --- a/benchmarks/dispatch.py +++ b/benchmarks/dispatch.py @@ -632,8 +632,8 @@ class User100(User): print() print(table) - with open(f"results-dispatch-benchmarks-{int(now)}.txt", "wt") as file: + with open(f"results-dispatch-benchmarks-{int(now)}.txt", "w") as file: file.write(table.get_string()) - with open(f"results-dispatch-benchmarks-{int(now)}.json", "wt") as file: + with open(f"results-dispatch-benchmarks-{int(now)}.json", "w") as file: file.write(table.get_json_string()) diff --git a/locust/dispatch.py b/locust/dispatch.py index dda49d46f4..34b2db7fd2 100644 --- a/locust/dispatch.py +++ b/locust/dispatch.py @@ -124,7 +124,7 @@ def _sort_workers(self): worker_nodes_by_id = sorted(self._worker_nodes, key=lambda w: w.id) # Give every worker an index indicating how many workers came before it on that host - workers_per_host = defaultdict(lambda: 0) + workers_per_host = defaultdict(int) for worker_node in worker_nodes_by_id: host = worker_node.id.split("_")[0] worker_node._index_within_host = workers_per_host[host] diff --git a/locust/runners.py b/locust/runners.py index 42d96e765f..e9d395120a 100644 --- a/locust/runners.py +++ b/locust/runners.py @@ -215,8 +215,7 @@ def spawn_users(self, user_classes_spawn_count: dict[str, int], wait: bool = Fal self.update_state(STATE_SPAWNING) logger.debug( - "Spawning additional %s (%s already running)..." - % (json.dumps(user_classes_spawn_count), json.dumps(self.user_classes_count)) + f"Spawning additional {json.dumps(user_classes_spawn_count)} ({json.dumps(self.user_classes_count)} already running)..." ) def spawn(user_class: str, spawn_count: int) -> list[User]: @@ -902,7 +901,7 @@ def quit(self) -> None: self.stop(send_stop_to_client=False) logger.debug("Quitting...") for client in self.clients.all: - logger.debug("Sending quit message to worker %s (index %s)" % (client.id, self.get_worker_index(client.id))) + logger.debug(f"Sending quit message to worker {client.id} (index {self.get_worker_index(client.id)})") self.server.send_to_client(Message("quit", None, client.id)) gevent.sleep(0.5) # wait for final stats report from all workers self.greenlet.kill(block=True) @@ -1123,7 +1122,7 @@ def worker_count(self) -> int: @property def reported_user_classes_count(self) -> dict[str, int]: - reported_user_classes_count: dict[str, int] = defaultdict(lambda: 0) + reported_user_classes_count: dict[str, int] = defaultdict(int) for client in self.clients.ready + self.clients.spawning + self.clients.running: for name, count in client.user_classes_count.items(): reported_user_classes_count[name] += count @@ -1139,11 +1138,11 @@ def send_message(self, msg_type: str, data: dict[str, Any] | None = None, client If None, will send to all attached workers """ if client_id: - logger.debug("Sending %s message to worker %s" % (msg_type, client_id)) + logger.debug(f"Sending {msg_type} message to worker {client_id}") self.server.send_to_client(Message(msg_type, data, client_id)) else: for client in self.clients.all: - logger.debug("Sending %s message to worker %s" % (msg_type, client.id)) + logger.debug(f"Sending {msg_type} message to worker {client.id}") self.server.send_to_client(Message(msg_type, data, client.id)) diff --git a/locust/user/task.py b/locust/user/task.py index d7d55aa2c3..ab680aa03b 100644 --- a/locust/user/task.py +++ b/locust/user/task.py @@ -423,10 +423,8 @@ class Tasks(TaskSet): return random.randint(self.min_wait, self.max_wait) / 1000.0 else: raise MissingWaitTimeError( - "You must define a wait_time method on either the %s or %s class" - % ( - type(self.user).__name__, - type(self).__name__, + "You must define a wait_time method on either the {} or {} class".format( + type(self.user).__name__, type(self).__name__ ) ) From 462065f0a22d41c52dfa3c1cbfb277c342993511 Mon Sep 17 00:00:00 2001 From: tdadela Date: Sun, 14 Jan 2024 19:49:23 +0100 Subject: [PATCH 4/5] add pyupgrade commit to .git-blame-ignore-revs --- .git-blame-ignore-revs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index f643713dcf..993e19b3a2 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -7,3 +7,5 @@ 313b80f27f525441c449593a3aeaf38389f63c13 # upgrade typing annotations using fix-future-annotations b5324820b299b1fe7da0608f0cc8ec47f58b1e40 +# upgrade code style to 3.8 using pyupgrade +60f3bceacc4ab9567433d40ae3ed280750f55ff1 From a94d54a01a8c53050983196b8ae7c607fea265b0 Mon Sep 17 00:00:00 2001 From: tdadela Date: Mon, 15 Jan 2024 21:52:42 +0100 Subject: [PATCH 5/5] minor: .format -> f-string --- locust/user/task.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/locust/user/task.py b/locust/user/task.py index ab680aa03b..06e89cb533 100644 --- a/locust/user/task.py +++ b/locust/user/task.py @@ -423,9 +423,7 @@ class Tasks(TaskSet): return random.randint(self.min_wait, self.max_wait) / 1000.0 else: raise MissingWaitTimeError( - "You must define a wait_time method on either the {} or {} class".format( - type(self.user).__name__, type(self).__name__ - ) + "You must define a wait_time method on either the {type(self.user).__name__} or {type(self).__name__} class" ) def wait(self):