Skip to content

Commit

Permalink
Made Listener covariant in the stream type variable (agronholm#464)
Browse files Browse the repository at this point in the history
For example, when a function expectes Listener[ByteStream], it will accept Listener[SocketStream] or Listener[TLSStream] too.
  • Loading branch information
mila committed Oct 2, 2022
1 parent 481b553 commit 70188d3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This library adheres to `Semantic Versioning 2.0 <http:https://semver.org/>`_.

- The ``TaskStatus`` class is now generic, and should be parametrized to indicate the
type of the value passed to ``task_status.started()``
- The ``Listener`` class is now covariant in its stream type
- Fixed ``CapacityLimiter`` on the asyncio backend to order waiting tasks in the FIFO
order (instead of LIFO) (PR by Conor Stevenson)
- Fixed ``CancelScope.cancel()`` not working on asyncio if called before entering the
Expand Down
6 changes: 4 additions & 2 deletions src/anyio/abc/_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
TypedAttributeSet,
typed_attribute,
)
from ._streams import ByteStream, Listener, T_Stream, UnreliableObjectStream
from ._streams import ByteStream, Listener, UnreliableObjectStream
from ._tasks import TaskGroup

IPAddressType = Union[str, IPv4Address, IPv6Address]
Expand Down Expand Up @@ -133,7 +133,9 @@ async def accept(self) -> SocketStream:
"""Accept an incoming connection."""

async def serve(
self, handler: Callable[[T_Stream], Any], task_group: TaskGroup | None = None
self,
handler: Callable[[SocketStream], Any],
task_group: TaskGroup | None = None,
) -> None:
from .. import create_task_group

Expand Down
6 changes: 3 additions & 3 deletions src/anyio/abc/_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ._tasks import TaskGroup

T_Item = TypeVar("T_Item")
T_Stream = TypeVar("T_Stream")
T_Stream_co = TypeVar("T_Stream_co", covariant=True)


class UnreliableObjectReceiveStream(
Expand Down Expand Up @@ -186,12 +186,12 @@ async def send_eof(self) -> None:
AnyByteStream = Union[ObjectStream[bytes], ByteStream]


class Listener(Generic[T_Stream], AsyncResource, TypedAttributeProvider):
class Listener(Generic[T_Stream_co], AsyncResource, TypedAttributeProvider):
"""An interface for objects that let you accept incoming connections."""

@abstractmethod
async def serve(
self, handler: Callable[[T_Stream], Any], task_group: TaskGroup | None = None
self, handler: Callable[[T_Stream_co], Any], task_group: TaskGroup | None = None
) -> None:
"""
Accept incoming connections as they come in and start tasks to handle them.
Expand Down

0 comments on commit 70188d3

Please sign in to comment.