Make Listener covariant in the stream type variable #464
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This changes allows to properly annotate return type in a function like:
Without this change, the function has to be annotated as returning
Union[Listener[SocketStream], TLSListener]
, which is long and it depends on specific implementation.The stream type of the listeners acts covariantly. When I have a handler that expects
ByteStream
, I needListener[ByteStream]
. But the handler won't mind if it getsTLSStream
, so I should not mind if I haveListener[TLSStream]
.For comparison, callables are covariant in the return type. For the listeners, the socket acts similar as the return value of the callable - it is an output, just passed to the handler callback instead of returned directly.
Note that I did not propagate the change to
anyio.streams.stapled.MultiListener
. The reason is similar why mutable collections are not covariant. The following function should not acceptMultiListener[TLSStream]
:I also did a small change to
anyio.abc.SocketListener
. Its use ofT_Stream
seems unnecessary, so I got rid of it instead of replacing it byT_Stream_co
.