Skip to content

Commit 59f2eb3

Browse files
authored
Merge pull request #2502 from harahu/types-unix-pipes
Add types to `_unix_pipes`
2 parents 52eab84 + f207b2b commit 59f2eb3

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

trio/_unix_pipes.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from __future__ import annotations
2+
13
import os
24
import errno
5+
from typing_extensions import Final as FinalType
36

47
from ._abc import Stream
58
from ._util import ConflictDetector, Final
@@ -13,7 +16,7 @@
1316

1417
# XX TODO: is this a good number? who knows... it does match the default Linux
1518
# pipe capacity though.
16-
DEFAULT_RECEIVE_SIZE = 65536
19+
DEFAULT_RECEIVE_SIZE: FinalType = 65536
1720

1821

1922
class _FdHolder:
@@ -34,7 +37,9 @@ class _FdHolder:
3437
# impossible to make this mistake – we'll just get an EBADF.
3538
#
3639
# (This trick was copied from the stdlib socket module.)
37-
def __init__(self, fd: int):
40+
fd: int
41+
42+
def __init__(self, fd: int) -> None:
3843
# make sure self.fd is always initialized to *something*, because even
3944
# if we error out here then __del__ will run and access it.
4045
self.fd = -1
@@ -46,10 +51,10 @@ def __init__(self, fd: int):
4651
os.set_blocking(fd, False)
4752

4853
@property
49-
def closed(self):
54+
def closed(self) -> bool:
5055
return self.fd == -1
5156

52-
def _raw_close(self):
57+
def _raw_close(self) -> None:
5358
# This doesn't assume it's in a Trio context, so it can be called from
5459
# __del__. You should never call it from Trio context, because it
5560
# skips calling notify_fd_close. But from __del__, skipping that is
@@ -64,10 +69,10 @@ def _raw_close(self):
6469
os.set_blocking(fd, self._original_is_blocking)
6570
os.close(fd)
6671

67-
def __del__(self):
72+
def __del__(self) -> None:
6873
self._raw_close()
6974

70-
def close(self):
75+
def close(self) -> None:
7176
if not self.closed:
7277
trio.lowlevel.notify_closing(self.fd)
7378
self._raw_close()
@@ -93,7 +98,7 @@ class FdStream(Stream, metaclass=Final):
9398
thrust upon them. For example, you can use
9499
``FdStream(os.dup(sys.stdin.fileno()))`` to obtain a stream for reading
95100
from standard input, but it is only safe to do so with heavy caveats: your
96-
stdin must not be shared by any other processes and you must not make any
101+
stdin must not be shared by any other processes, and you must not make any
97102
calls to synchronous methods of `sys.stdin` until the stream returned by
98103
`FdStream` is closed. See `issue #174
99104
<https://github.com/python-trio/trio/issues/174>`__ for a discussion of the
@@ -106,7 +111,7 @@ class FdStream(Stream, metaclass=Final):
106111
A new `FdStream` object.
107112
"""
108113

109-
def __init__(self, fd: int):
114+
def __init__(self, fd: int) -> None:
110115
self._fd_holder = _FdHolder(fd)
111116
self._send_conflict_detector = ConflictDetector(
112117
"another task is using this stream for send"
@@ -115,7 +120,7 @@ def __init__(self, fd: int):
115120
"another task is using this stream for receive"
116121
)
117122

118-
async def send_all(self, data: bytes):
123+
async def send_all(self, data: bytes) -> None:
119124
with self._send_conflict_detector:
120125
# have to check up front, because send_all(b"") on a closed pipe
121126
# should raise
@@ -151,7 +156,7 @@ async def wait_send_all_might_not_block(self) -> None:
151156
# of sending, which is annoying
152157
raise trio.BrokenResourceError from e
153158

154-
async def receive_some(self, max_bytes=None) -> bytes:
159+
async def receive_some(self, max_bytes: int | None = None) -> bytes:
155160
with self._receive_conflict_detector:
156161
if max_bytes is None:
157162
max_bytes = DEFAULT_RECEIVE_SIZE
@@ -179,12 +184,12 @@ async def receive_some(self, max_bytes=None) -> bytes:
179184

180185
return data
181186

182-
def close(self):
187+
def close(self) -> None:
183188
self._fd_holder.close()
184189

185-
async def aclose(self):
190+
async def aclose(self) -> None:
186191
self.close()
187192
await trio.lowlevel.checkpoint()
188193

189-
def fileno(self):
194+
def fileno(self) -> int:
190195
return self._fd_holder.fd

0 commit comments

Comments
 (0)