Skip to content

Commit

Permalink
Change type annotation from list to Sequence in send_batch and …
Browse files Browse the repository at this point in the history
…`send_sub_entry` methods to make variable covariant https://mypy.readthedocs.io/en/stable/generics.html#variance-of-generic-types (#219)
  • Loading branch information
nesb1 authored Dec 10, 2024
1 parent f858d85 commit 692df49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions rstream/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from collections import defaultdict
from dataclasses import dataclass
from enum import Enum
from typing import TypeVar
from typing import Sequence, TypeVar

from .amqp import _MessageProtocol
from .utils import RawMessage
Expand All @@ -25,7 +25,7 @@ class CompressionType(Enum):

class ICompressionCodec(abc.ABC):
@abc.abstractmethod
def compress(self, messages: list[MessageT]):
def compress(self, messages: Sequence[MessageT]):
pass

@abc.abstractmethod
Expand Down Expand Up @@ -60,7 +60,7 @@ class NoneCompressionCodec(ICompressionCodec):
message_count: int = 0
buffer: bytes = bytes()

def compress(self, messages: list[MessageT]):
def compress(self, messages: Sequence[MessageT]):
uncompressed_data = bytes()
for item in messages:
msg = RawMessage(item) if isinstance(item, bytes) else item
Expand Down Expand Up @@ -99,7 +99,7 @@ class GzipCompressionCodec(ICompressionCodec):
message_count: int = 0
buffer: bytes = bytes()

def compress(self, messages: list[MessageT]):
def compress(self, messages: Sequence[MessageT]):
uncompressed_data = bytes()
for item in messages:
msg = RawMessage(item) if isinstance(item, bytes) else item
Expand Down Expand Up @@ -152,7 +152,7 @@ def get_compression_codec(compression_type: CompressionType) -> ICompressionCode

class CompressionHelper:
@staticmethod
def compress(messages: list[MessageT], compression_type: CompressionType) -> ICompressionCodec:
def compress(messages: Sequence[MessageT], compression_type: CompressionType) -> ICompressionCodec:
codec = StreamCompressionCodecs.get_compression_codec(compression_type=compression_type)
codec.compress(messages=messages)
return codec
Expand Down
7 changes: 4 additions & 3 deletions rstream/producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Generic,
NoReturn,
Optional,
Sequence,
TypeVar,
Union,
)
Expand Down Expand Up @@ -291,7 +292,7 @@ async def _get_or_create_publisher(
async def send_batch(
self,
stream: str,
batch: list[MessageT],
batch: Sequence[MessageT],
publisher_name: Optional[str] = None,
on_publish_confirm: Optional[CB[ConfirmationStatus]] = None,
) -> list[int]:
Expand All @@ -311,7 +312,7 @@ async def send_batch(
async def _send_batch(
self,
stream: str,
batch: list[MessageT],
batch: Sequence[MessageT],
publisher_name: Optional[str] = None,
callback: Optional[CB[ConfirmationStatus]] = None,
sync: bool = True,
Expand Down Expand Up @@ -575,7 +576,7 @@ async def send(
async def send_sub_entry(
self,
stream: str,
sub_entry_messages: list[MessageT],
sub_entry_messages: Sequence[MessageT],
compression_type: CompressionType = CompressionType.No,
publisher_name: Optional[str] = None,
on_publish_confirm: Optional[CB[ConfirmationStatus]] = None,
Expand Down

0 comments on commit 692df49

Please sign in to comment.