From 195035e6d8eb88323bc4d2ca20448afbdd47c6fb Mon Sep 17 00:00:00 2001 From: Nikita Pastukhov Date: Sun, 15 Dec 2024 23:08:23 +0300 Subject: [PATCH] lint: fix rabbit response types --- faststream/_internal/types.py | 8 -------- faststream/middlewares/base.py | 14 ++++++++++++-- faststream/opentelemetry/middleware.py | 3 +-- faststream/prometheus/middleware.py | 3 +-- faststream/rabbit/response.py | 15 +++++++++++---- faststream/rabbit/schemas/proto.py | 2 +- 6 files changed, 26 insertions(+), 19 deletions(-) diff --git a/faststream/_internal/types.py b/faststream/_internal/types.py index 5aa5c1d63a..489d1b94b3 100644 --- a/faststream/_internal/types.py +++ b/faststream/_internal/types.py @@ -11,7 +11,6 @@ from typing_extensions import ( ParamSpec, TypeAlias, - TypeVar as TypeVar313, ) from faststream._internal.basic_types import AsyncFuncAny @@ -85,13 +84,6 @@ def __call__( ] -PublishCommandType = TypeVar313( - "PublishCommandType", - bound=PublishCommand, - default=PublishCommand, -) - - class PublisherMiddleware(Protocol): """Publisher middleware interface.""" diff --git a/faststream/middlewares/base.py b/faststream/middlewares/base.py index ca78b03ad1..131da032b4 100644 --- a/faststream/middlewares/base.py +++ b/faststream/middlewares/base.py @@ -1,9 +1,12 @@ from collections.abc import Awaitable from typing import TYPE_CHECKING, Any, Callable, Generic, Optional -from typing_extensions import Self +from typing_extensions import ( + Self, + TypeVar as TypeVar313, +) -from faststream._internal.types import PublishCommandType +from faststream.response import PublishCommand if TYPE_CHECKING: from types import TracebackType @@ -13,6 +16,13 @@ from faststream.message import StreamMessage +PublishCommandType = TypeVar313( + "PublishCommandType", + bound=PublishCommand, + default=PublishCommand, +) + + class BaseMiddleware(Generic[PublishCommandType]): """A base middleware class.""" diff --git a/faststream/opentelemetry/middleware.py b/faststream/opentelemetry/middleware.py index 42e323c85a..e1f07b4527 100644 --- a/faststream/opentelemetry/middleware.py +++ b/faststream/opentelemetry/middleware.py @@ -10,8 +10,7 @@ from opentelemetry.trace import Link, Span from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator -from faststream._internal.types import PublishCommandType -from faststream.middlewares.base import BaseMiddleware +from faststream.middlewares.base import BaseMiddleware, PublishCommandType from faststream.opentelemetry.baggage import Baggage from faststream.opentelemetry.consts import ( ERROR_TYPE, diff --git a/faststream/prometheus/middleware.py b/faststream/prometheus/middleware.py index 33d4950e62..db146eb4a1 100644 --- a/faststream/prometheus/middleware.py +++ b/faststream/prometheus/middleware.py @@ -3,10 +3,9 @@ from typing import TYPE_CHECKING, Any, Callable, Generic, Optional from faststream._internal.constants import EMPTY -from faststream._internal.types import PublishCommandType from faststream.exceptions import IgnoredException from faststream.message import SourceType -from faststream.middlewares.base import BaseMiddleware +from faststream.middlewares.base import BaseMiddleware, PublishCommandType from faststream.prometheus.consts import ( PROCESSING_STATUS_BY_ACK_STATUS, PROCESSING_STATUS_BY_HANDLER_EXCEPTION_MAP, diff --git a/faststream/rabbit/response.py b/faststream/rabbit/response.py index 9bac3f6417..f45cbb07ce 100644 --- a/faststream/rabbit/response.py +++ b/faststream/rabbit/response.py @@ -7,11 +7,18 @@ from faststream.response.publish_type import PublishType if TYPE_CHECKING: + from typing import TypedDict + from aio_pika.abc import TimeoutType from faststream.rabbit.publisher.options import MessageOptions from faststream.rabbit.types import AioPikaSendableMessage + class _PublishOptions(TypedDict): + timeout: TimeoutType + mandatory: bool + immediate: bool + class RabbitResponse(Response): def __init__( @@ -33,7 +40,7 @@ def __init__( ) self.message_options = message_options - self.publish_options = { + self.publish_options: _PublishOptions = { "mandatory": mandatory, "immediate": immediate, "timeout": timeout, @@ -41,7 +48,7 @@ def __init__( @override def as_publish_command(self) -> "RabbitPublishCommand": - return RabbitPublishCommand( + return RabbitPublishCommand( # type: ignore[misc] message=self.body, headers=self.headers, correlation_id=self.correlation_id, @@ -65,11 +72,11 @@ def __init__( mandatory: bool = True, immediate: bool = False, timeout: "TimeoutType" = None, - correlation_id: Optional[str] = None, **message_options: Unpack["MessageOptions"], ) -> None: headers = message_options.pop("headers", {}) - reply_to = message_options.pop("reply_to", "") + reply_to = message_options.pop("reply_to") or "" + correlation_id = message_options.pop("correlation_id", None) super().__init__( body=message, diff --git a/faststream/rabbit/schemas/proto.py b/faststream/rabbit/schemas/proto.py index 2109772124..2929ce7c4f 100644 --- a/faststream/rabbit/schemas/proto.py +++ b/faststream/rabbit/schemas/proto.py @@ -37,4 +37,4 @@ def _setup( self.virtual_host = virtual_host # Setup next parent class - super()._setup(**kwargs) + super()._setup(**kwargs) # type: ignore[misc]