Skip to content

Commit 583b600

Browse files
asyncio: improve bytes handling (#9013)
1 parent b8659e6 commit 583b600

File tree

8 files changed

+43
-33
lines changed

8 files changed

+43
-33
lines changed

stdlib/asyncio/base_events.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ssl
22
import sys
3-
from _typeshed import FileDescriptorLike, WriteableBuffer
3+
from _typeshed import FileDescriptorLike, ReadableBuffer, WriteableBuffer
44
from asyncio.events import AbstractEventLoop, AbstractServer, Handle, TimerHandle, _TaskFactory
55
from asyncio.futures import Future
66
from asyncio.protocols import BaseProtocol
@@ -102,7 +102,7 @@ class BaseEventLoop(AbstractEventLoop):
102102
async def getaddrinfo(
103103
self,
104104
host: bytes | str | None,
105-
port: str | int | None,
105+
port: bytes | str | int | None,
106106
*,
107107
family: int = ...,
108108
type: int = ...,
@@ -411,13 +411,13 @@ class BaseEventLoop(AbstractEventLoop):
411411
# BaseEventLoop, only on subclasses. We list them here for now for convenience.
412412
async def sock_recv(self, sock: socket, nbytes: int) -> bytes: ...
413413
async def sock_recv_into(self, sock: socket, buf: WriteableBuffer) -> int: ...
414-
async def sock_sendall(self, sock: socket, data: bytes) -> None: ...
414+
async def sock_sendall(self, sock: socket, data: ReadableBuffer) -> None: ...
415415
async def sock_connect(self, sock: socket, address: _Address) -> None: ...
416416
async def sock_accept(self, sock: socket) -> tuple[socket, _RetAddress]: ...
417417
if sys.version_info >= (3, 11):
418418
async def sock_recvfrom(self, sock: socket, bufsize: int) -> bytes: ...
419419
async def sock_recvfrom_into(self, sock: socket, buf: WriteableBuffer, nbytes: int = ...) -> int: ...
420-
async def sock_sendto(self, sock: socket, data: bytes, address: _Address) -> None: ...
420+
async def sock_sendto(self, sock: socket, data: ReadableBuffer, address: _Address) -> None: ...
421421
# Signal handling.
422422
def add_signal_handler(self, sig: int, callback: Callable[..., Any], *args: Any) -> None: ...
423423
def remove_signal_handler(self, sig: int) -> bool: ...

stdlib/asyncio/events.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ssl
22
import sys
3-
from _typeshed import FileDescriptorLike, Self, StrPath, WriteableBuffer
3+
from _typeshed import FileDescriptorLike, ReadableBuffer, Self, StrPath, WriteableBuffer
44
from abc import ABCMeta, abstractmethod
55
from collections.abc import Awaitable, Callable, Coroutine, Generator, Sequence
66
from contextvars import Context
@@ -194,7 +194,7 @@ class AbstractEventLoop:
194194
async def getaddrinfo(
195195
self,
196196
host: bytes | str | None,
197-
port: str | int | None,
197+
port: bytes | str | int | None,
198198
*,
199199
family: int = ...,
200200
type: int = ...,
@@ -562,7 +562,7 @@ class AbstractEventLoop:
562562
@abstractmethod
563563
async def sock_recv_into(self, sock: socket, buf: WriteableBuffer) -> int: ...
564564
@abstractmethod
565-
async def sock_sendall(self, sock: socket, data: bytes) -> None: ...
565+
async def sock_sendall(self, sock: socket, data: ReadableBuffer) -> None: ...
566566
@abstractmethod
567567
async def sock_connect(self, sock: socket, address: _Address) -> None: ...
568568
@abstractmethod
@@ -573,7 +573,7 @@ class AbstractEventLoop:
573573
@abstractmethod
574574
async def sock_recvfrom_into(self, sock: socket, buf: WriteableBuffer, nbytes: int = ...) -> int: ...
575575
@abstractmethod
576-
async def sock_sendto(self, sock: socket, data: bytes, address: _Address) -> None: ...
576+
async def sock_sendto(self, sock: socket, data: ReadableBuffer, address: _Address) -> None: ...
577577
# Signal handling.
578578
@abstractmethod
579579
def add_signal_handler(self, sig: int, callback: Callable[..., object], *args: Any) -> None: ...

stdlib/asyncio/sslproto.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class _SSLProtocolTransport(transports._FlowControlMixin, transports.Transport):
7474
def get_extra_info(self, name: str, default: Any | None = ...) -> dict[str, Any]: ...
7575
@property
7676
def _protocol_paused(self) -> bool: ...
77-
def write(self, data: bytes) -> None: ...
77+
def write(self, data: bytes | bytearray | memoryview) -> None: ...
7878
def can_write_eof(self) -> Literal[False]: ...
7979
if sys.version_info >= (3, 11):
8080
def get_write_buffer_limits(self) -> tuple[int, int]: ...

stdlib/asyncio/streams.pyi

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sys
33
from _typeshed import Self, StrPath
44
from collections.abc import AsyncIterator, Awaitable, Callable, Iterable, Sequence
55
from typing import Any
6-
from typing_extensions import TypeAlias
6+
from typing_extensions import SupportsIndex, TypeAlias
77

88
from . import events, protocols, transports
99
from .base_events import Server
@@ -139,8 +139,8 @@ class StreamWriter:
139139
) -> None: ...
140140
@property
141141
def transport(self) -> transports.WriteTransport: ...
142-
def write(self, data: bytes) -> None: ...
143-
def writelines(self, data: Iterable[bytes]) -> None: ...
142+
def write(self, data: bytes | bytearray | memoryview) -> None: ...
143+
def writelines(self, data: Iterable[bytes | bytearray | memoryview]) -> None: ...
144144
def write_eof(self) -> None: ...
145145
def can_write_eof(self) -> bool: ...
146146
def close(self) -> None: ...
@@ -160,9 +160,10 @@ class StreamReader(AsyncIterator[bytes]):
160160
def set_transport(self, transport: transports.BaseTransport) -> None: ...
161161
def feed_eof(self) -> None: ...
162162
def at_eof(self) -> bool: ...
163-
def feed_data(self, data: bytes) -> None: ...
163+
def feed_data(self, data: Iterable[SupportsIndex]) -> None: ...
164164
async def readline(self) -> bytes: ...
165-
async def readuntil(self, separator: bytes = ...) -> bytes: ...
165+
# Can be any buffer that supports len(); consider changing to a Protocol if PEP 688 is accepted
166+
async def readuntil(self, separator: bytes | bytearray | memoryview = ...) -> bytes: ...
166167
async def read(self, n: int = ...) -> bytes: ...
167168
async def readexactly(self, n: int) -> bytes: ...
168169
def __aiter__(self: Self) -> Self: ...

stdlib/asyncio/subprocess.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Process:
3838
def send_signal(self, signal: int) -> None: ...
3939
def terminate(self) -> None: ...
4040
def kill(self) -> None: ...
41-
async def communicate(self, input: bytes | None = ...) -> tuple[bytes, bytes]: ...
41+
async def communicate(self, input: bytes | bytearray | memoryview | None = ...) -> tuple[bytes, bytes]: ...
4242

4343
if sys.version_info >= (3, 10):
4444
async def create_subprocess_shell(

stdlib/asyncio/transports.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class WriteTransport(BaseTransport):
2323
def set_write_buffer_limits(self, high: int | None = ..., low: int | None = ...) -> None: ...
2424
def get_write_buffer_size(self) -> int: ...
2525
def get_write_buffer_limits(self) -> tuple[int, int]: ...
26-
def write(self, data: bytes) -> None: ...
27-
def writelines(self, list_of_data: Iterable[bytes]) -> None: ...
26+
def write(self, data: bytes | bytearray | memoryview) -> None: ...
27+
def writelines(self, list_of_data: Iterable[bytes | bytearray | memoryview]) -> None: ...
2828
def write_eof(self) -> None: ...
2929
def can_write_eof(self) -> bool: ...
3030
def abort(self) -> None: ...
3131

3232
class Transport(ReadTransport, WriteTransport): ...
3333

3434
class DatagramTransport(BaseTransport):
35-
def sendto(self, data: bytes, addr: _Address | None = ...) -> None: ...
35+
def sendto(self, data: bytes | bytearray | memoryview, addr: _Address | None = ...) -> None: ...
3636
def abort(self) -> None: ...
3737

3838
class SubprocessTransport(BaseTransport):

stdlib/asyncio/trsock.pyi

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import socket
22
import sys
3+
from _typeshed import ReadableBuffer
34
from builtins import type as Type # alias to avoid name clashes with property named "type"
45
from collections.abc import Iterable
56
from types import TracebackType
67
from typing import Any, BinaryIO, NoReturn, overload
78
from typing_extensions import TypeAlias
89

910
# These are based in socket, maybe move them out into _typeshed.pyi or such
10-
_Address: TypeAlias = tuple[Any, ...] | str
11+
_Address: TypeAlias = socket._Address
1112
_RetAddress: TypeAlias = Any
1213
_WriteBuffer: TypeAlias = bytearray | memoryview
1314
_CMSG: TypeAlias = tuple[int, int, bytes]
@@ -30,7 +31,7 @@ class TransportSocket:
3031
@overload
3132
def getsockopt(self, level: int, optname: int, buflen: int) -> bytes: ...
3233
@overload
33-
def setsockopt(self, level: int, optname: int, value: int | bytes) -> None: ...
34+
def setsockopt(self, level: int, optname: int, value: int | ReadableBuffer) -> None: ...
3435
@overload
3536
def setsockopt(self, level: int, optname: int, value: None, optlen: int) -> None: ...
3637
def getpeername(self) -> _RetAddress: ...
@@ -42,9 +43,9 @@ class TransportSocket:
4243
if sys.version_info < (3, 11):
4344
def _na(self, what: str) -> None: ...
4445
def accept(self) -> tuple[socket.socket, _RetAddress]: ...
45-
def connect(self, address: _Address | bytes) -> None: ...
46-
def connect_ex(self, address: _Address | bytes) -> int: ...
47-
def bind(self, address: _Address | bytes) -> None: ...
46+
def connect(self, address: _Address) -> None: ...
47+
def connect_ex(self, address: _Address) -> int: ...
48+
def bind(self, address: _Address) -> None: ...
4849
if sys.platform == "win32":
4950
def ioctl(self, control: int, option: int | tuple[int, int, int] | bool) -> None: ...
5051
else:
@@ -57,22 +58,26 @@ class TransportSocket:
5758
def detach(self) -> int: ...
5859
if sys.platform == "linux":
5960
def sendmsg_afalg(
60-
self, msg: Iterable[bytes] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ...
61+
self, msg: Iterable[ReadableBuffer] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ...
6162
) -> int: ...
6263
else:
6364
def sendmsg_afalg(
64-
self, msg: Iterable[bytes] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ...
65+
self, msg: Iterable[ReadableBuffer] = ..., *, op: int, iv: Any = ..., assoclen: int = ..., flags: int = ...
6566
) -> NoReturn: ...
6667

6768
def sendmsg(
68-
self, __buffers: Iterable[bytes], __ancdata: Iterable[_CMSG] = ..., __flags: int = ..., __address: _Address = ...
69+
self,
70+
__buffers: Iterable[ReadableBuffer],
71+
__ancdata: Iterable[_CMSG] = ...,
72+
__flags: int = ...,
73+
__address: _Address = ...,
6974
) -> int: ...
7075
@overload
71-
def sendto(self, data: bytes, address: _Address) -> int: ...
76+
def sendto(self, data: ReadableBuffer, address: _Address) -> int: ...
7277
@overload
73-
def sendto(self, data: bytes, flags: int, address: _Address) -> int: ...
74-
def send(self, data: bytes, flags: int = ...) -> int: ...
75-
def sendall(self, data: bytes, flags: int = ...) -> None: ...
78+
def sendto(self, data: ReadableBuffer, flags: int, address: _Address) -> int: ...
79+
def send(self, data: ReadableBuffer, flags: int = ...) -> int: ...
80+
def sendall(self, data: ReadableBuffer, flags: int = ...) -> None: ...
7681
def set_inheritable(self, inheritable: bool) -> None: ...
7782
if sys.platform == "win32":
7883
def share(self, process_id: int) -> bytes: ...

stdlib/asyncio/windows_events.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import socket
22
import sys
3-
from _typeshed import WriteableBuffer
3+
from _typeshed import Incomplete, WriteableBuffer
44
from collections.abc import Callable
55
from typing import IO, Any, ClassVar, NoReturn
66
from typing_extensions import Literal
@@ -50,10 +50,14 @@ if sys.platform == "win32":
5050
def recv_into(self, conn: socket.socket, buf: WriteableBuffer, flags: int = ...) -> futures.Future[Any]: ...
5151
def send(self, conn: socket.socket, buf: WriteableBuffer, flags: int = ...) -> futures.Future[Any]: ...
5252
def accept(self, listener: socket.socket) -> futures.Future[Any]: ...
53-
def connect(self, conn: socket.socket, address: bytes) -> futures.Future[Any]: ...
53+
def connect(
54+
self,
55+
conn: socket.socket,
56+
address: tuple[Incomplete, Incomplete] | tuple[Incomplete, Incomplete, Incomplete, Incomplete],
57+
) -> futures.Future[Any]: ...
5458
def sendfile(self, sock: socket.socket, file: IO[bytes], offset: int, count: int) -> futures.Future[Any]: ...
5559
def accept_pipe(self, pipe: socket.socket) -> futures.Future[Any]: ...
56-
async def connect_pipe(self, address: bytes) -> windows_utils.PipeHandle: ...
60+
async def connect_pipe(self, address: str) -> windows_utils.PipeHandle: ...
5761
def wait_for_handle(self, handle: windows_utils.PipeHandle, timeout: int | None = ...) -> bool: ...
5862
def close(self) -> None: ...
5963
SelectorEventLoop = _WindowsSelectorEventLoop

0 commit comments

Comments
 (0)