Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of using __ for positional only arguments #3151

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/3094.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Switch to using PEP570 for positional-only arguments for `~trio.socket.SocketType`'s methods.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, Sphinx has a :pep: role that one can use for PEP links in RST.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened #3160

134 changes: 68 additions & 66 deletions src/trio/_socket.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# ruff: noqa: PYI063 # Several cases throughout file where
# argument names with __ used because of typeshed, see comment for recv in _SocketType

from __future__ import annotations

import os
Expand Down Expand Up @@ -567,27 +564,25 @@ def getsockname(self) -> AddressFormat:
raise NotImplementedError

@overload
def getsockopt(self, /, level: int, optname: int) -> int: ...
def getsockopt(self, level: int, optname: int) -> int: ...

@overload
def getsockopt(self, /, level: int, optname: int, buflen: int) -> bytes: ...
def getsockopt(self, level: int, optname: int, buflen: int) -> bytes: ...

def getsockopt(
self,
/,
level: int,
optname: int,
buflen: int | None = None,
) -> int | bytes:
raise NotImplementedError

@overload
def setsockopt(self, /, level: int, optname: int, value: int | Buffer) -> None: ...
def setsockopt(self, level: int, optname: int, value: int | Buffer) -> None: ...

@overload
def setsockopt(
self,
/,
level: int,
optname: int,
value: None,
Expand All @@ -596,15 +591,14 @@ def setsockopt(

def setsockopt(
self,
/,
level: int,
optname: int,
value: int | Buffer | None,
optlen: int | None = None,
) -> None:
raise NotImplementedError

def listen(self, /, backlog: int = min(_stdlib_socket.SOMAXCONN, 128)) -> None:
def listen(self, backlog: int = min(_stdlib_socket.SOMAXCONN, 128)) -> None:
raise NotImplementedError

def get_inheritable(self) -> bool:
Expand All @@ -617,7 +611,7 @@ def set_inheritable(self, inheritable: bool) -> None:
not TYPE_CHECKING and hasattr(_stdlib_socket.socket, "share")
):

def share(self, /, process_id: int) -> bytes:
def share(self, process_id: int) -> bytes:
raise NotImplementedError

def __enter__(self) -> Self:
Expand Down Expand Up @@ -677,11 +671,11 @@ async def accept(self) -> tuple[SocketType, AddressFormat]:
async def connect(self, address: AddressFormat) -> None:
raise NotImplementedError

def recv(__self, __buflen: int, __flags: int = 0) -> Awaitable[bytes]:
def recv(self, buflen: int, flags: int = 0, /) -> Awaitable[bytes]:
raise NotImplementedError

def recv_into(
__self,
self,
buffer: Buffer,
nbytes: int = 0,
flags: int = 0,
Expand All @@ -690,15 +684,16 @@ def recv_into(

# return type of socket.socket.recvfrom in typeshed is tuple[bytes, Any]
def recvfrom(
__self,
__bufsize: int,
__flags: int = 0,
self,
bufsize: int,
flags: int = 0,
/,
) -> Awaitable[tuple[bytes, AddressFormat]]:
raise NotImplementedError

# return type of socket.socket.recvfrom_into in typeshed is tuple[bytes, Any]
def recvfrom_into(
__self,
self,
buffer: Buffer,
nbytes: int = 0,
flags: int = 0,
Expand All @@ -710,10 +705,11 @@ def recvfrom_into(
):

def recvmsg(
__self,
__bufsize: int,
__ancbufsize: int = 0,
__flags: int = 0,
self,
bufsize: int,
ancbufsize: int = 0,
flags: int = 0,
/,
) -> Awaitable[tuple[bytes, list[tuple[int, int, bytes]], int, object]]:
raise NotImplementedError

Expand All @@ -722,29 +718,32 @@ def recvmsg(
):

def recvmsg_into(
__self,
__buffers: Iterable[Buffer],
__ancbufsize: int = 0,
__flags: int = 0,
self,
buffers: Iterable[Buffer],
ancbufsize: int = 0,
flags: int = 0,
/,
) -> Awaitable[tuple[int, list[tuple[int, int, bytes]], int, object]]:
raise NotImplementedError

def send(__self, __bytes: Buffer, __flags: int = 0) -> Awaitable[int]:
def send(self, bytes: Buffer, flags: int = 0, /) -> Awaitable[int]:
raise NotImplementedError

@overload
async def sendto(
self,
__data: Buffer,
__address: tuple[object, ...] | str | Buffer,
data: Buffer,
address: tuple[object, ...] | str | Buffer,
/,
) -> int: ...

@overload
async def sendto(
self,
__data: Buffer,
__flags: int,
__address: tuple[object, ...] | str | Buffer,
data: Buffer,
flags: int,
address: tuple[object, ...] | str | Buffer,
/,
) -> int: ...

async def sendto(self, *args: object) -> int:
Expand All @@ -757,10 +756,11 @@ async def sendto(self, *args: object) -> int:
@_wraps(_stdlib_socket.socket.sendmsg, assigned=(), updated=())
async def sendmsg(
self,
__buffers: Iterable[Buffer],
__ancdata: Iterable[tuple[int, int, Buffer]] = (),
__flags: int = 0,
__address: AddressFormat | None = None,
buffers: Iterable[Buffer],
ancdata: Iterable[tuple[int, int, Buffer]] = (),
flags: int = 0,
address: AddressFormat | None = None,
/,
) -> int:
raise NotImplementedError

Expand Down Expand Up @@ -810,14 +810,13 @@ def getsockname(self) -> AddressFormat:
return self._sock.getsockname()

@overload
def getsockopt(self, /, level: int, optname: int) -> int: ...
def getsockopt(self, level: int, optname: int) -> int: ...

@overload
def getsockopt(self, /, level: int, optname: int, buflen: int) -> bytes: ...
def getsockopt(self, level: int, optname: int, buflen: int) -> bytes: ...

def getsockopt(
self,
/,
level: int,
optname: int,
buflen: int | None = None,
Expand All @@ -827,12 +826,11 @@ def getsockopt(
return self._sock.getsockopt(level, optname, buflen)

@overload
def setsockopt(self, /, level: int, optname: int, value: int | Buffer) -> None: ...
def setsockopt(self, level: int, optname: int, value: int | Buffer) -> None: ...

@overload
def setsockopt(
self,
/,
level: int,
optname: int,
value: None,
Expand All @@ -841,7 +839,6 @@ def setsockopt(

def setsockopt(
self,
/,
level: int,
optname: int,
value: int | Buffer | None,
Expand All @@ -862,7 +859,7 @@ def setsockopt(
# four parameters.
return self._sock.setsockopt(level, optname, value, optlen)

def listen(self, /, backlog: int = min(_stdlib_socket.SOMAXCONN, 128)) -> None:
def listen(self, backlog: int = min(_stdlib_socket.SOMAXCONN, 128)) -> None:
return self._sock.listen(backlog)

def get_inheritable(self) -> bool:
Expand All @@ -875,7 +872,7 @@ def set_inheritable(self, inheritable: bool) -> None:
not TYPE_CHECKING and hasattr(_stdlib_socket.socket, "share")
):

def share(self, /, process_id: int) -> bytes:
def share(self, process_id: int) -> bytes:
return self._sock.share(process_id)

def __enter__(self) -> Self:
Expand Down Expand Up @@ -1118,11 +1115,8 @@ async def connect(self, address: AddressFormat) -> None:
# complain about AmbiguousType
if TYPE_CHECKING:

def recv(__self, __buflen: int, __flags: int = 0) -> Awaitable[bytes]: ...
def recv(self, buflen: int, flags: int = 0, /) -> Awaitable[bytes]: ...

# _make_simple_sock_method_wrapper is typed, so this checks that the above is correct
# this requires that we refrain from using `/` to specify pos-only
# args, or mypy thinks the signature differs from typeshed.
recv = _make_simple_sock_method_wrapper(
_stdlib_socket.socket.recv,
_core.wait_readable,
Expand All @@ -1135,7 +1129,8 @@ def recv(__self, __buflen: int, __flags: int = 0) -> Awaitable[bytes]: ...
if TYPE_CHECKING:

def recv_into(
__self,
self,
/,
buffer: Buffer,
nbytes: int = 0,
flags: int = 0,
Expand All @@ -1153,9 +1148,10 @@ def recv_into(
if TYPE_CHECKING:
# return type of socket.socket.recvfrom in typeshed is tuple[bytes, Any]
def recvfrom(
__self,
__bufsize: int,
__flags: int = 0,
self,
bufsize: int,
flags: int = 0,
/,
) -> Awaitable[tuple[bytes, AddressFormat]]: ...

recvfrom = _make_simple_sock_method_wrapper(
Expand All @@ -1170,7 +1166,8 @@ def recvfrom(
if TYPE_CHECKING:
# return type of socket.socket.recvfrom_into in typeshed is tuple[bytes, Any]
def recvfrom_into(
__self,
self,
/,
buffer: Buffer,
nbytes: int = 0,
flags: int = 0,
Expand All @@ -1191,10 +1188,11 @@ def recvfrom_into(
if TYPE_CHECKING:

def recvmsg(
__self,
__bufsize: int,
__ancbufsize: int = 0,
__flags: int = 0,
self,
bufsize: int,
ancbufsize: int = 0,
flags: int = 0,
/,
) -> Awaitable[tuple[bytes, list[tuple[int, int, bytes]], int, object]]: ...

recvmsg = _make_simple_sock_method_wrapper(
Expand All @@ -1213,10 +1211,11 @@ def recvmsg(
if TYPE_CHECKING:

def recvmsg_into(
__self,
__buffers: Iterable[Buffer],
__ancbufsize: int = 0,
__flags: int = 0,
self,
buffers: Iterable[Buffer],
ancbufsize: int = 0,
flags: int = 0,
/,
) -> Awaitable[tuple[int, list[tuple[int, int, bytes]], int, object]]: ...

recvmsg_into = _make_simple_sock_method_wrapper(
Expand All @@ -1231,7 +1230,7 @@ def recvmsg_into(

if TYPE_CHECKING:

def send(__self, __bytes: Buffer, __flags: int = 0) -> Awaitable[int]: ...
def send(self, bytes: Buffer, flags: int = 0, /) -> Awaitable[int]: ...

send = _make_simple_sock_method_wrapper(
_stdlib_socket.socket.send,
Expand All @@ -1245,16 +1244,18 @@ def send(__self, __bytes: Buffer, __flags: int = 0) -> Awaitable[int]: ...
@overload
async def sendto(
self,
__data: Buffer,
__address: tuple[object, ...] | str | Buffer,
data: Buffer,
address: tuple[object, ...] | str | Buffer,
/,
) -> int: ...

@overload
async def sendto(
self,
__data: Buffer,
__flags: int,
__address: tuple[object, ...] | str | Buffer,
data: Buffer,
flags: int,
address: tuple[object, ...] | str | Buffer,
/,
) -> int: ...

@_wraps(_stdlib_socket.socket.sendto, assigned=(), updated=())
Expand Down Expand Up @@ -1287,6 +1288,7 @@ async def sendmsg(
ancdata: Iterable[tuple[int, int, Buffer]] = (),
flags: int = 0,
address: AddressFormat | None = None,
/,
) -> int:
"""Similar to :meth:`socket.socket.sendmsg`, but async.

Expand Down
Loading