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 <selwin.ong@gmail.com>
This commit is contained in:
Terence Honles 2024-11-08 23:04:24 -08:00 committed by GitHub
parent b44ae85a8d
commit cf39441f47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 6 deletions

View File

@ -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

View File

@ -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",