From cf39441f47c9a1ee44ae9c7297c53609c01b889b Mon Sep 17 00:00:00 2001 From: Terence Honles Date: Fri, 8 Nov 2024 23:04:24 -0800 Subject: [PATCH] use `Serializer` Protocol instead of concrete `DefaultSerializer` (#2136) * WorkerPool should accept `Serializer` instead of `DefaultSerializer` * always use type Protocol Serializer instead of DefaultSerializer --------- Co-authored-by: Selwin Ong --- rq/cli/cli.py | 7 +++++-- rq/worker_pool.py | 11 +++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/rq/cli/cli.py b/rq/cli/cli.py index 1954400b..0d53ccbc 100755 --- a/rq/cli/cli.py +++ b/rq/cli/cli.py @@ -7,7 +7,7 @@ import logging.config import os import sys import warnings -from typing import List, Type +from typing import TYPE_CHECKING, List, Type, cast import click from redis.exceptions import ConnectionError @@ -48,6 +48,9 @@ from rq.worker import Worker from rq.worker_pool import WorkerPool from rq.worker_registration import clean_worker_registry +if TYPE_CHECKING: + from rq.serializers import Serializer + @click.group() @click.version_option(version) @@ -461,7 +464,7 @@ def worker_pool( setup_loghandlers_from_args(verbose, quiet, date_format, log_format) if serializer: - serializer_class: Type[DefaultSerializer] = import_attribute(serializer) # type: ignore[assignment] + serializer_class = cast(Type['Serializer'], import_attribute(serializer)) else: serializer_class = DefaultSerializer diff --git a/rq/worker_pool.py b/rq/worker_pool.py index 92a7f2a1..900b7b9b 100644 --- a/rq/worker_pool.py +++ b/rq/worker_pool.py @@ -6,7 +6,7 @@ import signal import time from enum import Enum from multiprocessing import Process -from typing import Dict, List, NamedTuple, Optional, Type, Union +from typing import TYPE_CHECKING, Dict, List, NamedTuple, Optional, Type, Union from uuid import uuid4 from redis import ConnectionPool, Redis @@ -21,6 +21,9 @@ from .queue import Queue from .utils import parse_names from .worker import BaseWorker, Worker +if TYPE_CHECKING: + from rq.serializers import Serializer + class WorkerData(NamedTuple): name: str @@ -40,7 +43,7 @@ class WorkerPool: connection: Redis, num_workers: int = 1, worker_class: Type[BaseWorker] = Worker, - serializer: Type[DefaultSerializer] = DefaultSerializer, + serializer: Type['Serializer'] = DefaultSerializer, job_class: Type[Job] = Job, *args, **kwargs, @@ -57,7 +60,7 @@ class WorkerPool: self._sleep: int = 0 self.status: self.Status = self.Status.IDLE # type: ignore self.worker_class: Type[BaseWorker] = worker_class - self.serializer: Type[DefaultSerializer] = serializer + self.serializer: Type['Serializer'] = serializer self.job_class: Type[Job] = job_class # A dictionary of WorkerData keyed by worker name @@ -244,7 +247,7 @@ def run_worker( connection_pool_class, connection_pool_kwargs: dict, worker_class: Type[BaseWorker] = Worker, - serializer: Type[DefaultSerializer] = DefaultSerializer, + serializer: Type['Serializer'] = DefaultSerializer, job_class: Type[Job] = Job, burst: bool = True, logging_level: str = "INFO",